Documentation
¶
Index ¶
- Constants
- Variables
- func StatusMessage(status int) string
- type Client
- type Config
- type Cookie
- type Ctx
- func (c *Ctx) BindJSON(v interface{}) error
- func (c *Ctx) BindParam(key string, v interface{}) error
- func (c *Ctx) BindQuery(key string, v interface{}) error
- func (c *Ctx) Context() context.Context
- func (c *Ctx) DeleteCookie(names ...string) *Ctx
- func (c *Ctx) FormFile(key string) (multipart.File, *multipart.FileHeader, error)
- func (c *Ctx) Header(key string) string
- func (c *Ctx) IP() string
- func (c *Ctx) IPs() []string
- func (c *Ctx) JSON(data interface{}, status ...int) error
- func (c *Ctx) Locals(key string, value ...interface{}) interface{}
- func (c *Ctx) MultipartForm() *multipart.Form
- func (c *Ctx) MultipartFormValue(key string) string
- func (c *Ctx) MultipartReader(key string) (*multipart.Reader, error)
- func (c *Ctx) Next() error
- func (c *Ctx) Params(key string) string
- func (c *Ctx) ParamsInt(key string) (int, error)
- func (c *Ctx) Query(key string) string
- func (c *Ctx) ReadCookie(name string) (*Cookie, error)
- func (c *Ctx) RemoteAddr() string
- func (c *Ctx) Render(name string, data interface{}, status ...int) error
- func (c *Ctx) SaveFile(fh *multipart.FileHeader, path ...string) error
- func (c *Ctx) SendFile(filePath string) error
- func (c *Ctx) SendStatus(status int) error
- func (c *Ctx) SendString(body string) error
- func (c *Ctx) Set(key string, val interface{}) *Ctx
- func (c *Ctx) SetCookie(cookies ...Cookie) *Ctx
- func (c *Ctx) Status(status int) *Ctx
- func (c *Ctx) StreamFile(filePath string) error
- type Group
- func (g *Group) AddRoute(method, path string, handlers ...Handler)
- func (g *Group) Delete(path string, handlers ...Handler)
- func (g *Group) Get(path string, handlers ...Handler)
- func (g *Group) Group(prefix string, middlewares ...Middleware) *Group
- func (g *Group) Options(path string, handlers ...Handler)
- func (g *Group) Patch(path string, handlers ...Handler)
- func (g *Group) Post(path string, handlers ...Handler)
- func (g *Group) Put(path string, handlers ...Handler)
- type Handler
- type JSONMarshal
- type JSONUnmarshal
- type Middleware
- type Reloader
- type Request
- type Route
- type Router
- type SameSite
- type Server
- func (server *Server) AddRoute(method, path string, handlers ...Handler)
- func (server *Server) AddShutdownHook(hooks ...func())
- func (server *Server) Delete(path string, handlers ...Handler)
- func (server *Server) Get(path string, handlers ...Handler)
- func (server *Server) Group(prefix string, middlewares ...Middleware) *Group
- func (server *Server) Options(path string, handlers ...Handler)
- func (server *Server) Patch(path string, handlers ...Handler)
- func (server *Server) Post(path string, handlers ...Handler)
- func (server *Server) Put(path string, handlers ...Handler)
- func (server *Server) RebuildViews() error
- func (server *Server) ReloadTemplates() bool
- func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (server *Server) ServeShutDown(ctx context.Context, hooks ...func()) error
- func (server *Server) SetEngine(engine ViewEngine)
- func (server *Server) Start(address string) error
- func (server *Server) Use(middleware Middleware)
- func (server *Server) ViewPath() string
- type TLSConfig
- type ViewEngine
Constants ¶
const ( MethodGet = "GET" MethodPost = "POST" MethodPut = "PUT" MethodDelete = "DELETE" MethodPatch = "PATCH" MethodHead = "HEAD" MethodOptions = "OPTIONS" )
const (
DefaultBodyLimit = 5 * 1024 * 1024
)
Variables ¶
var ( ErrParse = errors.New("bind: cannot parse") ErrConvert = errors.New("bind: cannot convert") ErrType = errors.New("bind: unexpected type") ErrPtr = errors.New("bind: destination must be a pointer") ErrValidation = errors.New("bind: validation failed") )
var ( ErrURIRequired = errors.New("uri is required") ErrMethodRequired = errors.New("method is required") ErrResponseIsNil = errors.New("response is nil") )
Common errors if you want to use the client and its methods
var DefaultMethods = []string{ MethodGet, MethodHead, MethodPost, MethodPut, MethodDelete, MethodPatch, MethodOptions, }
DefaultMethods is the set of HTTP methods Pine accepts by default.
var (
ErrFileName = errors.New("could not determine file name")
)
Functions ¶
func StatusMessage ¶
StatusMessage returns the text for a given HTTP status code.
Types ¶
type Client ¶ added in v1.0.5
Client is a wrapper around the http.Client Has methods for setting the request URI, method, headers, and body
Ideally you want to use a client only once, however if you need to use multiple instances of the same client, start by creating a new client and then call the SetRequestURI, SetMethod, SetHeaders, and SetBody methods
Note that if you call SendRequest, the old response will be overwritten If you would like to store the response, call ReadResponse and store the response in a variable of your own choosing
func NewClient ¶ added in v1.0.5
func NewClient() *Client
Call this to create a new client You can then call SetRequestURI, SetMethod, SetHeaders, and SetBody after creating the client
func NewClientWithTimeout ¶ added in v1.0.5
NewClientWithTimeout returns a new client with a timeout
func (*Client) ReadResponse ¶ added in v1.0.5
Call this method to get the response from the request Note that after calling this method the old reponse will be discarded
Attempts to read the response after calling this method more than once will return an error
func (*Client) SendRequest ¶ added in v1.0.5
Call this method only if you have already set the request URI and method
body and headers are optional
It returns an error if the request URI or method is not set Please note that if you call SendRequest, the old response will be overwritten
func (*Client) SetTLSVerification ¶ added in v1.0.5
Use this method to skip TLS verification This can be useful if the api you are calling has outdated TLS certificates
type Config ¶
type Config struct {
// Defines the body limit for a request.
// -1 will decline any body size
//
// Default: 5 * 1024 * 1024
BodyLimit int64
// Default: 5 Seconds
ReadTimeout time.Duration
// Default: 5 Seconds
WriteTimeout time.Duration
// Default: false
DisableKeepAlive bool
// Default: json.Marshal
JSONEncoder JSONMarshal
// Default: json.Unmarshal
JSONDecoder JSONUnmarshal
// RequestMethods provides customizability for HTTP methods.
//
// Default: DefaultMethods
RequestMethods []string
// UploadPath is the path where uploaded files will be saved.
//
// Default: ./uploads
UploadPath string
// StaticPath is the path where static files will be served.
//
// Default: "static"
StaticPath string
// ViewPath is the path where view files will be served.
//
// Default: "views"
ViewPath string
// Engine is the template engine to use.
//
// Default: html
Engine string
// ReloadTemplates re-parses template files from disk on every render call.
// Useful during development so edits are visible without a server restart.
// Leave false in production — templates are parsed once at startup.
//
// Default: false
ReloadTemplates bool
// TLSConfig is the configuration for TLS.
TLSConfig TLSConfig
}
Config is a struct holding the server settings.
type Cookie ¶
type Cookie struct {
Name string
Value string
Path string
Domain string
Expires time.Time
RawExpires string
MaxAge int
Secure bool
HttpOnly bool
SameSite SameSite
Raw bool
Unparsed []string
}
Cookie defines the structure of an HTTP cookie.
type Ctx ¶
type Ctx struct {
Server *Server // Reference to *Server
Method string // HTTP method
BaseURI string // HTTP base uri
Request *http.Request // HTTP request
Response *responseWriterWrapper // HTTP response writer
// contains filtered or unexported fields
}
func (*Ctx) BindJSON ¶ added in v1.0.5
BindJSON binds the request body to the given interface. You can use this to validate the request body without adding further logic to your handlers.
Tested with nested JSON objects and arrays If you notice any errors, please open an issue on Github and I will fix it right away
func (*Ctx) DeleteCookie ¶
DeleteCookie expires the named cookies by setting Max-Age=-1.
func (*Ctx) MultipartForm ¶ added in v1.0.7
func (*Ctx) MultipartFormValue ¶ added in v1.0.7
func (*Ctx) MultipartReader ¶ added in v1.0.7
func (*Ctx) Next ¶
Next is intended for use inside handler chains registered via AddRoute. It advances the handler index but the actual dispatch loop in ServeHTTP already calls all handlers sequentially; Next() is here for API parity.
func (*Ctx) ReadCookie ¶
ReadCookie reads a named cookie from the request.
func (*Ctx) RemoteAddr ¶ added in v1.1.3
RemoteAddr returns the raw remote address of the client.
func (*Ctx) Render ¶ added in v1.1.4
Render executes a named template and writes the result to the response.
name is the template filename relative to ViewPath, e.g. "index.html" or "admin/dashboard.html". data is the dot value passed to the template. status defaults to 200 when omitted.
The output is buffered so a template error never sends a partial response. When render.LiveReload() is active the engine automatically appends the live-reload <script> — no manual template changes needed.
func (*Ctx) SaveFile ¶ added in v1.0.7
func (c *Ctx) SaveFile(fh *multipart.FileHeader, path ...string) error
SaveFile saves the file to the specified path or the default upload path if no path is specified
func (*Ctx) SendStatus ¶
SendStatus writes the status code and its text body.
func (*Ctx) SendString ¶
SendString writes a plain-text string to the response.
func (*Ctx) StreamFile ¶ added in v1.0.7
type Group ¶ added in v1.1.6
type Group struct {
// contains filtered or unexported fields
}
Group is a route group with a shared URL prefix and optional scoped middleware. Routes registered on a Group inherit the prefix and middleware of all parent groups. Create one via Server.Group or nest with Group.Group.
func (*Group) AddRoute ¶ added in v1.1.6
AddRoute registers a route under the group's prefix with group middleware applied.
func (*Group) Group ¶ added in v1.1.6
func (g *Group) Group(prefix string, middlewares ...Middleware) *Group
Group creates a sub-group rooted at prefix relative to this group's prefix. Middleware passed here is appended after the parent group's middleware, so execution order is: global → parent-group → sub-group → handler.
type JSONMarshal ¶
type JSONUnmarshal ¶
type Middleware ¶
type Reloader ¶ added in v1.1.4
type Reloader interface {
Rebuild() error
}
Reloader is an optional extension of ViewEngine. Engines that support hot-reloading implement this so RebuildViews() can re-parse templates from disk without restarting the server.
type Request ¶ added in v1.0.5
func (*Request) SetHeaders ¶ added in v1.0.5
Use this to set the headers of the request You can add as many headers as you want in a map
For example:
headers := map[string]string{
"X-API-KEY": "1234567890",
}
request.SetHeaders(headers)
func (*Request) SetMethod ¶ added in v1.0.5
Use this to set the method of the request
For example: request.SetMethod("GET")
func (*Request) SetRequestURI ¶ added in v1.0.5
Use this to set the url of the request
For example: request.SetRequestURI("https://example.com/api/v1/users")
type Route ¶
type Route struct {
Method string `json:"method"`
Path string `json:"path"`
Handlers []Handler `json:"-"`
}
Route holds all metadata for each registered handler.
type Router ¶ added in v1.1.4
type Router struct {
// contains filtered or unexported fields
}
Router is a radix-tree HTTP router. Insert registers a route; Search looks one up.
func (*Router) Insert ¶ added in v1.1.4
Insert registers handlers for a given HTTP method and path. Segments starting with ':' are treated as named parameters. A trailing '/*' is treated as a catch-all wildcard.
type SameSite ¶
type SameSite int
SameSite controls the SameSite cookie attribute. Use SameSiteUnset (0) to omit the attribute.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) AddShutdownHook ¶ added in v1.1.4
func (server *Server) AddShutdownHook(hooks ...func())
AddShutdownHook registers fn to be called during graceful shutdown. This lets packages like render register cleanup callbacks (e.g. stopping the file watcher) without requiring the caller to wire them up manually.
func (*Server) Group ¶ added in v1.1.6
func (server *Server) Group(prefix string, middlewares ...Middleware) *Group
Group returns a route group rooted at prefix. Middleware passed here applies to every route registered on the group or its sub-groups, but not to routes registered directly on the server.
func (*Server) RebuildViews ¶ added in v1.1.4
RebuildViews re-parses all template files from disk by delegating to the engine's Rebuild() method. Called by render.LiveReload() on every detected template file change. No-op when the engine does not implement Reloader.
func (*Server) ReloadTemplates ¶ added in v1.1.4
ReloadTemplates reports whether the server was configured with hot-reloading enabled. render.Setup() reads this to decide whether to start LiveReload.
func (*Server) ServeShutDown ¶
ServeShutDown gracefully shuts the server down.
func (*Server) SetEngine ¶ added in v1.1.4
func (server *Server) SetEngine(engine ViewEngine)
SetEngine installs a template engine on the server. Call this (or render.Setup()) before registering routes that use Ctx.Render().
func (*Server) Use ¶
func (server *Server) Use(middleware Middleware)
Use appends a middleware and rebuilds the router so every existing route is re-wrapped from its original handlers — preventing double-wrapping.
type ViewEngine ¶ added in v1.1.4
ViewEngine is the interface every template backend must implement. Register an engine with Server.SetEngine(); the render package provides the built-in HTML engine via render.Setup().
Directories
¶
| Path | Synopsis |
|---|---|
|
Examples
|
|
|
BindExample
command
|
|
|
CacheExample
command
|
|
|
CorsExample/server
command
|
|
|
CronExample
command
|
|
|
FileExample
command
|
|
|
FullStackApp
command
FullStackApp demonstrates Pine's HTML renderer alongside JSON API routes and static file serving.
|
FullStackApp demonstrates Pine's HTML renderer alongside JSON API routes and static file serving. |
|
GroupExample
command
|
|
|
HandlersWithNext
command
|
|
|
RateLimitExample
command
|
|
|
RunningInGoRoutine
command
|
|
|
SimpleRender
command
|
|
|
SimpleServer
command
|
|
|
UsingPineClient/ClientServer
command
|
|
|
UsingPineClient/MainServer
command
|
|
|
Websocket
command
|
|
|
Package render provides Pine's built-in HTML template engine.
|
Package render provides Pine's built-in HTML template engine. |
|
Pine's websocket package is a websocket server that supports multiple channels This feature is experimental and may change in the future.
|
Pine's websocket package is a websocket server that supports multiple channels This feature is experimental and may change in the future. |