From 58d77510b88b1e562831c1fe75d98b72e2c7e8d4 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Fri, 10 Nov 2017 00:30:07 +0100 Subject: [PATCH] - fixed not working debug mode - integrated logrus --- config/config.go | 7 ++++--- handlers/auth.go | 3 +-- handlers/handlers.go | 22 ++++++++++++++-------- main.go | 15 +++++++++++---- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/config/config.go b/config/config.go index 2ba8826..554a6ce 100644 --- a/config/config.go +++ b/config/config.go @@ -37,9 +37,10 @@ type Handlers struct { } `description:"OAuth holds the OAuth specific settings"` } -var config *Configuration - -var configPath string +var ( + config *Configuration + configPath string +) // Get returns the configuration from a given file func Get() *Configuration { diff --git a/handlers/auth.go b/handlers/auth.go index 0b9d29b..3d390fa 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "time" @@ -91,7 +90,7 @@ func (h *Handler) authMiddleware(c *gin.Context) { c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ "error": fmt.Sprintf("token is not valid: %v", authError), }) - log.Printf("Authentication middleware failed: %v\n", authError) + h.log.Debugf("Authentication middleware failed: %v\n", authError) } else { c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ "error": "authentication failed", diff --git a/handlers/handlers.go b/handlers/handlers.go index 1572526..3584ac9 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -5,12 +5,16 @@ import ( "crypto/rand" "html/template" "net/http" + "time" + + "github.com/gin-gonic/contrib/ginrus" "github.com/gin-gonic/gin" "github.com/maxibanki/golang-url-shortener/config" "github.com/maxibanki/golang-url-shortener/handlers/tmpls" "github.com/maxibanki/golang-url-shortener/store" "github.com/pkg/errors" + "github.com/sirupsen/logrus" "golang.org/x/oauth2" ) @@ -21,15 +25,20 @@ type Handler struct { store store.Store engine *gin.Engine oAuthConf *oauth2.Config + log *logrus.Logger DoNotCheckConfigViaGet bool // DoNotCheckConfigViaGet is for the unit testing usage } // New initializes the http handlers -func New(handlerConfig config.Handlers, store store.Store) (*Handler, error) { +func New(handlerConfig config.Handlers, store store.Store, log *logrus.Logger) (*Handler, error) { + if !handlerConfig.EnableDebugMode { + gin.SetMode(gin.ReleaseMode) + } h := &Handler{ config: handlerConfig, store: store, - engine: gin.Default(), + log: log, + engine: gin.New(), } if err := h.setHandlers(); err != nil { return nil, errors.Wrap(err, "could not set handlers") @@ -72,19 +81,16 @@ func (h *Handler) checkIfSecretExist() error { } func (h *Handler) setHandlers() error { - if !h.config.EnableDebugMode { - gin.SetMode(gin.ReleaseMode) + if err := h.setTemplateFromFS("token.tmpl"); err != nil { + return errors.Wrap(err, "could not set template from FS") } + h.engine.Use(ginrus.Ginrus(h.log, time.RFC3339, false)) protected := h.engine.Group("/api/v1/protected") protected.Use(h.authMiddleware) protected.POST("/create", h.handleCreate) protected.POST("/info", h.handleInfo) h.engine.NoRoute(h.handleAccess, gin.WrapH(http.FileServer(FS(false)))) - - if err := h.setTemplateFromFS("token.tmpl"); err != nil { - return errors.Wrap(err, "could not set template from FS") - } return nil } diff --git a/main.go b/main.go index 518eb70..3b2d0a4 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( - "log" "os" "os/signal" + "github.com/shiena/ansicolor" + "github.com/sirupsen/logrus" + "github.com/maxibanki/golang-url-shortener/config" "github.com/maxibanki/golang-url-shortener/handlers" "github.com/maxibanki/golang-url-shortener/store" @@ -14,7 +16,12 @@ import ( func main() { stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt) - close, err := initShortener() + log := logrus.New() + log.Formatter = &logrus.TextFormatter{ + ForceColors: true, + } + log.Out = ansicolor.NewAnsiColorWriter(os.Stdout) + close, err := initShortener(log) if err != nil { log.Fatalf("could not init shortener: %v", err) } @@ -23,7 +30,7 @@ func main() { close() } -func initShortener() (func(), error) { +func initShortener(log *logrus.Logger) (func(), error) { if err := config.Preload(); err != nil { return nil, errors.Wrap(err, "could not get config") } @@ -32,7 +39,7 @@ func initShortener() (func(), error) { if err != nil { return nil, errors.Wrap(err, "could not create store") } - handler, err := handlers.New(conf.Handlers, *store) + handler, err := handlers.New(conf.Handlers, *store, log) if err != nil { return nil, errors.Wrap(err, "could not create handlers") }