Documentation
¶
Overview ¶
Package mach provides a lightweight web framework for Go.
Mach is built on Go 1.22's enhanced net/http router with zero dependencies. It provides a simple, intuitive API for building web applications while leveraging the standard library's performance and reliability.
Example usage:
app := mach.Default()
app.GET("/", func(c *mach.Context) {
c.JSON(200, map[string]string{"message": "Hello, Mach!"})
})
app.Run(":8080")
Features:
- Go 1.22+ native routing with method matching and path parameters
- Standard http.Handler middleware pattern
- Route groups for organization
- Zero dependencies
Index ¶
- Variables
- type App
- func (app *App) DELETE(path string, handler HandlerFunc)
- func (app *App) GET(path string, handler HandlerFunc)
- func (app *App) Group(prefix string, middlewares ...MiddlewareFunc) *Group
- func (app *App) HEAD(path string, handler HandlerFunc)
- func (app *App) OPTIONS(path string, handler HandlerFunc)
- func (app *App) PATCH(path string, handler HandlerFunc)
- func (app *App) POST(path string, handler HandlerFunc)
- func (app *App) PUT(path string, handler HandlerFunc)
- func (app *App) Route(method, path string, handler HandlerFunc)
- func (app *App) Run(addr string, opts ...RunOption) error
- func (app *App) RunTLS(addr, certFile, keyFile string, opts ...RunOption) error
- func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (app *App) Static(prefix, dir string)
- func (app *App) Use(middlewares ...MiddlewareFunc)
- type CORSConfig
- type Context
- func (ctx *Context) Body() ([]byte, error)
- func (ctx *Context) ClientIP() string
- func (ctx *Context) Context() context.Context
- func (ctx *Context) Cookie(name string) (*http.Cookie, error)
- func (ctx *Context) Data(status int, contentType string, data []byte) error
- func (ctx *Context) DecodeJSON(data interface{}) error
- func (ctx *Context) DecodeXML(data interface{}) error
- func (ctx *Context) DefaultQuery(name, defaultValue string) string
- func (ctx *Context) DownloadFile(filepath string, downloadName string) error
- func (ctx *Context) File(name string) (*multipart.FileHeader, error)
- func (ctx *Context) Form(name string) string
- func (ctx *Context) GetHeader(key string) string
- func (ctx *Context) GetResponseHeader(key string) string
- func (ctx *Context) HTML(status int, html string) error
- func (ctx *Context) HTMX() *HTMX
- func (ctx *Context) JSON(status int, data interface{}) error
- func (ctx *Context) Method() string
- func (ctx *Context) NoContent(status int)
- func (ctx *Context) Param(name string) string
- func (ctx *Context) Path() string
- func (ctx *Context) Query(name string) string
- func (ctx *Context) Redirect(status int, url string)
- func (ctx *Context) SaveFile(file *multipart.FileHeader, path string) error
- func (ctx *Context) ServeStatic(dir string) error
- func (ctx *Context) SetCookie(cookie *http.Cookie)
- func (ctx *Context) SetHeader(key, value string)
- func (ctx *Context) StreamFile(filepath string) error
- func (ctx *Context) Text(status int, format string, values ...interface{}) error
- func (ctx *Context) XML(status int, data interface{}) error
- type Group
- func (g *Group) DELETE(path string, handler HandlerFunc)
- func (g *Group) GET(path string, handler HandlerFunc)
- func (g *Group) Group(prefix string, middlewares ...MiddlewareFunc) *Group
- func (g *Group) HEAD(path string, handler HandlerFunc)
- func (g *Group) OPTIONS(path string, handler HandlerFunc)
- func (g *Group) PATCH(path string, handler HandlerFunc)
- func (g *Group) POST(path string, handler HandlerFunc)
- func (g *Group) PUT(path string, handler HandlerFunc)
- func (g *Group) Use(middlewares ...MiddlewareFunc)
- type HTMX
- func (h *HTMX) CurrentURL() string
- func (h *HTMX) IsBoosted() bool
- func (h *HTMX) IsHTMX() bool
- func (h *HTMX) IsHistoryRestoreRequest() bool
- func (h *HTMX) Location(url string)
- func (h *HTMX) Prompt() string
- func (h *HTMX) PushURL(url string)
- func (h *HTMX) Redirect(url string)
- func (h *HTMX) Refresh()
- func (h *HTMX) ReplaceURL(url string)
- func (h *HTMX) Reselect(selector string)
- func (h *HTMX) Reswap(swap string)
- func (h *HTMX) Retarget(selector string)
- func (h *HTMX) Target() string
- func (h *HTMX) Trigger() string
- func (h *HTMX) TriggerAfterSettleResponse(events any)
- func (h *HTMX) TriggerAfterSwapResponse(events any)
- func (h *HTMX) TriggerName() string
- func (h *HTMX) TriggerResponse(events any)
- type HandlerFunc
- type MiddlewareFunc
- type Option
- type RunOption
Constants ¶
This section is empty.
Variables ¶
var (
ErrEmptyRequestBody = errors.New("request body is empty")
)
Functions ¶
This section is empty.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the main application instance.
func (*App) DELETE ¶
func (app *App) DELETE(path string, handler HandlerFunc)
DELETE registers a DELETE route.
func (*App) Group ¶
func (app *App) Group(prefix string, middlewares ...MiddlewareFunc) *Group
Group creates a route group with common prefix and middleware.
func (*App) HEAD ¶
func (app *App) HEAD(path string, handler HandlerFunc)
HEAD registers a HEAD route.
func (*App) OPTIONS ¶
func (app *App) OPTIONS(path string, handler HandlerFunc)
OPTIONS registers a OPTIONS route.
func (*App) PATCH ¶
func (app *App) PATCH(path string, handler HandlerFunc)
PATCH registers a PATCH route.
func (*App) POST ¶
func (app *App) POST(path string, handler HandlerFunc)
POST registers a POST route.
func (*App) Route ¶
func (app *App) Route(method, path string, handler HandlerFunc)
Route registers a handler for the given method and path.
func (*App) ServeHTTP ¶
func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the handler for serving each request.
func (*App) Use ¶
func (app *App) Use(middlewares ...MiddlewareFunc)
Use adds a global middleware to the application.
type CORSConfig ¶
type Context ¶
type Context struct {
Request *http.Request
Response http.ResponseWriter
IsFormParsed bool
// contains filtered or unexported fields
}
Context adds helpful methods to the ongoing request.
func (*Context) ClientIP ¶
ClientIP returns the client IP address. Use this if you trust request headers passed to the server (ie: reverse proxy sits before server) else use c.Request.RemoteAddr().
func (*Context) DecodeJSON ¶
DecodeJSON decodes a request body into a struct.
func (*Context) DefaultQuery ¶
DefaultQuery gets query param with default value.
func (*Context) DownloadFile ¶
DownloadFile sends a downloadable file response with the specified filename.
func (*Context) File ¶
func (ctx *Context) File(name string) (*multipart.FileHeader, error)
File gets an uploaded file by key name. The file header containing the file is returned.
func (*Context) GetResponseHeader ¶
GetResponseHeader retrieves a response header by key.
func (*Context) Param ¶
Param gets a path parameter by name. For example, this returns the value of id from /users/{id}.
func (*Context) SaveFile ¶
func (ctx *Context) SaveFile(file *multipart.FileHeader, path string) error
SaveFile saves an uploaded file to the specified destination path.
func (*Context) ServeStatic ¶
func (*Context) StreamFile ¶
StreamFile streams the content of a file in chunks to the client.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a route group with common named prefix.
func (*Group) DELETE ¶
func (g *Group) DELETE(path string, handler HandlerFunc)
DELETE registers a DELETE route for the group.
func (*Group) GET ¶
func (g *Group) GET(path string, handler HandlerFunc)
GET registers a GET route for the group.
func (*Group) Group ¶
func (g *Group) Group(prefix string, middlewares ...MiddlewareFunc) *Group
Group creates a sub-group. Global middlewares come first in the chain.
func (*Group) HEAD ¶
func (g *Group) HEAD(path string, handler HandlerFunc)
HEAD registers a HEAD route for the group.
func (*Group) OPTIONS ¶
func (g *Group) OPTIONS(path string, handler HandlerFunc)
OPTIONS registers a OPTIONS route for the group.
func (*Group) PATCH ¶
func (g *Group) PATCH(path string, handler HandlerFunc)
PATCH registers a PATCH route for the group.
func (*Group) POST ¶
func (g *Group) POST(path string, handler HandlerFunc)
POST registers a POST route for the group.
func (*Group) PUT ¶
func (g *Group) PUT(path string, handler HandlerFunc)
PUT registers a PUT route for the group.
func (*Group) Use ¶
func (g *Group) Use(middlewares ...MiddlewareFunc)
Use registers middlewares to the group.
type HTMX ¶
type HTMX struct {
// contains filtered or unexported fields
}
HTMX helper provides methods for interacting with HTMX headers.
func (*HTMX) CurrentURL ¶
CurrentURL returns the current URL of the browser.
func (*HTMX) IsHistoryRestoreRequest ¶
IsHistoryRestoreRequest returns true if the request is for history restoration after a miss in the local history cache.
func (*HTMX) Location ¶
Location allows you to do a client-side redirect that does not do a full page reload.
func (*HTMX) Refresh ¶
func (h *HTMX) Refresh()
Refresh triggers a client side full refresh of the page.
func (*HTMX) ReplaceURL ¶
ReplaceURL replaces the current URL in the browser location bar.
func (*HTMX) Reselect ¶
Reselect allows you to choose which part of the response is used to be swapped in.
func (*HTMX) Retarget ¶
Retarget allows you to target a different element on the page of the response.
func (*HTMX) TriggerAfterSettleResponse ¶
TriggerAfterSettleResponse allows you to trigger client side events after the settle step.
func (*HTMX) TriggerAfterSwapResponse ¶
TriggerAfterSwapResponse allows you to trigger client side events after the swap step.
func (*HTMX) TriggerName ¶
TriggerName returns the name of the triggered element if it exists.
func (*HTMX) TriggerResponse ¶
TriggerResponse allows you to trigger client side events. If events is a string, it sets it as the HX-Trigger header. If events is a map or struct, it serializes it to JSON and sets it as the HX-Trigger header. To avoid overwriting previously set events, this method appends to the existing HX-Trigger header if it exists.
type MiddlewareFunc ¶
MiddlewareFunc is the middleware signature.
func CORS ¶
func CORS(allowOrigins []string) MiddlewareFunc
func CORSWithConfig ¶
func CORSWithConfig(config CORSConfig) MiddlewareFunc
func Logger ¶
func Logger() MiddlewareFunc
func Recovery ¶
func Recovery() MiddlewareFunc
type RunOption ¶
type RunOption func(*serverConfig)
RunOption configures the server.
func WithGracefulShutdown ¶
WithGracefulShutdown sets the server graceful shutdown timeout.
func WithReadTimeout ¶
WithReadTimeout sets the server read timeout.
func WithWriteTimeout ¶
WithWriteTimeout sets the server write timeout.
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
basic
command
|
|
|
file-download
command
|
|
|
file-upload
command
|
|
|
middleware
command
|
|
|
nested-routes
command
|
|
|
rest-api
command
|
|