http

package
v0.0.0-...-ed7afed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package http provides the HTTP kernel, context, request, and response abstractions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Request  *Request
	Response *Response
	// contains filtered or unexported fields
}

Context holds the request, response writer, URL params, and a per-request store. It is the single argument to every handler and middleware.

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request, params map[string]string) *Context

NewContext creates a Context from a raw http.Request and ResponseWriter. This is used by the router and should not be called directly in application code.

func (*Context) Abort

func (c *Context) Abort(status int, message string) error

Abort stops middleware chain propagation with a JSON error response.

func (*Context) Bind

func (c *Context) Bind(dest any) error

Bind decodes the request body (JSON, XML, or form) into dest.

func (*Context) Ctx

func (c *Context) Ctx() context.Context

Ctx returns the request's context.Context.

func (*Context) File

func (c *Context) File(path string) error

File serves a file from the local filesystem.

func (*Context) FormFile

func (c *Context) FormFile(name string) (*multipart.FileHeader, error)

FormFile returns the first file uploaded under the given field name.

func (*Context) FormValue

func (c *Context) FormValue(name string) string

FormValue returns a form field value.

func (*Context) Get

func (c *Context) Get(key string) (any, bool)

Get retrieves a value from the per-request store.

func (*Context) HTML

func (c *Context) HTML(status int, html string) error

HTML writes an HTML body.

func (*Context) Header

func (c *Context) Header(name string) string

Header returns a request header by name.

func (*Context) IP

func (c *Context) IP() string

IP returns the client's real IP address, respecting X-Forwarded-For if set.

func (*Context) IsAJAX

func (c *Context) IsAJAX() bool

IsAJAX reports whether the request was made via XMLHttpRequest.

func (*Context) IsJSON

func (c *Context) IsJSON() bool

IsJSON reports whether the request content type is application/json.

func (*Context) JSON

func (c *Context) JSON(status int, v any) error

JSON writes a JSON-encoded body with the given status code.

func (*Context) Method

func (c *Context) Method() string

Method returns the HTTP method in uppercase.

func (*Context) MustGet

func (c *Context) MustGet(key string) any

MustGet retrieves a value and panics if the key is absent.

func (*Context) NoContent

func (c *Context) NoContent() error

NoContent sends a 204 No Content response.

func (*Context) Param

func (c *Context) Param(name string) string

Param returns a URL path parameter by name (e.g. ":id").

func (*Context) ParseUpload

func (c *Context) ParseUpload(field string, cfg ...UploadConfig) (*UploadedFile, error)

ParseUpload parses a single file upload from the request field `name`.

func (*Context) ParseUploads

func (c *Context) ParseUploads(field string, cfg ...UploadConfig) ([]*UploadedFile, error)

ParseUploads parses all files uploaded under a multi-value field name.

func (*Context) Path

func (c *Context) Path() string

Path returns the request URL path.

func (*Context) Query

func (c *Context) Query(name string) string

Query returns a URL query parameter by name.

func (*Context) QueryD

func (c *Context) QueryD(name, def string) string

QueryD returns a URL query parameter, or def if absent.

func (*Context) Redirect

func (c *Context) Redirect(status int, url string) error

Redirect sends a redirect response.

func (*Context) Set

func (c *Context) Set(key string, value any)

Set stores a value in the per-request key-value store.

func (*Context) String

func (c *Context) String(status int, format string, args ...any) error

String writes a plain-text body.

func (*Context) Validate

func (c *Context) Validate(dest any) error

Validate binds the request body into dest and then validates it against `validate` struct tags. On validation failure it returns a 422 HTTPError whose JSON body contains the field-level error map.

var req struct {
    Email    string `json:"email"    validate:"required,email"`
    Password string `json:"password" validate:"required,min=8"`
}
if err := c.Validate(&req); err != nil {
    return err // automatic 422 with {"errors":{"email":["..."]}}
}

func (*Context) WithContext

func (c *Context) WithContext(ctx context.Context) *Context

WithContext returns a shallow copy of the Context with a new context.Context.

func (*Context) XML

func (c *Context) XML(status int, v any) error

XML writes an XML-encoded body with the given status code.

type HTTPError

type HTTPError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

HTTPError represents an HTTP error with a status code and message.

func NewHTTPError

func NewHTTPError(code int, message string) *HTTPError

NewHTTPError creates an HTTPError.

func (*HTTPError) Error

func (e *HTTPError) Error() string

type HandlerFunc

type HandlerFunc func(*Context) error

HandlerFunc is an OniWorks HTTP handler. Handlers return an error to signal failure; the error is dispatched to the registered error handler automatically.

type Kernel

type Kernel struct {
	// contains filtered or unexported fields
}

Kernel is the HTTP server kernel. It wraps the standard library's http.Server and adds graceful shutdown, TLS support, and signal handling.

func NewKernel

func NewKernel(cfg ServerConfig, handler http.Handler) *Kernel

NewKernel creates a Kernel with the given config and handler.

func (*Kernel) Addr

func (k *Kernel) Addr() string

Addr returns the listening address (useful for tests that pick a random port).

