diff --git a/Makefile b/Makefile index b3c66d8..e10136c 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ race: go run --race cmd/server/main.go build: - go build -o build/server cmd/server/main.go + GOOS=linux GOARCH=amd64 go build -o build/server cmd/server/main.go test: go test -v -run=. test/**/*.go diff --git a/init/api-server.service b/init/api-server.service new file mode 100644 index 0000000..b41f2fe --- /dev/null +++ b/init/api-server.service @@ -0,0 +1,17 @@ +#FIXME: FIX PATHS + +[Unit] +Requires=network-online.target +After=network-online.target +Requires=api-server.socket + +[Service] +ExecStart=/home/keedosn/go/src/git.pbiernat.dev/api-prototype/build/server +WorkingDirectory=/home/keedosn/go/src/git.pbiernat.dev/api-prototype +User=keedosn +Group=keedosn +NonBlocking=true +StandardOutput=append:/home/keedosn/go/src/git.pbiernat.dev/api-prototype/build/err.log +StandardError=append:/home/keedosn/go/src/git.pbiernat.dev/api-prototype/build/err.log +SyslogIdentifier=api-server + diff --git a/init/api-server.socket b/init/api-server.socket new file mode 100644 index 0000000..5ff625f --- /dev/null +++ b/init/api-server.socket @@ -0,0 +1,7 @@ +[Unit] +Description=API Server socket + +[Socket] +ListenStream=10080 +NoDelay=true + diff --git a/internal/app/server.go b/internal/app/server.go index 458bf8b..ae017e2 100644 --- a/internal/app/server.go +++ b/internal/app/server.go @@ -5,7 +5,10 @@ import ( "encoding/json" "io/ioutil" "log" + "net" "net/http" + "os" + "strconv" "time" def "git.pbiernat.dev/golang/rest-api-prototype/internal/app/definition" @@ -30,8 +33,18 @@ func NewServer(env *handler.Env) *Server { func (s *Server) Start() { log.Println("Server listening on " + s.Addr) - if err := s.ListenAndServe(); err != nil { - log.Println(err) + + if os.Getenv("LISTEN_PID") == strconv.Itoa(os.Getpid()) { + // systemd run + f := os.NewFile(3, "from systemd") + l, err := net.FileListener(f) + if err != nil { + log.Fatalln(err) + } + http.Serve(l, nil) + } else { + // manual run + log.Fatalln(s.ListenAndServe()) } }