mirror of
https://github.com/restic/rest-server.git
synced 2025-12-07 09:36:13 -08:00
Add support for logging HTTP requests in the combined log format
This commit is contained in:
22
main.go
22
main.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user