Documentation
¶
Overview ¶
Package http re-exports commonly used net/http symbols so that consumers only need a single import: "github.com/CodeSyncr/nimbus/http".
Index ¶
- Constants
- Variables
- func SPAHandler(dir string) http.HandlerFunc
- func ServeStatic(dir string) http.Handler
- func ServeStaticFile(filePath string) http.HandlerFunc
- type Authenticator
- type Container
- type Context
- func (c *Context) Abort(code int) error
- func (c *Context) AbortWithJSON(code int, body any) error
- func (c *Context) Accepted(body any) error
- func (c *Context) All() map[string]string
- func (c *Context) Attachment(filename string)
- func (c *Context) Auth() Authenticator
- func (c *Context) BadRequest(body any) error
- func (c *Context) Bind(v any) error
- func (c *Context) BindJSON(v any) error
- func (c *Context) CacheControl(directives string) *Context
- func (c *Context) ClearCookie(name, path, domain string)
- func (c *Context) ContentType(ct string) *Context
- func (c *Context) Cookie(name string, def ...string) string
- func (c *Context) Created(body any) error
- func (c *Context) Ctx() context.Context
- func (c *Context) Data(code int, contentType string, data []byte)
- func (c *Context) Deadline() (deadline interface{ ... }, ok bool)
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Download(filePath, fileName string) error
- func (c *Context) Err() error
- func (c *Context) Except(keys ...string) map[string]string
- func (c *Context) Expires(t time.Time) *Context
- func (c *Context) File(name string) (multipart.File, *multipart.FileHeader, error)
- func (c *Context) Files(name string) []*multipart.FileHeader
- func (c *Context) Filled(keys ...string) bool
- func (c *Context) Flush()
- func (c *Context) Forbidden(body ...any) error
- func (c *Context) FullURL() string
- func (c *Context) Get(key string) (any, bool)
- func (c *Context) HTML(code int, html string)
- func (c *Context) Has(key string) bool
- func (c *Context) HasFile(name string) bool
- func (c *Context) Header(key string) string
- func (c *Context) IP() string
- func (c *Context) Inline(filePath, fileName string) error
- func (c *Context) Input(key string, def ...string) string
- func (c *Context) InputBool(key string) bool
- func (c *Context) InputFloat(key string, def float64) float64
- func (c *Context) InputInt(key string, def int) int
- func (c *Context) IsAjax() bool
- func (c *Context) IsJSON() bool
- func (c *Context) JSON(code int, body any) error
- func (c *Context) LastModified(t time.Time) *Context
- func (c *Context) Method() string
- func (c *Context) MustGet(key string) any
- func (c *Context) NoCache() *Context
- func (c *Context) NoContent()
- func (c *Context) NotFound(body ...any) error
- func (c *Context) Only(keys ...string) map[string]string
- func (c *Context) Param(name string) string
- func (c *Context) Path() string
- func (c *Context) Query(key string, def ...string) string
- func (c *Context) QueryBool(key string) bool
- func (c *Context) QueryInt(key string, def int) int
- func (c *Context) Redirect(code int, url string)
- func (c *Context) Require(key string) (any, error)
- func (c *Context) SSE(event, data string) error
- func (c *Context) SendFile(dir, file string)
- func (c *Context) ServerError(body ...any) error
- func (c *Context) Set(key string, val any)
- func (c *Context) SetAuth(a Authenticator)
- func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
- func (c *Context) SetHeader(key, value string)
- func (c *Context) Status(code int) *Context
- func (c *Context) StatusCode() int
- func (c *Context) Stream(code int, contentType string, fn func(w io.Writer) error) error
- func (c *Context) StreamJSON(code int, fn func(enc *json.Encoder) error) error
- func (c *Context) String(code int, s string)
- func (c *Context) Unauthorized(body ...any) error
- func (c *Context) UserAgent() string
- func (c *Context) View(name string, data any) error
- func (c *Context) WithContext(ctx context.Context) *Context
- func (c *Context) Write(data []byte) (int, error)
- func (c *Context) WriteString(s string) (int, error)
- type Cookie
- type Dir
- type FileSystem
- type Flusher
- type Handler
- type Header
- type Request
- type ResponseWriter
- type SameSite
- type ServeMux
- type Session
- type StdHandlerFunc
Constants ¶
const ( SameSiteDefaultMode = stdlib.SameSiteDefaultMode SameSiteLaxMode = stdlib.SameSiteLaxMode SameSiteStrictMode = stdlib.SameSiteStrictMode SameSiteNoneMode = stdlib.SameSiteNoneMode )
SameSite mode constants.
const ( MethodGet = stdlib.MethodGet MethodHead = stdlib.MethodHead MethodPost = stdlib.MethodPost MethodPut = stdlib.MethodPut MethodPatch = stdlib.MethodPatch MethodDelete = stdlib.MethodDelete MethodConnect = stdlib.MethodConnect MethodOptions = stdlib.MethodOptions MethodTrace = stdlib.MethodTrace )
const ( StatusContinue = stdlib.StatusContinue StatusSwitchingProtocols = stdlib.StatusSwitchingProtocols StatusOK = stdlib.StatusOK StatusCreated = stdlib.StatusCreated StatusAccepted = stdlib.StatusAccepted StatusNoContent = stdlib.StatusNoContent StatusResetContent = stdlib.StatusResetContent StatusPartialContent = stdlib.StatusPartialContent StatusMovedPermanently = stdlib.StatusMovedPermanently StatusFound = stdlib.StatusFound StatusSeeOther = stdlib.StatusSeeOther StatusNotModified = stdlib.StatusNotModified StatusTemporaryRedirect = stdlib.StatusTemporaryRedirect StatusPermanentRedirect = stdlib.StatusPermanentRedirect StatusBadRequest = stdlib.StatusBadRequest StatusPaymentRequired = stdlib.StatusPaymentRequired StatusForbidden = stdlib.StatusForbidden StatusNotFound = stdlib.StatusNotFound StatusMethodNotAllowed = stdlib.StatusMethodNotAllowed StatusConflict = stdlib.StatusConflict StatusGone = stdlib.StatusGone StatusUnprocessableEntity = stdlib.StatusUnprocessableEntity StatusTooManyRequests = stdlib.StatusTooManyRequests StatusInternalServerError = stdlib.StatusInternalServerError StatusNotImplemented = stdlib.StatusNotImplemented StatusBadGateway = stdlib.StatusBadGateway StatusGatewayTimeout = stdlib.StatusGatewayTimeout )
Variables ¶
var Error = stdlib.Error
Error replies to the request with the specified error message and HTTP code.
var FileServer = stdlib.FileServer
FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at root.
var ListenAndServe = stdlib.ListenAndServe
ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections.
var MaxBytesReader = stdlib.MaxBytesReader
MaxBytesReader limits the size of an incoming request body.
var NewServeMux = stdlib.NewServeMux
NewServeMux allocates and returns a new ServeMux.
var NotFoundHandler = stdlib.NotFoundHandler
NotFoundHandler returns a simple handler that replies with a 404 not found error.
var ServeFile = stdlib.ServeFile
ServeFile replies to the request with the contents of the named file.
var SetCookie = stdlib.SetCookie
SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers.
var StatusText = stdlib.StatusText
StatusText returns a text for the HTTP status code.
var StdRedirect = stdlib.Redirect
Redirect replies to the request with a redirect to url.
var StripPrefix = stdlib.StripPrefix
StripPrefix returns a handler that serves HTTP requests by removing the given prefix from the request URL's Path.
Functions ¶
func SPAHandler ¶
func SPAHandler(dir string) http.HandlerFunc
SPAHandler serves an SPA: serves static files from dir, falls back to index.html.
func ServeStatic ¶
ServeStatic serves static files from the given directory.
func ServeStaticFile ¶
func ServeStaticFile(filePath string) http.HandlerFunc
ServeStaticFile serves a single file.
Types ¶
type Authenticator ¶
type Authenticator interface {
User() (any, error)
Login(user any) error
Logout() error
Check() bool
}
Authenticator provides AdonisJS-style auth access on the context. Implemented by auth.Accessor so controllers can write c.Auth().User() instead of container.MustMake("auth.guard").(auth.Guard).
type Context ¶
type Context struct {
Request *Request
Response ResponseWriter
Session Session
Container Container
Params map[string]string
// contains filtered or unexported fields
}
Context wraps an HTTP request and response with AdonisJS-style helpers.
func New ¶
func New(w ResponseWriter, r *Request, params map[string]string) *Context
New creates a new request context.
func (*Context) Abort ¶
Abort writes the given status code with no body and returns a generic error.
func (*Context) AbortWithJSON ¶
AbortWithJSON sends a JSON error and returns an error.
func (*Context) Attachment ¶
Attachment sets the Content-Disposition header to "attachment".
func (*Context) Auth ¶
func (c *Context) Auth() Authenticator
Auth returns the AdonisJS-style auth accessor for this request. Usage: user, err := c.Auth().User()
func (*Context) BadRequest ¶
BadRequest sends a 400 Bad Request JSON response.
func (*Context) Bind ¶
Bind decodes the request body (JSON or form) into v. Content-Type is auto-detected.
func (*Context) CacheControl ¶
CacheControl sets the Cache-Control response header.
func (*Context) ClearCookie ¶
ClearCookie removes a cookie by setting MaxAge to -1.
func (*Context) ContentType ¶
ContentType sets the Content-Type response header.
func (*Context) Ctx ¶
Ctx returns the request's context.Context. Use this to pass deadlines, cancellation signals, and request-scoped values to downstream calls (database queries, HTTP clients, etc.).
func (*Context) Done ¶
func (c *Context) Done() <-chan struct{}
Done returns the request context's Done channel.
func (*Context) Err ¶
Err returns the request context's error (nil, context.Canceled, or context.DeadlineExceeded).
func (*Context) Files ¶
func (c *Context) Files(name string) []*multipart.FileHeader
Files returns all uploaded files for a form field (e.g. "photos[]").
func (*Context) Flush ¶
func (c *Context) Flush()
Flush flushes the response writer if it supports flushing.
func (*Context) FullURL ¶
FullURL returns the full request URL including scheme and host. Trusts X-Forwarded-Proto for scheme detection — use middleware.TrustedProxies to strip this header from untrusted sources.
func (*Context) IP ¶
IP returns the client's IP address from forwarding headers or RemoteAddr. This method trusts X-Forwarded-For and X-Real-IP headers. For this to be safe in production, use middleware.TrustedProxies to strip these headers from untrusted sources before they reach your handlers.
func (*Context) Input ¶
Input returns a form or query parameter by key. Checks POST body first, then query string.
func (*Context) InputBool ¶
InputBool returns a form or query parameter as bool. Truthy values: "1", "true", "yes", "on".
func (*Context) InputFloat ¶
InputFloat returns a form or query parameter as float64.
func (*Context) LastModified ¶
LastModified sets the Last-Modified header.
func (*Context) NoContent ¶
func (c *Context) NoContent()
NoContent sends a 204 No Content response.
func (*Context) Require ¶
Require retrieves a stored value or returns an error when missing. Prefer this in runtime code paths where panics are undesirable.
func (*Context) ServerError ¶
ServerError sends a 500 Internal Server Error JSON response.
func (*Context) SetAuth ¶
func (c *Context) SetAuth(a Authenticator)
SetAuth sets the auth accessor (called by auth.Init middleware).
func (*Context) SetCookie ¶
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
SetCookie sets a response cookie with the given parameters.
func (*Context) Status ¶
Status sets the HTTP status code for the next response write. Does NOT flush headers immediately — the actual WriteHeader call happens when JSON/View/String/HTML writes the response body.
func (*Context) StatusCode ¶
StatusCode returns the current HTTP status code.
func (*Context) Stream ¶
Stream sends a streaming response. The callback writes directly to the response.
func (*Context) StreamJSON ¶
StreamJSON sends a streaming JSON response (useful for large datasets).
func (*Context) Unauthorized ¶
Unauthorized sends a 401 Unauthorized JSON response.
func (*Context) View ¶
View renders a .nimbus template and sends HTML response. When Shield CSRF is enabled, csrfField is auto-injected so templates can use {{ .csrfField }}.
func (*Context) WithContext ¶
WithContext returns a shallow copy of Context with the request's context replaced by ctx. Use this to attach deadlines or values:
ctx, cancel := context.WithTimeout(c.Ctx(), 5*time.Second) defer cancel() c = c.WithContext(ctx)
type FileSystem ¶
type FileSystem = stdlib.FileSystem
FileSystem is a re-export of net/http.FileSystem.
type ResponseWriter ¶
type ResponseWriter = stdlib.ResponseWriter
ResponseWriter is a re-export of net/http.ResponseWriter.
type SameSite ¶
SameSite allows a server to define a cookie attribute making it impossible for the browser to send this cookie along with cross-site requests.
type StdHandlerFunc ¶
type StdHandlerFunc = stdlib.HandlerFunc
HandlerFunc is a re-export of net/http.HandlerFunc. Note: renamed to StdHandlerFunc to avoid conflict with router.HandlerFunc.