Add logger to webserver

master
Nick Krichevsky 2019-03-10 15:08:08 -04:00
parent b7cf249bfb
commit e05751d269
3 changed files with 40 additions and 11 deletions

View File

@ -1,16 +1,20 @@
package web
import (
"io"
"net/http"
"github.com/go-chi/chi"
"github.com/ollien/updown/handler/paste"
"github.com/sirupsen/logrus"
)
// Webserver is the main websever
type Webserver struct {
httpServer *http.Server
router chi.Router
logger *logrus.Logger
logWriter *io.PipeWriter // used for httpServer.ErrorLog. Must be stored separately so we can close it.
pasteService *paste.Service
}
@ -24,6 +28,7 @@ func NewWebserver() *Webserver {
// Start starts the webserver
func (server *Webserver) Start() error {
defer server.logWriter.Close()
return server.httpServer.ListenAndServe()
}

View File

@ -8,7 +8,11 @@ package web
import (
"github.com/go-chi/chi"
"github.com/ollien/updown/config"
"github.com/ollien/updown/handler/log"
"github.com/ollien/updown/handler/paste"
"github.com/sirupsen/logrus"
"io"
log2 "log"
"net/http"
"strconv"
)
@ -18,25 +22,30 @@ import (
func initWebserver() *Webserver {
configConfig := config.Get()
router := provideRouter()
server := provideHTTPServer(configConfig, router)
logger := log.Get()
pipeWriter := provideLogWriter(logger)
server := provideHTTPServer(configConfig, router, pipeWriter)
service := paste.NewService()
webserver := provideWebserver(server, router, service)
webserver := provideWebserver(server, router, service, logger, pipeWriter)
return webserver
}
// wire_stubs.go:
func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server {
func provideHTTPServer(appConfig config.Config, router chi.Router, logWriter *io.PipeWriter) *http.Server {
return &http.Server{
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
Handler: router,
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
Handler: router,
ErrorLog: log2.New(logWriter, "", 0),
}
}
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,
logger: logger,
logWriter: logWriter,
pasteService: pasteService,
}
@ -47,3 +56,7 @@ func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *
func provideRouter() chi.Router {
return chi.NewRouter()
}
func provideLogWriter(logger *logrus.Logger) *io.PipeWriter {
return logger.Writer()
}

View File

@ -3,32 +3,39 @@
package web
import (
"io"
"log"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/google/wire"
"github.com/ollien/updown/config"
logHandler "github.com/ollien/updown/handler/log"
"github.com/ollien/updown/handler/paste"
"github.com/sirupsen/logrus"
)
// initWebserver is generated by wire. Please see wire_gen.go for the generated implementation
func initWebserver() *Webserver {
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, paste.NewService)
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, paste.NewService, logHandler.Get, provideLogWriter)
return &Webserver{}
}
func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server {
func provideHTTPServer(appConfig config.Config, router chi.Router, logWriter *io.PipeWriter) *http.Server {
return &http.Server{
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
Handler: router,
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
Handler: router,
ErrorLog: log.New(logWriter, "", 0),
}
}
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,
logger: logger,
logWriter: logWriter,
pasteService: pasteService,
}
@ -39,3 +46,7 @@ func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *
func provideRouter() chi.Router {
return chi.NewRouter()
}
func provideLogWriter(logger *logrus.Logger) *io.PipeWriter {
return logger.Writer()
}