From 1e7dd17f1ab38a76fbd7b59a8a65c405b3b12663 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 3 Nov 2020 18:58:32 +0100 Subject: [PATCH] Log to file in combined format --- config.go | 2 ++ go.mod | 1 + go.sum | 4 ++++ http.go | 38 ++++++++++++++++++++++++++------------ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index cb9b3c2..5a2b415 100644 --- a/config.go +++ b/config.go @@ -23,6 +23,7 @@ type config struct { type configServer struct { Logging bool `mapstructure:"logging"` + LogFile string `mapstructure:"logFile"` Debug bool `mapstructure:"Debug"` Port int `mapstructure:"port"` Domain string `mapstructure:"domain"` @@ -165,6 +166,7 @@ func initConfig() error { } // Defaults viper.SetDefault("server.logging", false) + viper.SetDefault("server.logFile", "data/access.log") viper.SetDefault("server.debug", false) viper.SetDefault("server.port", 8080) viper.SetDefault("server.publicAddress", "http://localhost:8080") diff --git a/go.mod b/go.mod index abd2c79..fa1beda 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/goodsign/monday v1.0.1-0.20201007115131-c065b60ec611 github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect github.com/gorilla/feeds v1.1.1 + github.com/gorilla/handlers v1.5.1 github.com/jeremywohl/flatten v1.0.1 github.com/jinzhu/gorm v1.9.16 // indirect github.com/json-iterator/go v1.1.10 diff --git a/go.sum b/go.sum index 3797872..9e5c032 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= +github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= @@ -105,6 +107,8 @@ github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3 github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY= github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA= +github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= +github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= diff --git a/http.go b/http.go index f8cf1b3..e3c43e4 100644 --- a/http.go +++ b/http.go @@ -2,7 +2,6 @@ package main import ( "compress/flate" - "log" "net/http" "os" "strconv" @@ -10,6 +9,7 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" + "github.com/gorilla/handlers" "golang.org/x/crypto/acme/autocert" ) @@ -30,10 +30,32 @@ const ( ) var ( - d *dynamicHandler + d *dynamicHandler + logMiddleware func(next http.Handler) http.Handler + authMiddleware func(next http.Handler) http.Handler ) func startServer() (err error) { + // Init + if appConfig.Server.Logging { + f, err := os.OpenFile(appConfig.Server.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + return err + } + defer f.Close() + logMiddleware = func(next http.Handler) http.Handler { + lh := handlers.CombinedLoggingHandler(f, next) + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + // Remove remote address for privacy + r.RemoteAddr = "127.0.0.1" + lh.ServeHTTP(rw, r) + }) + } + } + authMiddleware = middleware.BasicAuth("", map[string]string{ + appConfig.User.Nick: appConfig.User.Password, + }) + // Start d = &dynamicHandler{} err = reloadRouter() if err != nil { @@ -79,14 +101,10 @@ func reloadRouter() error { func buildHandler() (http.Handler, error) { r := chi.NewRouter() - r.Use(middleware.Recoverer) if appConfig.Server.Logging { - r.Use(middleware.RealIP) - r.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{ - Logger: log.New(os.Stdout, "", log.LstdFlags), - NoColor: true, - })) + r.Use(logMiddleware) } + r.Use(middleware.Recoverer) r.Use(middleware.Compress(flate.DefaultCompression)) r.Use(middleware.StripSlashes) r.Use(middleware.GetHead) @@ -99,10 +117,6 @@ func buildHandler() (http.Handler, error) { r.Mount("/debug", middleware.Profiler()) } - authMiddleware := middleware.BasicAuth("API", map[string]string{ - appConfig.User.Nick: appConfig.User.Password, - }) - // API r.Route("/api", func(apiRouter chi.Router) { apiRouter.Use(middleware.NoCache, authMiddleware)