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 (c *Context) Body() ([]byte, error)
- func (c *Context) ClientIP() string
- func (c *Context) Context() context.Context
- func (c *Context) Cookie(name string) (*http.Cookie, error)
- func (c *Context) Data(status int, contentType string, data []byte) error
- func (c *Context) DecodeJSON(data interface{}) error
- func (c *Context) DecodeXML(data interface{}) error
- func (c *Context) DefaultQuery(name, defaultValue string) string
- func (c *Context) DownloadFile(filepath string, downloadName string) error
- func (c *Context) File(name string) (*multipart.FileHeader, error)
- func (c *Context) Form(name string) string
- func (c *Context) GetHeader(key string) string
- func (c *Context) HTML(status int, html string) error
- func (c *Context) JSON(status int, data interface{}) error
- func (c *Context) Method() string
- func (c *Context) NoContent(status int)
- func (c *Context) Param(name string) string
- func (c *Context) Path() string
- func (c *Context) Query(name string) string
- func (c *Context) Redirect(status int, url string)
- func (c *Context) SaveFile(file *multipart.FileHeader, path string) error
- func (c *Context) ServeStatic(dir string) error
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) SetHeader(key, value string)
- func (c *Context) StreamFile(filepath string) error
- func (c *Context) Text(status int, format string, values ...interface{}) error
- func (c *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 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 (c *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) Param ¶
Param gets a path parameter by name. For example, this returns the value of id from /users/{id}
func (*Context) SaveFile ¶
func (c *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 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 ¶
WithReadTimeout sets the server graceful shutdown timeout
func WithReadTimeout ¶
WithReadTimeout sets the server read timeout
func WithWriteTimeout ¶
WithReadTimeout 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
|
|