Browse Source

- fixed not working debug mode

- integrated logrus
dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
58d77510b8
  1. 7
      config/config.go
  2. 3
      handlers/auth.go
  3. 22
      handlers/handlers.go
  4. 15
      main.go

7
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 {

3
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",

22
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
}

15
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")
}

Loading…
Cancel
Save