Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created November 7, 2016 18:59
Show Gist options
  • Save gjohnson/9d7b778379fbb3ba44cc2045d3eb3fa2 to your computer and use it in GitHub Desktop.
Save gjohnson/9d7b778379fbb3ba44cc2045d3eb3fa2 to your computer and use it in GitHub Desktop.
package main
import (
_ "context"
"flag"
"fmt"
"github.com/go-kit/kit/log"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"syscall"
)
func main() {
var (
debugAddr = flag.String("debug.addr", ":3000", "Debug and metrics listen address")
httpAddr = flag.String("http.addr", ":8080", "HTTP listen address")
)
flag.Parse()
// Logging.
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
}
// Mechanical.
errc := make(chan error)
//ctx := context.Background()
// Interrupt.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
// Debug.
go func() {
logger := log.NewContext(logger).With("transport", "debug")
m := http.NewServeMux()
m.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
m.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
m.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
m.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
m.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
logger.Log("addr", *debugAddr)
errc <- http.ListenAndServe(*debugAddr, m)
}()
// App.
go func() {
logger := log.NewContext(logger).With("transport", "http")
m := http.NewServeMux()
logger.Log("addr", *httpAddr)
errc <- http.ListenAndServe(*httpAddr, m)
}()
// Run.
logger.Log("exit", <-errc)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment