mirror of https://github.com/jlelse/GoBlog
Log to file in combined format
This commit is contained in:
parent
11c4467caa
commit
1e7dd17f1a
|
@ -23,6 +23,7 @@ type config struct {
|
||||||
|
|
||||||
type configServer struct {
|
type configServer struct {
|
||||||
Logging bool `mapstructure:"logging"`
|
Logging bool `mapstructure:"logging"`
|
||||||
|
LogFile string `mapstructure:"logFile"`
|
||||||
Debug bool `mapstructure:"Debug"`
|
Debug bool `mapstructure:"Debug"`
|
||||||
Port int `mapstructure:"port"`
|
Port int `mapstructure:"port"`
|
||||||
Domain string `mapstructure:"domain"`
|
Domain string `mapstructure:"domain"`
|
||||||
|
@ -165,6 +166,7 @@ func initConfig() error {
|
||||||
}
|
}
|
||||||
// Defaults
|
// Defaults
|
||||||
viper.SetDefault("server.logging", false)
|
viper.SetDefault("server.logging", false)
|
||||||
|
viper.SetDefault("server.logFile", "data/access.log")
|
||||||
viper.SetDefault("server.debug", false)
|
viper.SetDefault("server.debug", false)
|
||||||
viper.SetDefault("server.port", 8080)
|
viper.SetDefault("server.port", 8080)
|
||||||
viper.SetDefault("server.publicAddress", "http://localhost:8080")
|
viper.SetDefault("server.publicAddress", "http://localhost:8080")
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -12,6 +12,7 @@ require (
|
||||||
github.com/goodsign/monday v1.0.1-0.20201007115131-c065b60ec611
|
github.com/goodsign/monday v1.0.1-0.20201007115131-c065b60ec611
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
|
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
|
||||||
github.com/gorilla/feeds v1.1.1
|
github.com/gorilla/feeds v1.1.1
|
||||||
|
github.com/gorilla/handlers v1.5.1
|
||||||
github.com/jeremywohl/flatten v1.0.1
|
github.com/jeremywohl/flatten v1.0.1
|
||||||
github.com/jinzhu/gorm v1.9.16 // indirect
|
github.com/jinzhu/gorm v1.9.16 // indirect
|
||||||
github.com/json-iterator/go v1.1.10
|
github.com/json-iterator/go v1.1.10
|
||||||
|
|
4
go.sum
4
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 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
|
||||||
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
|
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/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 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
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/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 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
|
||||||
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
|
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/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-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=
|
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
|
||||||
|
|
38
http.go
38
http.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"compress/flate"
|
"compress/flate"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -10,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/go-chi/chi/middleware"
|
"github.com/go-chi/chi/middleware"
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,10 +30,32 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
d *dynamicHandler
|
d *dynamicHandler
|
||||||
|
logMiddleware func(next http.Handler) http.Handler
|
||||||
|
authMiddleware func(next http.Handler) http.Handler
|
||||||
)
|
)
|
||||||
|
|
||||||
func startServer() (err error) {
|
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{}
|
d = &dynamicHandler{}
|
||||||
err = reloadRouter()
|
err = reloadRouter()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -79,14 +101,10 @@ func reloadRouter() error {
|
||||||
func buildHandler() (http.Handler, error) {
|
func buildHandler() (http.Handler, error) {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
r.Use(middleware.Recoverer)
|
|
||||||
if appConfig.Server.Logging {
|
if appConfig.Server.Logging {
|
||||||
r.Use(middleware.RealIP)
|
r.Use(logMiddleware)
|
||||||
r.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{
|
|
||||||
Logger: log.New(os.Stdout, "", log.LstdFlags),
|
|
||||||
NoColor: true,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
r.Use(middleware.Recoverer)
|
||||||
r.Use(middleware.Compress(flate.DefaultCompression))
|
r.Use(middleware.Compress(flate.DefaultCompression))
|
||||||
r.Use(middleware.StripSlashes)
|
r.Use(middleware.StripSlashes)
|
||||||
r.Use(middleware.GetHead)
|
r.Use(middleware.GetHead)
|
||||||
|
@ -99,10 +117,6 @@ func buildHandler() (http.Handler, error) {
|
||||||
r.Mount("/debug", middleware.Profiler())
|
r.Mount("/debug", middleware.Profiler())
|
||||||
}
|
}
|
||||||
|
|
||||||
authMiddleware := middleware.BasicAuth("API", map[string]string{
|
|
||||||
appConfig.User.Nick: appConfig.User.Password,
|
|
||||||
})
|
|
||||||
|
|
||||||
// API
|
// API
|
||||||
r.Route("/api", func(apiRouter chi.Router) {
|
r.Route("/api", func(apiRouter chi.Router) {
|
||||||
apiRouter.Use(middleware.NoCache, authMiddleware)
|
apiRouter.Use(middleware.NoCache, authMiddleware)
|
||||||
|
|
Loading…
Reference in New Issue