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"` } `description:"OAuth holds the OAuth specific settings"`
} }
var config *Configuration var (
config *Configuration
var configPath string configPath string
)
// Get returns the configuration from a given file // Get returns the configuration from a given file
func Get() *Configuration { func Get() *Configuration {

3
handlers/auth.go

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"time" "time"
@ -91,7 +90,7 @@ func (h *Handler) authMiddleware(c *gin.Context) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": fmt.Sprintf("token is not valid: %v", authError), "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 { } else {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "authentication failed", "error": "authentication failed",

22
handlers/handlers.go

@ -5,12 +5,16 @@ import (
"crypto/rand" "crypto/rand"
"html/template" "html/template"
"net/http" "net/http"
"time"
"github.com/gin-gonic/contrib/ginrus"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/maxibanki/golang-url-shortener/config" "github.com/maxibanki/golang-url-shortener/config"
"github.com/maxibanki/golang-url-shortener/handlers/tmpls" "github.com/maxibanki/golang-url-shortener/handlers/tmpls"
"github.com/maxibanki/golang-url-shortener/store" "github.com/maxibanki/golang-url-shortener/store"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
@ -21,15 +25,20 @@ type Handler struct {
store store.Store store store.Store
engine *gin.Engine engine *gin.Engine
oAuthConf *oauth2.Config oAuthConf *oauth2.Config
log *logrus.Logger
DoNotCheckConfigViaGet bool // DoNotCheckConfigViaGet is for the unit testing usage DoNotCheckConfigViaGet bool // DoNotCheckConfigViaGet is for the unit testing usage
} }
// New initializes the http handlers // 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{ h := &Handler{
config: handlerConfig, config: handlerConfig,
store: store, store: store,
engine: gin.Default(), log: log,
engine: gin.New(),
} }
if err := h.setHandlers(); err != nil { if err := h.setHandlers(); err != nil {
return nil, errors.Wrap(err, "could not set handlers") return nil, errors.Wrap(err, "could not set handlers")
@ -72,19 +81,16 @@ func (h *Handler) checkIfSecretExist() error {
} }
func (h *Handler) setHandlers() error { func (h *Handler) setHandlers() error {
if !h.config.EnableDebugMode { if err := h.setTemplateFromFS("token.tmpl"); err != nil {
gin.SetMode(gin.ReleaseMode) 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 := h.engine.Group("/api/v1/protected")
protected.Use(h.authMiddleware) protected.Use(h.authMiddleware)
protected.POST("/create", h.handleCreate) protected.POST("/create", h.handleCreate)
protected.POST("/info", h.handleInfo) protected.POST("/info", h.handleInfo)
h.engine.NoRoute(h.handleAccess, gin.WrapH(http.FileServer(FS(false)))) 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 return nil
} }

15
main.go

@ -1,10 +1,12 @@
package main package main
import ( import (
"log"
"os" "os"
"os/signal" "os/signal"
"github.com/shiena/ansicolor"
"github.com/sirupsen/logrus"
"github.com/maxibanki/golang-url-shortener/config" "github.com/maxibanki/golang-url-shortener/config"
"github.com/maxibanki/golang-url-shortener/handlers" "github.com/maxibanki/golang-url-shortener/handlers"
"github.com/maxibanki/golang-url-shortener/store" "github.com/maxibanki/golang-url-shortener/store"
@ -14,7 +16,12 @@ import (
func main() { func main() {
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt) 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 { if err != nil {
log.Fatalf("could not init shortener: %v", err) log.Fatalf("could not init shortener: %v", err)
} }
@ -23,7 +30,7 @@ func main() {
close() close()
} }
func initShortener() (func(), error) { func initShortener(log *logrus.Logger) (func(), error) {
if err := config.Preload(); err != nil { if err := config.Preload(); err != nil {
return nil, errors.Wrap(err, "could not get config") return nil, errors.Wrap(err, "could not get config")
} }
@ -32,7 +39,7 @@ func initShortener() (func(), error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not create store") 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 { if err != nil {
return nil, errors.Wrap(err, "could not create handlers") return nil, errors.Wrap(err, "could not create handlers")
} }

Loading…
Cancel
Save