Last active
April 16, 2018 12:24
-
-
Save robertgzr/c1d5f38baa17e21977240d44c91711ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "net" | |
| "net/http" | |
| "os" | |
| "os/signal" | |
| "path/filepath" | |
| "syscall" | |
| "github.com/apex/log" | |
| "github.com/oklog/run" | |
| ) | |
| const addr string = ":3000" | |
| func main() { | |
| log.SetLevel(log.DebugLevel) | |
| var g run.Group | |
| fs := http.FileServer(http.Dir(filepath.Dir(os.Args[0]))) | |
| { | |
| ln, err := net.Listen("tcp", addr) | |
| if err != nil { | |
| panic(err) | |
| } | |
| log.WithField("address", addr).Info("listening...") | |
| g.Add(func() error { | |
| if err := http.Serve(ln, fs); err != nil { | |
| return err | |
| } | |
| return nil | |
| }, func(error) { | |
| ln.Close() | |
| log.Warn("stopped listening") | |
| }) | |
| } | |
| { | |
| g.Add(func() error { | |
| var sigchan = make(chan os.Signal) | |
| signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT) | |
| for { | |
| select { | |
| case s := <-sigchan: | |
| return fmt.Errorf("received signal %v", s) | |
| } | |
| } | |
| }, func(error) {}) | |
| } | |
| err := g.Run() | |
| log.WithField("cause", err.Error()).Warn("shutting down...") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment