diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2025-06-04 12:51:47 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2025-06-04 12:51:47 +0200 |
commit | 3739c9e14b0c65a59a520dbfefa459e43af3bf20 (patch) | |
tree | 3731016eb12180f12817ed1094eee8cb0f67b49b /pkg/ext | |
parent | 90b2a890096ee9ab3ff84c57542b5220aa9ebe4c (diff) | |
download | cerrado-3739c9e14b0c65a59a520dbfefa459e43af3bf20.tar.gz cerrado-3739c9e14b0c65a59a520dbfefa459e43af3bf20.tar.bz2 cerrado-3739c9e14b0c65a59a520dbfefa459e43af3bf20.zip |
feat: Wrap request
Since request is not a interface I need to create a wraper for it so I
can extend it later.
Diffstat (limited to 'pkg/ext')
-rw-r--r-- | pkg/ext/auth.go | 22 | ||||
-rw-r--r-- | pkg/ext/compression.go | 31 | ||||
-rw-r--r-- | pkg/ext/log.go | 4 | ||||
-rw-r--r-- | pkg/ext/request.go | 14 | ||||
-rw-r--r-- | pkg/ext/router.go | 19 |
5 files changed, 63 insertions, 27 deletions
diff --git a/pkg/ext/auth.go b/pkg/ext/auth.go index 5c3070e..ef126ec 100644 --- a/pkg/ext/auth.go +++ b/pkg/ext/auth.go @@ -14,19 +14,20 @@ type authService interface { ValidateToken(token []byte) (bool, error) } -func DisableAuthentication(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func DisableAuthentication(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { ctx := r.Context() ctx = context.WithValue(ctx, "disableAuthentication", true) - next(w, r.WithContext(ctx)) + r.Request = r.WithContext(ctx) + next(w, r) } } func VerifyRespository( config *serverconfig.ConfigurationRepository, -) func(next http.HandlerFunc) http.HandlerFunc { - return func(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +) func(next HandlerFunc) HandlerFunc { + return func(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { name := r.PathValue("name") if name != "" { repo := config.GetByName(name) @@ -41,9 +42,9 @@ func VerifyRespository( } } -func Authenticate(auth authService) func(next http.HandlerFunc) http.HandlerFunc { - return func(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func Authenticate(auth authService) func(next HandlerFunc) HandlerFunc { + return func(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { cookie, err := r.Cookie("auth") if err != nil { if !errors.Is(err, http.ErrNoCookie) { @@ -70,9 +71,10 @@ func Authenticate(auth authService) func(next http.HandlerFunc) http.HandlerFunc ctx := r.Context() ctx = context.WithValue(ctx, "logged", valid) + r.Request = r.WithContext(ctx) slog.Info("Validated token", "valid?", valid) - next(w, r.WithContext(ctx)) + next(w, r) } } } diff --git a/pkg/ext/compression.go b/pkg/ext/compression.go index 6c7a219..d3a3df1 100644 --- a/pkg/ext/compression.go +++ b/pkg/ext/compression.go @@ -15,18 +15,37 @@ import ( "github.com/klauspost/compress/zstd" ) -var ( - errInvalidParam = errors.New("Invalid weighted param") -) +var errInvalidParam = errors.New("Invalid weighted param") type CompressionResponseWriter struct { innerWriter http.ResponseWriter compressWriter io.Writer } -func Compress(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func Compress(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { + // TODO: hand this better + if strings.HasSuffix(r.URL.Path, ".tar.gz") { + next(w, r) + return + } + + if accept, ok := r.Header["Accept-Encoding"]; ok { + if compress, algo := GetCompressionWriter(u.FirstOrZero(accept), w); algo != "" { + defer compress.Close() + w.Header().Add("Content-Encoding", algo) + w = &CompressionResponseWriter{ + innerWriter: w, + compressWriter: compress, + } + } + } + next(w, r) + } +} +func Decompress(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { // TODO: hand this better if strings.HasSuffix(r.URL.Path, ".tar.gz") { next(w, r) @@ -61,12 +80,12 @@ func GetCompressionWriter(header string, inner io.Writer) (io.WriteCloser, strin default: return nil, "" } - } func (c *CompressionResponseWriter) Header() http.Header { return c.innerWriter.Header() } + func (c *CompressionResponseWriter) Write(b []byte) (int, error) { return c.compressWriter.Write(b) } diff --git a/pkg/ext/log.go b/pkg/ext/log.go index 8e68134..e0ad89f 100644 --- a/pkg/ext/log.go +++ b/pkg/ext/log.go @@ -39,8 +39,8 @@ func wrap(w http.ResponseWriter) *statusWraper { } } -func Log(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func Log(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { t := time.Now() s := wrap(w) next(s, r) diff --git a/pkg/ext/request.go b/pkg/ext/request.go new file mode 100644 index 0000000..d1593b2 --- /dev/null +++ b/pkg/ext/request.go @@ -0,0 +1,14 @@ +package ext + +import ( + "io" + "net/http" +) + +type Request struct { + *http.Request +} + +func (r *Request) ReadBody() io.ReadCloser { + return r.Body +} diff --git a/pkg/ext/router.go b/pkg/ext/router.go index 434972b..bbbffa1 100644 --- a/pkg/ext/router.go +++ b/pkg/ext/router.go @@ -16,8 +16,9 @@ type ( middlewares []Middleware router *http.ServeMux } - Middleware func(next http.HandlerFunc) http.HandlerFunc - ErrorRequestHandler func(w http.ResponseWriter, r *http.Request) error + HandlerFunc func(http.ResponseWriter, *Request) + Middleware func(next HandlerFunc) HandlerFunc + ErrorRequestHandler func(w http.ResponseWriter, r *Request) error ) func NewRouter() *Router { @@ -34,15 +35,15 @@ func (r *Router) AddMiddleware(middleware Middleware) { r.middlewares = append(r.middlewares, middleware) } -func wrapError(next ErrorRequestHandler) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func wrapError(next ErrorRequestHandler) HandlerFunc { + return func(w http.ResponseWriter, r *Request) { if err := next(w, r); err != nil { if errors.Is(err, service.ErrRepositoryNotFound) || errors.Is(err, plumbing.ErrReferenceNotFound) { NotFound(w, r) } else { slog.Error("Internal Server Error", "error", err) - InternalServerError(r, w, err) + InternalServerError(w, r, err) } } } @@ -54,7 +55,7 @@ func (r *Router) run(next ErrorRequestHandler) http.HandlerFunc { for _, r := range r.middlewares { req = r(req) } - req(w, re) + req(w, &Request{Request: re}) } } @@ -62,14 +63,14 @@ func (r *Router) HandleFunc(path string, handler ErrorRequestHandler) { r.router.HandleFunc(path, r.run(handler)) } -func NotFound(w http.ResponseWriter, r *http.Request) { +func NotFound(w http.ResponseWriter, r *Request) { w.WriteHeader(http.StatusNotFound) templates.WritePageTemplate(w, &templates.ErrorPage{ Message: "Not Found", }, r.Context()) } -func BadRequest(w http.ResponseWriter, r *http.Request, msg string) { +func BadRequest(w http.ResponseWriter, r *Request, msg string) { w.WriteHeader(http.StatusBadRequest) templates.WritePageTemplate(w, &templates.ErrorPage{ Message: msg, @@ -81,7 +82,7 @@ func Redirect(w http.ResponseWriter, location string) { w.WriteHeader(http.StatusTemporaryRedirect) } -func InternalServerError(r *http.Request, w http.ResponseWriter, err error) { +func InternalServerError(w http.ResponseWriter, r *Request, err error) { w.WriteHeader(http.StatusInternalServerError) templates.WritePageTemplate(w, &templates.ErrorPage{ Message: fmt.Sprintf("Internal Server Error:\n%s", err.Error()), |