Skip to content

Commit

Permalink
Support running on demand via inetd and systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
fd0 committed Aug 9, 2021
1 parent 96a6f0a commit 7aa41a3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
58 changes: 50 additions & 8 deletions cmd/rest-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/pprof"

"github.com/coreos/go-systemd/activation"
restserver "github.com/restic/rest-server"
"github.com/spf13/cobra"
)
Expand All @@ -31,6 +33,7 @@ var server = restserver.Server{

var (
cpuProfile string
inetd bool
)

func init() {
Expand All @@ -49,6 +52,7 @@ func init() {
flags.BoolVar(&server.PrivateRepos, "private-repos", server.PrivateRepos, "users can only access their private repo")
flags.BoolVar(&server.Prometheus, "prometheus", server.Prometheus, "enable Prometheus metrics")
flags.BoolVar(&server.Prometheus, "prometheus-no-auth", server.PrometheusNoAuth, "disable auth for Prometheus /metrics endpoint")
flags.BoolVar(&inetd, "inetd", false, "start in inetd mode, accepts an already opened socket as file descriptor 0")
}

var version = "0.10.0-dev"
Expand Down Expand Up @@ -111,21 +115,59 @@ func runRoot(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

listener, err := findListener(inetd, server.Listen)
if err != nil {
return fmt.Errorf("unable to listen: %w", err)
}

if !enabledTLS {
log.Printf("Starting server on %s\n", server.Listen)
err = http.ListenAndServe(server.Listen, handler)
err = http.Serve(listener, handler)
} else {

log.Println("TLS enabled")
log.Printf("Private key: %s", privateKey)
log.Printf("Public key(certificate): %s", publicKey)
log.Printf("Starting server on %s\n", server.Listen)
err = http.ListenAndServeTLS(server.Listen, publicKey, privateKey, handler)
log.Printf("TLS enabled, private key %s, pubkey %v", privateKey, publicKey)
err = http.ServeTLS(listener, handler, publicKey, privateKey)
}

return err
}

func findListener(inetd bool, addr string) (listener net.Listener, err error) {
if inetd {
log.Print("inetd mode activated")
return net.FileListener(os.Stdin)
}

// try systemd socket activation
listeners, err := activation.Listeners()
if err != nil {
panic(err)
}

switch len(listeners) {
case 0:
// no listeners found, listen manually
listener, err = net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("listen on %v failed: %w", addr, err)
}

log.Printf("start server on %v", addr)
return listener, nil

case 1:
// one listener supplied by systemd, use that one
//
// for testing, run rest-server with systemd-socket-activate as follows:
//
// systemd-socket-activate -l 8080 ./rest-server
log.Printf("systemd socket activation mode")
return listeners[0], nil

default:
return nil, fmt.Errorf("got %d listeners from systemd, expected one", len(listeners))
}
}

func main() {
if err := cmdRoot.Execute(); err != nil {
log.Fatalf("error: %v", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/restic/rest-server
go 1.14

require (
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/gorilla/handlers v1.5.1
github.com/miolini/datacounter v1.0.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit 7aa41a3

Please sign in to comment.