Add some middleware
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-19 03:23:54 +01:00
parent 9449b37ab1
commit e3ff8065f1
3 changed files with 55 additions and 15 deletions

View File

@ -57,6 +57,7 @@ func ActionServe(c *cli.Context) error {
// Setup loggers // Setup loggers
rootLogger := getRootLogger(cfg.LogLevel) rootLogger := getRootLogger(cfg.LogLevel)
serverLogger := rootLogger.Named("SERV") serverLogger := rootLogger.Named("SERV")
accessLogger := rootLogger.Named("ACCS")
// Setup contexts for clean shutdown // Setup contexts for clean shutdown
rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt) rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt)
@ -70,6 +71,7 @@ func ActionServe(c *cli.Context) error {
srv := gpaste.NewHTTPServer(cfg) srv := gpaste.NewHTTPServer(cfg)
srv.Addr = cfg.ListenAddr srv.Addr = cfg.ListenAddr
srv.Logger = serverLogger srv.Logger = serverLogger
srv.AccessLogger = accessLogger
// Wait for cancel // Wait for cancel
go func() { go func() {

28
http.go
View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/google/uuid" "github.com/google/uuid"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -15,6 +16,7 @@ type HTTPServer struct {
store FileStore store FileStore
config *ServerConfig config *ServerConfig
Logger *zap.SugaredLogger Logger *zap.SugaredLogger
AccessLogger *zap.SugaredLogger
http.Server http.Server
} }
@ -22,10 +24,14 @@ func NewHTTPServer(cfg *ServerConfig) *HTTPServer {
srv := &HTTPServer{ srv := &HTTPServer{
config: cfg, config: cfg,
Logger: zap.NewNop().Sugar(), Logger: zap.NewNop().Sugar(),
AccessLogger: zap.NewNop().Sugar(),
} }
srv.store = NewMemoryFileStore() srv.store = NewMemoryFileStore()
r := chi.NewRouter() r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
r.Use(srv.MiddlewareAccessLogger)
r.Get("/", srv.HandlerIndex) r.Get("/", srv.HandlerIndex)
r.Post("/api/file", srv.HandlerAPIFilePost) r.Post("/api/file", srv.HandlerAPIFilePost)
r.Get("/api/file/{id}", srv.HandlerAPIFileGet) r.Get("/api/file/{id}", srv.HandlerAPIFileGet)
@ -43,6 +49,7 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
ID: uuid.Must(uuid.NewRandom()).String(), ID: uuid.Must(uuid.NewRandom()).String(),
Body: r.Body, Body: r.Body,
} }
reqID := middleware.GetReqID(r.Context())
// Check if multipart form // Check if multipart form
ct := r.Header.Get("Content-Type") ct := r.Header.Get("Content-Type")
@ -53,10 +60,10 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
err := s.store.Store(f) err := s.store.Store(f)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
s.Logger.Warnw("Error storing file.", "erorr", err, "id", f.ID, "remote_addr", r.RemoteAddr) s.Logger.Warnw("Error storing file.", "req_id", reqID, "error", err, "id", f.ID, "remote_addr", r.RemoteAddr)
return return
} }
s.Logger.Infow("Stored file.", "id", f.ID, "remote_addr", r.RemoteAddr) s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "remote_addr", r.RemoteAddr)
var resp = struct { var resp = struct {
Message string `json:"message"` Message string `json:"message"`
ID string `json:"id"` ID string `json:"id"`
@ -69,7 +76,7 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusAccepted) w.WriteHeader(http.StatusAccepted)
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
if err := encoder.Encode(&resp); err != nil { if err := encoder.Encode(&resp); err != nil {
s.Logger.Warnw("Error encoding response to client.", "error", err, "remote_addr", r.RemoteAddr) s.Logger.Warnw("Error encoding response to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
} }
} }
@ -89,12 +96,13 @@ func (s *HTTPServer) HandlerAPIFileGet(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
if _, err := io.Copy(w, f.Body); err != nil { if _, err := io.Copy(w, f.Body); err != nil {
s.Logger.Warnw("Error writing file to client.", "error", err, "remote_addr", r.RemoteAddr) reqID := middleware.GetReqID(r.Context())
s.Logger.Warnw("Error writing file to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
} }
} }
func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) { func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
s.Logger.Debugw("Processing multipart form.") reqID := middleware.GetReqID(r.Context())
type resp struct { type resp struct {
Message string `json:"message"` Message string `json:"message"`
ID string `json:"id"` ID string `json:"id"`
@ -104,12 +112,12 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
var responses []resp var responses []resp
if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil { if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil {
s.Logger.Warnw("Error parsing multipart form.", "err", err) s.Logger.Warnw("Error parsing multipart form.", "req_id", reqID, "err", err)
} }
for k := range r.MultipartForm.File { for k := range r.MultipartForm.File {
ff, fh, err := r.FormFile(k) ff, fh, err := r.FormFile(k)
if err != nil { if err != nil {
s.Logger.Warnw("Error reading file from multipart form.", "error", err) s.Logger.Warnw("Error reading file from multipart form.", "req_id", reqID, "error", err)
return return
} }
f := &File{ f := &File{
@ -120,10 +128,10 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
if err := s.store.Store(f); err != nil { if err := s.store.Store(f); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
s.Logger.Warnw("Error storing file.", "erorr", err, "id", f.ID, "remote_addr", r.RemoteAddr) s.Logger.Warnw("Error storing file.", "req_id", reqID, "error", err, "id", f.ID, "remote_addr", r.RemoteAddr)
return return
} }
s.Logger.Infow("Stored file.", "id", f.ID, "filename", f.OriginalFilename, "remote_addr", r.RemoteAddr) s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "filename", f.OriginalFilename, "remote_addr", r.RemoteAddr)
responses = append(responses, resp{Message: "OK", ID: f.ID, URL: "TODO"}) responses = append(responses, resp{Message: "OK", ID: f.ID, URL: "TODO"})
@ -132,6 +140,6 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
w.WriteHeader(http.StatusAccepted) w.WriteHeader(http.StatusAccepted)
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
if err := encoder.Encode(&responses); err != nil { if err := encoder.Encode(&responses); err != nil {
s.Logger.Warnw("Error encoding response to client.", "error", err, "remote_addr", r.RemoteAddr) s.Logger.Warnw("Error encoding response to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
} }
} }

30
middleware.go Normal file
View File

@ -0,0 +1,30 @@
package gpaste
import (
"net/http"
"time"
"github.com/go-chi/chi/v5/middleware"
)
func (s *HTTPServer) MiddlewareAccessLogger(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
t1 := time.Now()
reqID := middleware.GetReqID(r.Context())
defer func() {
s.AccessLogger.Infow(r.Method,
"path", r.URL.Path,
"status", ww.Status(),
"written", ww.BytesWritten(),
"remote_addr", r.RemoteAddr,
"processing_time_ms", time.Since(t1).Milliseconds(),
"req_id", reqID)
}()
next.ServeHTTP(ww, r)
}
return http.HandlerFunc(fn)
}