Documentation ¶
Index ¶
- Constants
- func ChiPathParamFunc(r *http.Request, values map[string]interface{})
- type App
- func (a *App) Delete(path string, handle func(c *Context) error)
- func (a *App) FileServer(path string, dir string)
- func (a *App) Get(path string, handle func(c *Context) error)
- func (a *App) Head(path string, handle func(c *Context) error)
- func (a *App) Namespace(pattern string, fn func(app *App)) *App
- func (a *App) Options(path string, handle func(c *Context) error)
- func (a *App) Patch(path string, handle func(c *Context) error)
- func (a *App) Post(path string, handle func(c *Context) error)
- func (a *App) Put(path string, handle func(c *Context) error)
- func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (a *App) SetErrorHandler(fn func(c *Context, err error))
- func (a *App) Start(addr string) error
- func (a *App) Use(middleware ...MiddlewareFunc)
- func (a *App) UseStd(middleware ...func(http.Handler) http.Handler)
- type Context
- func (c *Context) FormValue(name string) string
- func (c *Context) GetIP() string
- func (c *Context) JSON(code int, v interface{}) error
- func (c *Context) NoContent() error
- func (c *Context) Params(v interface{}) error
- func (c *Context) PathParam(name string) string
- func (c *Context) QueryParam(name string) string
- func (c *Context) Redirect(url string) error
- func (c *Context) Render(name string, data map[string]interface{}, opts ...render.RenderOptions) error
- func (c *Context) RenderToBytes(name string, data map[string]interface{}, opts ...render.RenderOptions) []byte
- func (c *Context) Request() *http.Request
- func (c *Context) Response() http.ResponseWriter
- func (c *Context) String(code int, s string) error
- type ContextFlash
- type ContextI18N
- type ContextSession
- type ContextValues
- type MiddlewareFunc
- type Option
Constants ¶
const Version = "v0.4.0"
Version is the version of the Seatbelt package.
Variables ¶
This section is empty.
Functions ¶
func ChiPathParamFunc ¶
ChiPathParamFunc extracts path parameters from the given HTTP request using the github.com/go-chi/chi router.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
An App contains the data necessary to start and run an application.
An App acts as a router. You must provide your own HTTP server in order to start it in the application, i.e.:
app := seatbelt.New() http.ListenAndServe(":3000", app)
Or,
app := seatbelt.New() srv := &http.Server{ Handler: app, } srv.ListenAndServe()
func (*App) FileServer ¶
FileServer serves the contents of the given directory at the given path.
func (*App) Namespace ¶
Namespace creates a new *seatbelt.App with an empty middleware stack and mounts it on the `pattern` as a subrouter.
func (*App) ServeHTTP ¶
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP makes the Seatbelt application implement the http.Handler interface.
func (*App) SetErrorHandler ¶
SetErrorHandler allows you to set a custom error handler that runs when an error is returned from an HTTP handler.
func (*App) Start ¶
Start is a convenience method for starting the application server with a default *http.Server.
Start should not be used in production, as the standard library's default HTTP server is not suitable for production use due to a lack of timeouts, etc.
Production applications should create their own *http.Server, and pass the *seatbelt.App to that *http.Server's `Handler`.
func (*App) Use ¶
func (a *App) Use(middleware ...MiddlewareFunc)
Use registers Seatbelt HTTP middleware on the application.
type Context ¶
type Context struct { I18N *ContextI18N Flash *ContextFlash Values *ContextValues Session *ContextSession // contains filtered or unexported fields }
func (*Context) GetIP ¶
func (c *Context) GetIP() string
GetIP attempts to return the request's IP address, first by checking the `X-Real-Ip` header, then the `X-Forwarded-For` header, and finally falling back to the request's `RemoteAddr`.
func (*Context) NoContent ¶
func (c *Context) NoContent() error
NoContent sends a 204 No Content HTTP response. It will always return a nil error.
func (*Context) QueryParam ¶
QueryParam returns the URL query parameter with the given name.
func (*Context) Render ¶
func (c *Context) Render(name string, data map[string]interface{}, opts ...render.RenderOptions) error
Render renders an HTML template.
If there are any request-scoped values present on the request, they will be merged with the given data, with the data taking precendence in case of key collisions.
Render will never return an error, and only has the function signature as a convenience for writing shorter handlers, for example,
func ShowNewUser(c *seatbelt.Context) error { return c.Render("users/new", nil) }
func (*Context) RenderToBytes ¶ added in v0.3.1
func (c *Context) RenderToBytes(name string, data map[string]interface{}, opts ...render.RenderOptions) []byte
RenderToBytes is the same as Render, but returns the rendered template as a byte slice instead of writing diredtly to the response writer.
func (*Context) Request ¶
Request returns the underlying *http.Request belonging to the current request context.
func (*Context) Response ¶
func (c *Context) Response() http.ResponseWriter
Response returns the underlying http.ResponseWriter belonging to the current request context.
type ContextFlash ¶
type ContextFlash context
func (*ContextFlash) Add ¶
func (c *ContextFlash) Add(key string, value interface{})
Flash adds a flash message on a request.
func (*ContextFlash) List ¶
func (c *ContextFlash) List() map[string]interface{}
List returns all flash messages, clearing all saved flashes.
type ContextI18N ¶
type ContextI18N context
type ContextSession ¶
type ContextSession context
func (*ContextSession) Delete ¶
func (c *ContextSession) Delete(key string) interface{}
Delete deletes the session data with the given key. The deleted session data is returned.
func (*ContextSession) Get ¶
func (c *ContextSession) Get(key string) interface{}
Get returns the value associated with the given key in the request session.
func (*ContextSession) List ¶
func (c *ContextSession) List() map[string]interface{}
List returns all key value pairs of session data from the given request.
func (*ContextSession) Reset ¶
func (c *ContextSession) Reset()
Reset deletes all values from the session data.
func (*ContextSession) Set ¶
func (c *ContextSession) Set(key string, value interface{})
Set sets or updates the given value on the session.
type ContextValues ¶
type ContextValues context
func (*ContextValues) Delete ¶
func (c *ContextValues) Delete(key string)
Delete deletes the given request-scoped value.
func (*ContextValues) Get ¶
func (c *ContextValues) Get(key string) any
Get returns the request-scoped value with the given key.
func (*ContextValues) List ¶
func (c *ContextValues) List() map[string]any
List returns all request-scoped values.
func (*ContextValues) Set ¶
func (c *ContextValues) Set(key string, value any)
Set sets the given key value pair on the request. These values are passed to every HTML template by merging them with the given `data`.
type MiddlewareFunc ¶
MiddlewareFunc is the type alias for Seatbelt middleware.
type Option ¶
type Option struct { // The directory containing your HTML templates. TemplateDir string // The directory containing your i18n data. LocaleDir string // The signing key for the session cookie store. SigningKey string // The session name for the session cookie. Default is "_session". SessionName string // The MaxAge for the session cookie. Default is 365 days. Pass -1 for no // max age. SessionMaxAge int // Request-contextual HTML functions. Funcs func(w http.ResponseWriter, r *http.Request) template.FuncMap // Whether or not to reload templates on each request. Reload bool // SkipServeFiles does not automatically serve static files from the // project's /public directory when set to true. Default is false. SkipServeFiles bool // SkipCSRFPaths is used to skip the CSRF validation to POST, PUT, PATCH, // DELETE, etc requests to paths that match one of the given paths. SkipCSRFPaths []string }
An Option is used to configure a Seatbelt application.
Directories ¶
Path | Synopsis |
---|---|
example
|
|
Package handler provides convenience methods for working with HTTP requests.
|
Package handler provides convenience methods for working with HTTP requests. |
Package render provides functionality for rendering HTML when building web applications.
|
Package render provides functionality for rendering HTML when building web applications. |