Skip to content

Instantly share code, notes, and snippets.

@chipaca
Created April 17, 2018 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chipaca/b1861183bbcda2f2d4e14ab3f74e60b2 to your computer and use it in GitHub Desktop.
Save chipaca/b1861183bbcda2f2d4e14ab3f74e60b2 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/coreos/go-systemd/activation"
"context"
"fmt"
"net/http"
"os"
"sync/atomic"
)
var stopCh = make(chan struct{})
var n uint64
var pid = os.Getpid()
func HelloServer(w http.ResponseWriter, req *http.Request) {
m := atomic.AddUint64(&n, 1)
fmt.Fprintln(w, pid, m, req.URL.Path)
fmt.Println(pid, m, req.URL.Path)
if m == 1 {
close(stopCh)
}
}
func main() {
listeners, err := activation.Listeners(true)
if err != nil {
panic(err)
}
if len(listeners) != 1 {
panic("wrong # of listeners")
}
http.HandleFunc("/", HelloServer)
server := &http.Server{}
go server.Serve(listeners[0])
<-stopCh
server.Shutdown(context.TODO())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment