seatbelt

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: MIT Imports: 18 Imported by: 0

README

Seatbelt

A web framework for Go that balances upfront productivity with long term maintability.

Usage

Don't use this yet. It's too early.

Notes
  1. Static assets are always served from public.

Documentation

Index

Constants

View Source
const Version = "v0.4.0"

Version is the version of the Seatbelt package.

Variables

This section is empty.

Functions

func ChiPathParamFunc

func ChiPathParamFunc(r *http.Request, values map[string]interface{})

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 New

func New(opts ...Option) *App

New returns a new instance of a Seatbelt application.

func (*App) Delete

func (a *App) Delete(path string, handle func(c *Context) error)

Delete routes DELETE requests to the given path.

func (*App) FileServer

func (a *App) FileServer(path string, dir string)

FileServer serves the contents of the given directory at the given path.

func (*App) Get

func (a *App) Get(path string, handle func(c *Context) error)

Get routes GET requests to the given path.

func (*App) Head

func (a *App) Head(path string, handle func(c *Context) error)

Head routes HEAD requests to the given path.

func (*App) Namespace

func (a *App) Namespace(pattern string, fn func(app *App)) *App

Namespace creates a new *seatbelt.App with an empty middleware stack and mounts it on the `pattern` as a subrouter.

func (*App) Options

func (a *App) Options(path string, handle func(c *Context) error)

Options routes OPTIONS requests to the given path.

func (*App) Patch

func (a *App) Patch(path string, handle func(c *Context) error)

Patch routes PATCH requests to the given path.

func (*App) Post

func (a *App) Post(path string, handle func(c *Context) error)

Post routes POST requests to the given path.

func (*App) Put

func (a *App) Put(path string, handle func(c *Context) error)

Put routes PUT requests to the given path.

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

func (a *App) SetErrorHandler(fn func(c *Context, err error))

SetErrorHandler allows you to set a custom error handler that runs when an error is returned from an HTTP handler.

func (*App) Start

func (a *App) Start(addr string) error

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.

func (*App) UseStd

func (a *App) UseStd(middleware ...func(http.Handler) http.Handler)

UseStd registers standard 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) FormValue

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

FormValue returns the form value with the given name.

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) JSON

func (c *Context) JSON(code int, v interface{}) error

JSON renders a JSON response with the given status code and data.

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) Params

func (c *Context) Params(v interface{}) error

func (*Context) PathParam

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

PathParam returns the path param with the given name.

func (*Context) QueryParam

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

QueryParam returns the URL query parameter with the given name.

func (*Context) Redirect

func (c *Context) Redirect(url string) error

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

func (c *Context) Request() *http.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.

func (*Context) String

func (c *Context) String(code int, s string) error

String sends a string response with the given status code.

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

func (*ContextI18N) T

func (c *ContextI18N) T(id string, data map[string]any, count ...int) string

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

type MiddlewareFunc func(fn func(ctx *Context) error) func(*Context) error

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
cmd
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.

Jump to

Keyboard shortcuts

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