From 3f41d328a37b5bae22d58f610c97cf5c45aa4a11 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 3 Feb 2021 13:24:42 +1300 Subject: Switch listen to systemd socket activation --- .build.yml | 26 -------------------------- Makefile | 2 +- README.md | 6 ++++++ cmd/example/example.go | 2 +- go.mod | 7 +++++-- go.sum | 3 +++ server.go | 19 ++++++++++--------- 7 files changed, 26 insertions(+), 39 deletions(-) delete mode 100644 .build.yml diff --git a/.build.yml b/.build.yml deleted file mode 100644 index 0b1a1ff..0000000 --- a/.build.yml +++ /dev/null @@ -1,26 +0,0 @@ -image: fedora/latest -packages: - - go -sources: - - https://git.sr.ht/~yotam/go-gemini - - https://git.sr.ht/~sircmpwn/annotatego -secrets: - - f43da1db-697d-4101-8607-e96f574b035a -tasks: -- lint: | - cd go-gemini - go vet -- test: | - cd go-gemini - go test -- build: | - cd go-gemini - make -- annotatego: | - cd annotatego - go build - sudo cp annotatego /usr/bin/ -- annotate: | - cd go-gemini - annotatego -v git.sr.ht/~yotam/go-gemini... >annotations.json - ~/upload-annotations annotations.json yotam go-gemini diff --git a/Makefile b/Makefile index ca8bd62..5888a58 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ all: build build: gemini-example gemini-example: cmd/example/*.go *.go - go build -o gemini-example git.sr.ht/~yotam/go-gemini/cmd/example + go build -o gemini-example sanctum.geek.nz/code/go-gemini.git/cmd/example clean: rm -rf gemini-example diff --git a/README.md b/README.md index 9a3db3e..fa26129 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,9 @@ The repository comes with an example server that respond with an hardcoded text to the root page. To build the server run the following command: make build + +Fork +---- + +This is a fork by Tom Ryder that replaces the default +listening socket with systemd activation. diff --git a/cmd/example/example.go b/cmd/example/example.go index 6e7ece2..49d3090 100644 --- a/cmd/example/example.go +++ b/cmd/example/example.go @@ -6,7 +6,7 @@ import ( "net/url" "strings" - gemini "git.sr.ht/~yotam/go-gemini" + gemini "sanctum.geek.nz/code/go-gemini.git" ) type ExampleHandler struct { diff --git a/go.mod b/go.mod index 4afecd0..ade4ee4 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,8 @@ -module git.sr.ht/~yotam/go-gemini +module sanctum.geek.nz/code/go-gemini.git.git go 1.12 -require github.com/google/go-cmp v0.3.1 +require ( + github.com/coreos/go-systemd/v22 v22.1.0 + github.com/google/go-cmp v0.3.1 +) diff --git a/go.sum b/go.sum index a6ddb1d..c4748d9 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,5 @@ +github.com/coreos/go-systemd/v22 v22.1.0 h1:kq/SbG2BCKLkDKkjQf5OWwKWUKj1lgs3lFI4PxnR5lg= +github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk= +github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= diff --git a/server.go b/server.go index 49bccb5..34d8070 100644 --- a/server.go +++ b/server.go @@ -4,6 +4,7 @@ import ( "bufio" "crypto/tls" "fmt" + "github.com/coreos/go-systemd/v22/activation" "io" "net" "strings" @@ -23,15 +24,18 @@ type Handler interface { // new connections to the given handler. // Each request is handled in a separate goroutine. func ListenAndServe(addr, certFile, keyFile string, handler Handler) error { - if addr == "" { - addr = "127.0.0.1:1965" + listeners, err := activation.Listeners() + if err != nil { + return err + } + if len(listeners) != 1 { + return err } - listener, err := listen(addr, certFile, keyFile) + listener, err := listen(listeners[0], certFile, keyFile) if err != nil { return err } - err = serve(listener, handler) if err != nil { return err @@ -45,17 +49,14 @@ func ListenAndServe(addr, certFile, keyFile string, handler Handler) error { return nil } -func listen(addr, certFile, keyFile string) (net.Listener, error) { +func listen(listener net.Listener, certFile, keyFile string) (net.Listener, error) { cer, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { return nil, fmt.Errorf("failed to load certificates: %v", err) } config := &tls.Config{Certificates: []tls.Certificate{cer}} - ln, err := tls.Listen("tcp", addr, config) - if err != nil { - return nil, fmt.Errorf("failed to listen: %v", err) - } + ln := tls.NewListener(listener, config) return ln, nil } -- cgit v1.2.3