[feature] Added systemd support
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Piotr Biernat 2022-04-23 21:11:04 +02:00
parent ceb3c4bba5
commit 41292b25e3
4 changed files with 40 additions and 3 deletions

View File

@ -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

17
init/api-server.service Normal file
View File

@ -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

7
init/api-server.socket Normal file
View File

@ -0,0 +1,7 @@
[Unit]
Description=API Server socket
[Socket]
ListenStream=10080
NoDelay=true

View File

@ -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())
}
}