func (*Kernel) ListenAndServeBackground

func (k *Kernel) ListenAndServeBackground() (string, error)

ListenAndServeBackground starts the server in a goroutine and returns the bound address. Useful for integration tests.

func (*Kernel) Serve

func (k *Kernel) Serve() error

Serve starts the HTTP server and blocks until a shutdown signal is received. It handles SIGINT and SIGTERM for graceful shutdown.

func (*Kernel) Shutdown

func (k *Kernel) Shutdown() error

Shutdown performs a graceful shutdown with the configured timeout.

func (*Kernel) WithLogger

func (k *Kernel) WithLogger(l *slog.Logger) *Kernel

WithLogger sets a custom slog.Logger on the kernel.

type Map

type Map = map[string]any

Map is a convenience alias for map[string]any used in JSON/template responses.

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc wraps a HandlerFunc, returning a new HandlerFunc.

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request wraps *http.Request with additional helpers.

func (*Request) BearerToken

func (r *Request) BearerToken() string

BearerToken extracts the Bearer token from the Authorization header.

func (*Request) Bind

func (r *Request) Bind(dest any) error

Bind decodes the request body based on the Content-Type header. Supports application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data.

func (*Request) BodyBytes

func (r *Request) BodyBytes() ([]byte, error)

BodyBytes reads and returns the request body as raw bytes (does not consume Body for later reads).

func (*Request) IP

func (r *Request) IP() string

IP returns the real client IP, considering X-Forwarded-For and X-Real-IP headers.

func (*Request) IsSecure

func (r *Request) IsSecure() bool

IsSecure reports whether the request was made over HTTPS.

func (*Request) Param

func (r *Request) Param(name string) string

Param returns a path parameter value (e.g. for route "/users/:id", Param("id")).

func (*Request) Params

func (r *Request) Params() map[string]string

Params returns all path parameters as a map.

func (*Request) WantsJSON

func (r *Request) WantsJSON() bool

WantsJSON reports whether the client prefers a JSON response (via Accept header).

type Response

type Response struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

Response wraps http.ResponseWriter with status tracking and body size counting.

func (*Response) Committed

func (r *Response) Committed() bool

Committed reports whether the response headers have been sent.

func (*Response) Flush

func (r *Response) Flush()

Flush implements http.Flusher if the underlying writer supports it.

func (*Response) Hijack

func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements http.Hijacker if the underlying writer supports it (needed for WebSocket upgrades).

func (*Response) Size

func (r *Response) Size() int64

Size returns the number of bytes written to the response body.

func (*Response) Status

func (r *Response) Status() int

Status returns the HTTP status code sent (or 200 if WriteHeader was never called).

func (*Response) Unwrap

func (r *Response) Unwrap() http.ResponseWriter

Unwrap returns the underlying http.ResponseWriter for type-assertion chains.

func (*Response) Wrap

func (r *Response) Wrap(w http.ResponseWriter)

Wrap replaces the underlying http.ResponseWriter. Used by middleware like Compress that need to intercept writes. The status/size counters are reset.

func (*Response) Write

func (r *Response) Write(b []byte) (int, error)

Write sends bytes to the client.

func (*Response) WriteHeader

func (r *Response) WriteHeader(code int)

WriteHeader captures the status code and delegates to the underlying writer. Calling this more than once is a no-op (the header is sent only once).

type ServerConfig

type ServerConfig struct {
	Host            string
	Port            int
	TLSCertFile     string
	TLSKeyFile      string
	ReadTimeout     time.Duration
	WriteTimeout    time.Duration
	IdleTimeout     time.Duration
	ShutdownTimeout time.Duration
}

ServerConfig holds configuration for the HTTP server.

func DefaultServerConfig

func DefaultServerConfig() ServerConfig

DefaultServerConfig returns production-ready defaults.

type UploadConfig

type UploadConfig struct {
	MaxSize      int64    // bytes; default 10 MB
	AllowedTypes []string // e.g. ["image/jpeg","image/png"]; nil = all allowed
}

UploadConfig controls max file size and allowed MIME types.

func DefaultUploadConfig

func DefaultUploadConfig() UploadConfig

DefaultUploadConfig returns sensible upload defaults.

type UploadedFile

type UploadedFile struct {
	Header   *multipart.FileHeader
	Original string // original filename from client
	Size     int64  // bytes
	MIMEType string
}

UploadedFile wraps a *multipart.FileHeader with helpers for inspection and saving.

func (*UploadedFile) Ext

func (uf *UploadedFile) Ext() string

Ext returns the lowercase file extension including the dot (e.g. ".jpg").

func (*UploadedFile) IsImage

func (uf *UploadedFile) IsImage() bool

IsImage reports whether the MIME type indicates an image.

func (*UploadedFile) Open

func (uf *UploadedFile) Open() (multipart.File, error)

Open returns a ReadCloser for the uploaded file content.

func (*UploadedFile) Store

func (uf *UploadedFile) Store(dir string, filename ...string) (string, error)

Store saves the uploaded file to the given destination directory. Returns the full path to the saved file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL