Documentation
¶
Overview ¶
Package inertia provides a Go web framework for building modern web applications using the Inertia.js approach.
Index ¶
- Constants
- Variables
- func Error(w http.ResponseWriter, err error, HTTPStatus int)
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(code int, err error)
- func (c *Context) AbortWithStatus(code int)
- func (c *Context) ClientIP() string
- func (c *Context) Data() map[string]any
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) DefaultPostForm(key, defaultValue string) string
- func (c *Context) DefaultQuery(key, defaultValue string) string
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Get(key string) (value any, exists bool)
- func (c *Context) GetHeader(key string) string
- func (c *Context) GetPostForm(key string) (string, bool)
- func (c *Context) GetPostFormArray(key string) (values []string, ok bool)
- func (c *Context) GetQuery(key string) (string, bool)
- func (c *Context) GetQueryArray(key string) (values []string, ok bool)
- func (c *Context) GetQueryMap(key string) (map[string]string, bool)
- func (c *Context) Header(key, value string)
- func (c *Context) IsAborted() bool
- func (c *Context) JSON(data any) error
- func (c *Context) Next()
- func (c *Context) PostForm(key string) (value string)
- func (c *Context) Query(key string) (value string)
- func (c *Context) QueryArray(key string) (values []string)
- func (c *Context) QueryMap(key string) (dicts map[string]string)
- func (c *Context) Render(view string) error
- func (c *Context) Set(key string, value any)
- func (c *Context) SetMeta(meta Meta)
- func (c *Context) Status(code int)
- func (c *Context) Value(key any) any
- func (c *Context) Write(b []byte) (int, error)
- type ContextKeyType
- type Engine
- func (e *Engine) ANY(path string, fn func(c *Context))
- func (e *Engine) Addr() string
- func (e *Engine) DELETE(path string, fn func(c *Context))
- func (e *Engine) DevAddr() string
- func (e *Engine) GET(path string, fn func(c *Context))
- func (e *Engine) HEAD(path string, fn func(c *Context))
- func (e *Engine) IsDevelopentMode() bool
- func (e *Engine) IsSSRMode() bool
- func (e *Engine) OPTIONS(path string, fn func(c *Context))
- func (e *Engine) PATCH(path string, fn func(c *Context))
- func (e *Engine) POST(path string, fn func(c *Context))
- func (e *Engine) PUT(path string, fn func(c *Context))
- func (e *Engine) Serve() error
- func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (e *Engine) StaticFS(path string, fs fs.FS)
- func (e *Engine) Use(middleware ...HandlerFunc)
- type ErrorHandlerFunc
- type HandlerFunc
- type Meta
- func (m *Meta) AddCustom(name, content string) *Meta
- func (m Meta) Clone() Meta
- func (m *Meta) SetAuthor(author string) *Meta
- func (m *Meta) SetDescription(description string) *Meta
- func (m *Meta) SetKeywords(keywords string) *Meta
- func (m *Meta) SetOpenGraph(og OpenGraph) *Meta
- func (m *Meta) SetTitle(title string) *Meta
- func (m *Meta) SetTwitterCard(twitter TwitterCard) *Meta
- func (m *Meta) ToHTML() string
- type Mode
- type OpenGraph
- type Option
- type Params
- func (p Params) Get(key string) string
- func (p Params) GetFloat32(key string) (float32, bool)
- func (p Params) GetFloat64(key string) (float64, bool)
- func (p Params) GetInt(key string) (int, bool)
- func (p Params) GetInt64(key string) (int64, bool)
- func (p Params) GetUint(key string) (uint, bool)
- func (p Params) GetUint64(key string) (uint64, bool)
- type ResponseWriter
- type TagFunc
- type TwitterCard
Constants ¶
const ContextKey = "_inertia/contextkey"
ContextKey is the key that a Context returns itself for.
Variables ¶
var ( ErrorHandlerMap = map[int]ErrorHandlerFunc{ http.StatusNotFound: func(w http.ResponseWriter, r *http.Request, err error) { http.NotFound(w, r) }, http.StatusForbidden: func(w http.ResponseWriter, r *http.Request, err error) { http.Error(w, "403 Forbidden", http.StatusForbidden) }, http.StatusInternalServerError: func(w http.ResponseWriter, r *http.Request, err error) { slog.Error(err.Error(), slog.Int("status", http.StatusInternalServerError), slog.String("method", r.Method), slog.String("path", r.URL.Path)) http.Error(w, "500 Internal Server Error", http.StatusInternalServerError) }, } )
Functions ¶
Types ¶
type Context ¶
type Context struct {
Meta Meta
Request *http.Request
Writer ResponseWriter
Params Params
// contains filtered or unexported fields
}
func MustFromContext ¶
func (*Context) Abort ¶
func (c *Context) Abort()
Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers for this request are not called.
func (*Context) AbortWithError ¶
func (*Context) AbortWithStatus ¶
AbortWithStatus calls `Abort()` and writes the headers with the specified status code.
func (*Context) Deadline ¶
Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
func (*Context) DefaultPostForm ¶
DefaultPostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns the specified defaultValue string. See: PostForm() and GetPostForm() for further information.
func (*Context) DefaultQuery ¶
DefaultQuery returns the keyed url query value if it exists, otherwise it returns the specified defaultValue string. See: Query() and GetQuery() for further information.
GET /?name=Manu&lastname=
c.DefaultQuery("name", "unknown") == "Manu"
c.DefaultQuery("id", "none") == "none"
c.DefaultQuery("lastname", "none") == ""
func (*Context) Done ¶
func (c *Context) Done() <-chan struct{}
Done returns nil (chan which will wait forever) when c.Request has no Context.
func (*Context) GetPostForm ¶
GetPostForm is like PostForm(key). It returns the specified key from a POST urlencoded form or multipart form when it exists `(value, true)` (even when the value is an empty string), otherwise it returns ("", false). For example, during a PATCH request to update the user's email:
[email protected] --> ("[email protected]", true) := GetPostForm("email") // set email to "[email protected]" email= --> ("", true) := GetPostForm("email") // set email to "" --> ("", false) := GetPostForm("email") // do nothing with email
func (*Context) GetPostFormArray ¶
GetPostFormArray returns a slice of strings for a given form key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetQuery ¶
GetQuery is like Query(), it returns the keyed url query value if it exists `(value, true)` (even when the value is an empty string), otherwise it returns `("", false)`. It is shortcut for `c.Request.URL.Query().Get(key)`
GET /?name=Manu&lastname=
("Manu", true) == c.GetQuery("name")
("", false) == c.GetQuery("id")
("", true) == c.GetQuery("lastname")
func (*Context) GetQueryArray ¶
GetQueryArray returns a slice of strings for a given query key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetQueryMap ¶
GetQueryMap returns a map for a given query key, plus a boolean value whether at least one value exists for the given key.
func (*Context) Header ¶
Header is an intelligent shortcut for c.Writer.Header().Set(key, value). It writes a header in the response. If value == "", this method removes the header `c.Writer.Header().Del(key)`
func (*Context) Next ¶
func (c *Context) Next()
Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler.
func (*Context) PostForm ¶
PostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns an empty string `("")`.
func (*Context) Query ¶
Query returns the keyed url query value if it exists, otherwise it returns an empty string `("")`. It is shortcut for `c.Request.URL.Query().Get(key)`
GET /path?id=1234&name=Manu&value=
c.Query("id") == "1234"
c.Query("name") == "Manu"
c.Query("value") == ""
c.Query("wtf") == ""
func (*Context) QueryArray ¶
QueryArray returns a slice of strings for a given query key. The length of the slice depends on the number of params with the given key.
type Engine ¶
type Engine struct {
MaxMultipartMemory int64
// contains filtered or unexported fields
}
func (*Engine) IsDevelopentMode ¶
IsDevelopentMode returns true if the engine is in development mode
func (*Engine) Use ¶
func (e *Engine) Use(middleware ...HandlerFunc)
Use allows to specify a middleware that should be executed for all the handlers in the group
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
type HandlerFunc ¶
type HandlerFunc func(c *Context)
func StaticFileServer ¶
func StaticFileServer(prefix string, root fs.FS) HandlerFunc
StaticFileServer returns an inertia.HandlerFunc that serves files from the given fs.FS.
func UseMeta ¶
func UseMeta(defaultMeta *Meta) HandlerFunc
UseMeta creates a middleware that initializes Meta in context
type Meta ¶
type Meta struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Keywords string `json:"keywords,omitempty"`
Author string `json:"author,omitempty"`
Viewport string `json:"viewport,omitempty"`
Charset string `json:"charset,omitempty"`
Custom map[string]string `json:"custom,omitempty"`
OpenGraph OpenGraph `json:"og,omitempty"`
Twitter TwitterCard `json:"twitter,omitempty"`
}
Meta represents HTML meta information
func (*Meta) SetDescription ¶
SetDescription sets the page description
func (*Meta) SetKeywords ¶
SetKeywords sets the page keywords
func (*Meta) SetOpenGraph ¶
SetOpenGraph sets Open Graph meta tags
func (*Meta) SetTwitterCard ¶
func (m *Meta) SetTwitterCard(twitter TwitterCard) *Meta
SetTwitterCard sets Twitter Card meta tags
type OpenGraph ¶
type OpenGraph struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Image string `json:"image,omitempty"`
URL string `json:"url,omitempty"`
Type string `json:"type,omitempty"`
SiteName string `json:"site_name,omitempty"`
}
OpenGraph represents Open Graph meta tags
type Option ¶
Option is an option parameter that modifies Inertia.
func WithDevAddr ¶
func WithErrorHandler ¶
func WithErrorHandler(status int, errorHandlerFn ErrorHandlerFunc) Option
func WithRootHTML ¶
type ResponseWriter ¶
type ResponseWriter interface {
http.ResponseWriter
http.Hijacker
http.Flusher
http.CloseNotifier
// Status returns the HTTP response status code of the current request.
Status() int
// Size returns the number of bytes already written into the response http body.
// See Written()
Size() int
// WriteString writes the string into the response body.
WriteString(string) (int, error)
// Written returns true if the response body was already written.
Written() bool
// WriteHeaderNow forces to write the http header (status code + headers).
WriteHeaderNow()
// Pusher get the http.Pusher for server push
Pusher() http.Pusher
}
ResponseWriter ...
type TwitterCard ¶
type TwitterCard struct {
Card string `json:"card,omitempty"`
Site string `json:"site,omitempty"`
Creator string `json:"creator,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Image string `json:"image,omitempty"`
}
TwitterCard represents Twitter Card meta tags