Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/src-d/code-annotation/server/serializer"
"github.com/src-d/code-annotation/server/service"

"github.com/sirupsen/logrus"
"github.com/pressly/lg"
)

// Login handler redirects user to oauth provider
Expand All @@ -26,9 +26,10 @@ func OAuthCallback(
jwt *service.JWT,
userRepo *repository.Users,
uiDomain string,
logger logrus.FieldLogger,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := lg.RequestLog(r)

if err := oAuth.ValidateState(r, r.FormValue("state")); err != nil {
errorText := "The state passed is incorrect or expired"
write(
Expand Down
85 changes: 85 additions & 0 deletions server/handler/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package handler

import (
"fmt"
"net/http"
"strconv"

"github.com/src-d/code-annotation/server/serializer"

"github.com/pressly/lg"
)

// Levels
const (
Fatal int = iota
ManualPanic
Panic
Error
Warning
Info
Debug
Unk
)

// Log testing handler
func Log(level int) RequestProcessFunc {
return func(r *http.Request) (*serializer.Response, error) {
logger := lg.RequestLog(r)
switch level {
case Fatal:
logger.Fatal("Fatal logged. App will exit")
logger.Error("This message should not appear after a fatal message")
case ManualPanic:
panic("Panic manually thrown")
case Panic:
logger.Panic("Panic logged")
logger.Error("This message should not appear after a panic message")
case Error:
return nil, fmt.Errorf("Error sent")
case Warning:
logger.Warn("Warn logged")
case Info:
logger.Info("Info logged")
case Debug:
logger.Debug("Debug logged")
default:
logger.Error("unknown requested level")
}

logger.Info("Message sent")

return serializer.NewVersionResponse(strconv.Itoa(level)), nil
}
}

// LoggingTestMiddleware testing middleware
func LoggingTestMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := lg.RequestLog(r)
testValue := r.FormValue("test-middleware")

switch testValue {
case "fatal":
logger.Fatal("LoggingTestMiddleware killed the app")
logger.Error("This message should not appear after a fatal message")
case "manual-panic":
panic("LoggingTestMiddleware throwed a manual panic")
case "panic":
logger.Panic("LoggingTestMiddleware throwed a panic")
logger.Error("This message should not appear after a panic message")
case "error":
logger.Error("LoggingTestMiddleware logged an error message")
w.WriteHeader(http.StatusInternalServerError)
return
case "warn":
logger.Info("LoggingTestMiddleware logged an warning message")
case "info":
logger.Info("LoggingTestMiddleware logged an info message")
case "debug":
logger.Info("LoggingTestMiddleware logged an debug message")
}

next.ServeHTTP(w, r)
})
}
17 changes: 15 additions & 2 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func Router(
r := chi.NewRouter()

r.Use(middleware.Recoverer)
r.Use(cors.New(corsOptions).Handler)
r.Use(lg.RequestLogger(logger))
r.Use(cors.New(corsOptions).Handler)
r.Use(handler.LoggingTestMiddleware)

r.Get("/login", handler.Login(oauth))
r.Get("/oauth-callback", handler.OAuthCallback(oauth, jwt, userRepo, uiDomain, logger))
r.Get("/oauth-callback", handler.OAuthCallback(oauth, jwt, userRepo, uiDomain))

r.Route("/api", func(r chi.Router) {
r.Use(jwt.Middleware)
Expand Down Expand Up @@ -106,6 +107,18 @@ func Router(

r.Get("/version", handler.APIHandlerFunc(handler.Version(version)))

/*
* Testing logs
*/
r.Get("/destroy", handler.Get(handler.Log(handler.Fatal)))
r.Get("/manual-panic", handler.Get(handler.Log(handler.ManualPanic)))
r.Get("/panic", handler.Get(handler.Log(handler.Panic)))
r.Get("/error", handler.Get(handler.Log(handler.Error)))
r.Get("/warn", handler.Get(handler.Log(handler.Warning)))
r.Get("/info", handler.Get(handler.Log(handler.Info)))
r.Get("/debug", handler.Get(handler.Log(handler.Debug)))
r.Get("/unk", handler.Get(handler.Log(handler.Unk)))

r.Get("/static/*", static.ServeHTTP)
r.Get("/*", static.ServeHTTP)

Expand Down