Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BUX-421): replace router logger #377

Merged
merged 3 commits into from
Dec 21, 2023
Merged
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
47 changes: 42 additions & 5 deletions actions/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package actions

import (
"net/http"
"time"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux-server/config"
"github.com/BuxOrg/bux-server/dictionary"
"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
"github.com/mrz1836/go-parameters"
)

// Action is the configuration for the actions and related services
Expand Down Expand Up @@ -71,11 +74,8 @@ func (a *Action) RequireAdminAuthentication(fn httprouter.Handle) httprouter.Han
}

// Request will process the request in the router
func (a *Action) Request(router *apirouter.Router, h httprouter.Handle) httprouter.Handle {
if a.AppConfig.RequestLogging {
return router.Request(h)
}
return router.RequestNoLogging(h)
func (a *Action) Request(_ *apirouter.Router, h httprouter.Handle) httprouter.Handle {
return Request(h, a)
}

// CheckAuthentication will check the authentication
Expand All @@ -101,3 +101,40 @@ func CheckAuthentication(appConfig *config.AppConfig, bux bux.ClientInterface, r
// Return an empty error message
return req, dictionary.ErrorMessage{}
}

// Request will write the request to the logs before and after calling the handler
func Request(h httprouter.Handle, a *Action) httprouter.Handle {
return parameters.MakeHTTPRouterParsedReq(func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
// Start the custom response writer
guid, _ := uuid.NewV4()
writer := &apirouter.APIResponseWriter{
IPAddress: apirouter.GetClientIPAddress(req),
Method: req.Method,
RequestID: guid.String(),
ResponseWriter: w,
Status: 0, // future use with E-tags
URL: req.URL.String(),
UserAgent: req.UserAgent(),
}

// Start the log (timer)
start := time.Now()

// Fire the request
h(writer, req, ps)

// Complete the timer and final log
elapsed := time.Since(start)

if a.AppConfig.RequestLogging {
arkadiuszos4chain marked this conversation as resolved.
Show resolved Hide resolved
a.Services.Logger.Debug().
Str("logger", "http-request").
Int("status", writer.Status).
Str("remote", req.RemoteAddr).
Str("url", req.URL.String()).
Str("elapsed", elapsed.String()).
Str("remote_address", req.RemoteAddr).
Msgf("%d | %s | %s | %s | %s ", writer.Status, elapsed, req.RemoteAddr, req.Method, req.URL)
}
})
}
Loading