Skip to content

Commit

Permalink
addressed more pr review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
devang-gaur committed May 31, 2021
1 parent 5d69aa5 commit 664c960
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 99 deletions.
26 changes: 22 additions & 4 deletions pkg/http-server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
httputils "github.com/accurics/terrascan/pkg/utils/http"
gorillaHandlers "github.com/gorilla/handlers"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (g *APIServer) start(routes []*Route, port, certFile, privateKeyFile string
router = mux.NewRouter() // new router
)

logWriter := httputils.GetLogWriter(logger)
logWriter := getLogWriter(logger)

logger.Info("registering routes...")

Expand All @@ -71,12 +72,12 @@ func (g *APIServer) start(routes []*Route, port, certFile, privateKeyFile string
// register all routes
for _, v := range routes {
logger.Info("Route ", v.verb, " - ", v.path)
handler := gorillaHandlers.CustomLoggingHandler(logWriter, http.HandlerFunc(v.fn), httputils.WriteRequestLog)
handler := gorillaHandlers.LoggingHandler(logWriter, http.HandlerFunc(v.fn))
router.Methods(v.verb).Path(v.path).Handler(handler)
}

router.NotFoundHandler = gorillaHandlers.CustomLoggingHandler(logWriter, http.HandlerFunc(httputils.NotFound), httputils.WriteRequestLog)
router.MethodNotAllowedHandler = gorillaHandlers.CustomLoggingHandler(logWriter, http.HandlerFunc(httputils.NotAllowed), httputils.WriteRequestLog)
router.NotFoundHandler = gorillaHandlers.LoggingHandler(logWriter, http.HandlerFunc(httputils.NotFound))
router.MethodNotAllowedHandler = gorillaHandlers.LoggingHandler(logWriter, http.HandlerFunc(httputils.NotAllowed))

// Add a route for all static templates / assets. Currently used for the Webhook logs views
// go/terrascan/asset is the path where the assets files are located inside the docker container
Expand Down Expand Up @@ -122,3 +123,20 @@ func (g *APIServer) start(routes []*Route, port, certFile, privateKeyFile string
}
logger.Info("server exiting gracefully")
}

type LogWriter struct {
logger *zap.SugaredLogger
}

// GetLogWriter creates and fetches a LogWriter object
func getLogWriter(logger *zap.SugaredLogger) LogWriter {
return LogWriter{
logger: logger,
}
}

// Write writes the byte array to the logger object
func (l LogWriter) Write(p []byte) (n int, err error) {
l.logger.Info(string(p))
return len(p), nil
}
95 changes: 0 additions & 95 deletions pkg/utils/http/logwriter.go

This file was deleted.

0 comments on commit 664c960

Please sign in to comment.