Add support for logging HTTP requests in the combined log format

This commit is contained in:
Zlatko Čalušić
2017-05-31 23:29:21 +02:00
parent ed59c2ec28
commit c7ffc41a65
22 changed files with 2489 additions and 4 deletions

22
main.go
View File

@@ -8,6 +8,7 @@ import (
"runtime"
"runtime/pprof"
"github.com/gorilla/handlers"
"github.com/spf13/cobra"
"goji.io"
"goji.io/pat"
@@ -26,17 +27,19 @@ var config = struct {
path string
listen string
tls bool
log string
cpuprofile string
debug bool
}{}
func init() {
flags := cmdRoot.Flags()
flags.StringVar(&config.path, "path", "/tmp/restic", "data directory")
flags.StringVar(&config.listen, "listen", ":8000", "listen address")
flags.BoolVar(&config.tls, "tls", false, "turn on TLS support")
flags.StringVar(&config.cpuprofile, "cpuprofile", "", "write CPU profile to file")
flags.BoolVar(&config.debug, "debug", false, "output debug messages")
flags.StringVar(&config.listen, "listen", ":8000", "listen address")
flags.StringVar(&config.log, "log", "", "log HTTP requests in the combined log format")
flags.StringVar(&config.path, "path", "/tmp/restic", "data directory")
flags.BoolVar(&config.tls, "tls", false, "turn on TLS support")
}
func debugHandler(next http.Handler) http.Handler {
@@ -47,6 +50,15 @@ func debugHandler(next http.Handler) http.Handler {
})
}
func logHandler(next http.Handler) http.Handler {
accessLog, err := os.OpenFile(config.log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
return handlers.CombinedLoggingHandler(accessLog, next)
}
func setupMux() *goji.Mux {
mux := goji.NewMux()
@@ -54,6 +66,10 @@ func setupMux() *goji.Mux {
mux.Use(debugHandler)
}
if config.log != "" {
mux.Use(logHandler)
}
mux.HandleFunc(pat.Head("/config"), CheckConfig)
mux.HandleFunc(pat.Head("/:repo/config"), CheckConfig)
mux.HandleFunc(pat.Get("/config"), GetConfig)