diff --git a/ctx.go b/ctx.go index e8a181bb..cf405643 100644 --- a/ctx.go +++ b/ctx.go @@ -26,8 +26,6 @@ type ContextNoBody = ContextWithBody[any] type ContextWithBody[B any] interface { context.Context - ValidableCtx - // Body returns the body of the request. // If (*B) implements [InTransformer], it will be transformed after deserialization. // It caches the result, so it can be called multiple times. @@ -93,9 +91,6 @@ type ContextWithBody[B any] interface { // SetStatus sets the status code of the response. // Alias to http.ResponseWriter.WriteHeader. SetStatus(code int) - // SetDefaultStatusCode sets the status code of the response defined in the options. - // Is automatically done by Fuego but you might need it. - SetDefaultStatusCode() // Redirect redirects to the given url with the given status code. // Example: @@ -104,13 +99,6 @@ type ContextWithBody[B any] interface { // return c.Redirect(301, "/recipes-list") // }) Redirect(code int, url string) (any, error) - - // Serialize serializes the given data to the response. - // Is automatically done by Fuego but you might need it. - Serialize(data any) error - // SerializeError serializes the given error to the response. - // Is automatically done by Fuego but you might need it. - SerializeError(err error) } // NewNetHTTPContext returns a new context. It is used internally by Fuego. You probably want to use Ctx[B] instead. @@ -144,9 +132,9 @@ type netHttpContext[Body any] struct { fs fs.FS templates *template.Template - readOptions readOptions - serializer Sender - serializeError ErrorSender + readOptions readOptions + serializer Sender + errorSerializer ErrorSender } var ( @@ -280,14 +268,14 @@ func (c netHttpContext[B]) Serialize(data any) error { // SerializeError serializes the given error to the response. It uses the Content-Type header to determine the serialization format. func (c netHttpContext[B]) SerializeError(err error) { - if c.serializeError == nil { + if c.errorSerializer == nil { SendError(c.Res, c.Req, err) return } - c.serializeError(c.Res, c.Req, err) + c.errorSerializer(c.Res, c.Req, err) } -// SetDefaultStatusCode sets the default status code of the response. +// setDefaultStatusCode sets the default status code of the response. func (c netHttpContext[B]) SetDefaultStatusCode() { if c.DefaultStatusCode != 0 { c.SetStatus(c.DefaultStatusCode) diff --git a/serve.go b/serve.go index 72a10595..f1fe06b7 100644 --- a/serve.go +++ b/serve.go @@ -77,16 +77,31 @@ func HTTPHandler[ReturnType, Body any](s *Server, controller func(c ContextWithB MaxBodySize: s.maxBodySize, }) ctx.serializer = s.Serialize - ctx.serializeError = s.SerializeError + ctx.errorSerializer = s.SerializeError ctx.fs = s.fs ctx.templates = templates - flow(s.Engine, ctx, controller) + Flow(s.Engine, ctx, controller) } } +// Contains the logic for the flow of a Fuego controller. +// Extends ContextWithBody with private methods. +type ContextFlowable[B any] interface { + ContextWithBody[B] + + ValidableCtx + + // SetDefaultStatusCode sets the status code of the response defined in the options. + SetDefaultStatusCode() + // Serialize serializes the given data to the response. + Serialize(data any) error + // SerializeError serializes the given error to the response. + SerializeError(err error) +} + // Generic handler for Fuego controllers. -func flow[B, T any](s *Engine, ctx ContextWithBody[B], controller func(c ContextWithBody[B]) (T, error)) { +func Flow[B, T any](s *Engine, ctx ContextFlowable[B], controller func(c ContextWithBody[B]) (T, error)) { ctx.SetHeader("X-Powered-By", "Fuego") ctx.SetHeader("Trailer", "Server-Timing")