initial commit
This commit is contained in:
commit
84abd9bcf1
5 changed files with 222 additions and 0 deletions
82
main.go
Normal file
82
main.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
running := true
|
||||
instance_path, err := filepath.Abs("/home/pauljako/boundaries")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
instance, err := filepath.EvalSymlinks(instance_path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
socket_path, err := filepath.Abs(filepath.Join(instance, "/boundaries.sock"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ln, err := net.Listen("unix", socket_path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Listening on", instance+"/boundaries.sock")
|
||||
signalChannel := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
sig := <-signalChannel
|
||||
switch sig {
|
||||
case os.Interrupt:
|
||||
running = false
|
||||
log.Println("Received interrupt")
|
||||
ln.Close()
|
||||
os.Exit(0)
|
||||
case syscall.SIGTERM:
|
||||
running = false
|
||||
log.Println("Received SIGTERM")
|
||||
ln.Close()
|
||||
os.Exit(15)
|
||||
}
|
||||
}()
|
||||
|
||||
accept_clients(&ln, &instance, &running)
|
||||
}
|
||||
|
||||
func exists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func accept_clients(ln *net.Listener, instance *string, running *bool) {
|
||||
for *running {
|
||||
conn, err := (*ln).Accept()
|
||||
if err != nil {
|
||||
if !(*running) {
|
||||
continue
|
||||
}
|
||||
log.Println(err)
|
||||
}
|
||||
go func() {
|
||||
err := client(conn, *instance)
|
||||
if err != nil {
|
||||
conn.Write([]byte(err.Error()))
|
||||
}
|
||||
conn.Close()
|
||||
}()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue