Skip to content

Instantly share code, notes, and snippets.

@robertgzr
Last active April 16, 2018 12:24
Show Gist options
  • Select an option

  • Save robertgzr/c1d5f38baa17e21977240d44c91711ff to your computer and use it in GitHub Desktop.

Select an option

Save robertgzr/c1d5f38baa17e21977240d44c91711ff to your computer and use it in GitHub Desktop.
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