Add logger to webserver
parent
b7cf249bfb
commit
e05751d269
|
@ -1,16 +1,20 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/ollien/updown/handler/paste"
|
"github.com/ollien/updown/handler/paste"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Webserver is the main websever
|
// Webserver is the main websever
|
||||||
type Webserver struct {
|
type Webserver struct {
|
||||||
httpServer *http.Server
|
httpServer *http.Server
|
||||||
router chi.Router
|
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
|
pasteService *paste.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +28,7 @@ func NewWebserver() *Webserver {
|
||||||
|
|
||||||
// Start starts the webserver
|
// Start starts the webserver
|
||||||
func (server *Webserver) Start() error {
|
func (server *Webserver) Start() error {
|
||||||
|
defer server.logWriter.Close()
|
||||||
return server.httpServer.ListenAndServe()
|
return server.httpServer.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,11 @@ package web
|
||||||
import (
|
import (
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/ollien/updown/config"
|
"github.com/ollien/updown/config"
|
||||||
|
"github.com/ollien/updown/handler/log"
|
||||||
"github.com/ollien/updown/handler/paste"
|
"github.com/ollien/updown/handler/paste"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"io"
|
||||||
|
log2 "log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
@ -18,25 +22,30 @@ import (
|
||||||
func initWebserver() *Webserver {
|
func initWebserver() *Webserver {
|
||||||
configConfig := config.Get()
|
configConfig := config.Get()
|
||||||
router := provideRouter()
|
router := provideRouter()
|
||||||
server := provideHTTPServer(configConfig, router)
|
logger := log.Get()
|
||||||
|
pipeWriter := provideLogWriter(logger)
|
||||||
|
server := provideHTTPServer(configConfig, router, pipeWriter)
|
||||||
service := paste.NewService()
|
service := paste.NewService()
|
||||||
webserver := provideWebserver(server, router, service)
|
webserver := provideWebserver(server, router, service, logger, pipeWriter)
|
||||||
return webserver
|
return webserver
|
||||||
}
|
}
|
||||||
|
|
||||||
// wire_stubs.go:
|
// 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{
|
return &http.Server{
|
||||||
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
|
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
|
||||||
Handler: router,
|
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{
|
webserver := Webserver{
|
||||||
httpServer: httpServer,
|
httpServer: httpServer,
|
||||||
router: router,
|
router: router,
|
||||||
|
logger: logger,
|
||||||
|
logWriter: logWriter,
|
||||||
pasteService: pasteService,
|
pasteService: pasteService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,3 +56,7 @@ func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *
|
||||||
func provideRouter() chi.Router {
|
func provideRouter() chi.Router {
|
||||||
return chi.NewRouter()
|
return chi.NewRouter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func provideLogWriter(logger *logrus.Logger) *io.PipeWriter {
|
||||||
|
return logger.Writer()
|
||||||
|
}
|
||||||
|
|
|
@ -3,32 +3,39 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/google/wire"
|
"github.com/google/wire"
|
||||||
"github.com/ollien/updown/config"
|
"github.com/ollien/updown/config"
|
||||||
|
logHandler "github.com/ollien/updown/handler/log"
|
||||||
"github.com/ollien/updown/handler/paste"
|
"github.com/ollien/updown/handler/paste"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// initWebserver is generated by wire. Please see wire_gen.go for the generated implementation
|
// initWebserver is generated by wire. Please see wire_gen.go for the generated implementation
|
||||||
func initWebserver() *Webserver {
|
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{}
|
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{
|
return &http.Server{
|
||||||
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
|
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
|
||||||
Handler: router,
|
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{
|
webserver := Webserver{
|
||||||
httpServer: httpServer,
|
httpServer: httpServer,
|
||||||
router: router,
|
router: router,
|
||||||
|
logger: logger,
|
||||||
|
logWriter: logWriter,
|
||||||
pasteService: pasteService,
|
pasteService: pasteService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,3 +46,7 @@ func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *
|
||||||
func provideRouter() chi.Router {
|
func provideRouter() chi.Router {
|
||||||
return chi.NewRouter()
|
return chi.NewRouter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func provideLogWriter(logger *logrus.Logger) *io.PipeWriter {
|
||||||
|
return logger.Writer()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue