Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Atendi9Context ¶
type Atendi9Context struct {
Context
}
Atendi9Context is a wrapper around Context that allows extending or customizing behavior without modifying the original implementation.
It can be used to add helper methods specific to your application.
func NewContext ¶
func NewContext(ctx Context) *Atendi9Context
NewContext creates a new Atendi9Context wrapping the given Context.
Example:
ctx := NewContext(originalCtx)
func (*Atendi9Context) Test ¶
func (c *Atendi9Context) Test(ctx Context) *Atendi9Context
Test replaces the underlying Context instance.
This method is primarily useful for testing or dynamically swapping context implementations.
Example:
ctx.Test(mockCtx)
type Context ¶
type Context interface {
// Headers returns all request headers.
//
// The returned map follows the standard Go format:
// map[string][]string.
Headers() map[string][]string
// BodyParser parses the request body into the given struct.
//
// It supports formats such as JSON, XML, or form data,
// depending on the implementation.
//
// Example:
// var req MyStruct
// if err := c.BodyParser(&req); err != nil {
// return err
// }
BodyParser(v any) error
// QueryParser parses query string parameters into the given struct.
//
// Example:
// var query QueryParams
// _ = c.QueryParser(&query)
QueryParser(v any) error
// ParamsParser parses route (path) parameters into the given struct.
//
// Example:
// // Route: /users/:id
// var params struct { ID string `param:"id"` }
// _ = c.ParamsParser(¶ms)
ParamsParser(v any) error
// ReqHeaderParser parses request headers into the given struct.
//
// This is useful for binding headers to typed structures.
ReqHeaderParser(v any) error
// Header returns the value of a specific request header.
//
// If the header does not exist, an empty string is returned.
Header(key string) string
// Method returns the HTTP method used in the request
// (e.g., GET, POST, PUT, DELETE).
Method() string
// IP returns the client IP address.
//
// Depending on the implementation, this may consider proxy headers
// such as X-Forwarded-For.
IP() string
// IPs returns all IP addresses associated with the request,
// including proxy chain addresses.
IPs() []string
// Body returns the raw request body.
//
// Useful when manual parsing is needed.
Body() []byte
// Query returns the value of a query parameter.
//
// If the parameter is not present, it returns the optional default value.
//
// Example:
// name := c.Query("name", "guest")
Query(name string, defaultValue ...string) string
// Params returns the value of a route (path) parameter.
//
// If the parameter is not present, it returns the optional default value.
//
// Example:
// id := c.Params("id", "0")
Params(name string, defaultValue ...string) string
// FormFile retrieves a file uploaded via multipart form.
//
// Returns a pointer to multipart.FileHeader, which can be used
// to open and read the file.
//
// Example:
// file, err := c.FormFile("avatar")
FormFile(key string) (*multipart.FileHeader, error)
// SendStatus sets the HTTP status code and sends the response.
//
// Example:
// return c.SendStatus(404)
SendStatus(status int) error
// Send writes raw bytes as the response body.
//
// Example:
// return c.Send([]byte("ok"))
Send(data []byte) error
// JSON serializes the given data as JSON and writes it to the response.
//
// Example:
// return c.JSON(map[string]string{"status": "ok"})
JSON(data any) error
// Next passes control to the next middleware/handler in the chain.
//
// Commonly used in middleware pipelines.
Next() error
// Now returns the current time.
//
// This abstraction allows easier testing by mocking time.
Now() time.Time
// Path returns the request path.
//
// If no path is available, it may return the optional default value.
Path(defaultValue ...string) string
}
Context defines an abstraction over an HTTP request/response lifecycle.
It provides a unified API to access request data (headers, body, params), parse input into structs, and build responses.
This interface is designed to be framework-agnostic, allowing different HTTP engines (Fiber, Echo, net/http, etc.) to implement it.
type Converter ¶
type Converter[T any] interface { // Convert transforms a generic Handler into a framework-specific handler. Convert(h Handler) T }
Converter defines a generic adapter that transforms a Handler into a framework-specific handler type.
This abstraction allows the same business logic (Handler) to be reused across different HTTP frameworks.
T represents the target handler type (e.g., fiber.Handler, echo.HandlerFunc).
Example (Fiber):
type FiberConverter struct{}
func (f FiberConverter) Convert(h Handler) fiber.Handler {
return func(c *fiber.Ctx) error {
// Wrap framework context into our abstraction
ctx := config.FiberContext{Ctx: c}
// Execute handler
res := h(Atendi9Context{ctx})
// Middleware flow control
if res.GoNext() {
return c.Next()
}
// File response
if len(res.FilePath) > 0 {
return c.SendFile(res.FilePath)
}
// Error handling (priority over Data)
if err := res.Err; err != nil {
return c.Status(res.Status()).JSON(fiber.Map{
"err": err.Error(),
})
}
// String response optimization
if v, ok := res.Data.(string); ok {
return c.Status(res.Status()).SendString(v)
}
// Default: JSON response
return c.Status(res.Status()).JSON(res.Data)
}
}
type Handler ¶
Handler represents a generic request handler.
It receives a Context abstraction and returns a Response, allowing full control over request parsing and response generation.
This design enables framework-independent business logic.
Example:
func HelloHandler(c Context) Response {
name := c.Query("name", "guest")
return Response{
Data: map[string]string{
"message": "Hello " + name,
},
}
}
Example with error:
func ErrorHandler(c Context) Response {
return Response{
Err: errors.New("something went wrong"),
StatusCode: 500,
}
}
type Response ¶
type Response struct {
// Err represents an error that occurred during request handling.
//
// If set, it usually takes priority over Data and will be
// serialized as an error response by the converter.
Err error
// StatusCode defines the HTTP status code to be returned.
//
// If not explicitly set or invalid, it defaults to 200 (OK).
StatusCode int
// FilePath, if set, indicates that a file should be sent
// as the response instead of JSON or raw data.
FilePath string
// Data holds the response payload.
//
// It can be:
// - struct/map → serialized as JSON
// - string → sent as plain text (depending on converter)
// - any other type supported by the converter
Data any
// contains filtered or unexported fields
}
Response represents the result of a handler execution.
It encapsulates all possible outcomes of a request, including:
- HTTP status code
- response data (JSON, string, etc.)
- file responses
- error handling
- middleware flow control (Next)
This struct is designed to be interpreted by a Converter, which translates it into a specific framework response (Fiber, Echo, etc.).
func SendStatus ¶
SendStatus creates a Response with only a status code.
Useful for simple responses without a body.
Example:
return SendStatus(404)
func (Response) GoNext ¶
GoNext returns whether the handler chain should continue.
This method should be used instead of accessing internal fields directly, ensuring proper encapsulation.
Example:
if res.GoNext() {
// call next middleware
}
func (Response) JSON ¶ added in v1.0.1
JSON sets the JSON payload on the response.
The provided data is assigned to the Data field; all other fields already set on the receiver (StatusCode, Err, FilePath, next) are preserved. The StatusCode is normalized to a valid value via Status.
The provided data should be serializable by the converter (usually to JSON).
Example:
return Response{}.JSON(map[string]string{
"message": "ok",
})
func (Response) Next ¶
Next marks the response to pass execution to the next handler.
This is typically used in middleware scenarios.
Example:
return Response{}.Next()
func (Response) Status ¶
Status returns a valid HTTP status code.
If the StatusCode is unset (its zero value, 0) or falls outside the valid HTTP range [100, 599], it defaults to http.StatusOK (200).
Any explicitly set code within the valid range is returned as-is, so 1xx informational codes and an intentional 200 are both preserved.