Documentation
¶
Index ¶
- Constants
- Variables
- func StatusMessage(status int) string
- type BackgroundTask
- type Config
- type Cookie
- type Ctx
- func (c *Ctx) Context() context.Context
- func (c *Ctx) DeleteCookie(names ...string) *Ctx
- func (c *Ctx) Header(key string) string
- func (c *Ctx) IP() string
- func (c *Ctx) JSON(data interface{}, status ...int) error
- func (c *Ctx) Locals(key interface{}, value ...interface{}) interface{}
- 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) 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
- type Handler
- type JSONMarshal
- type JSONUnmarshal
- type Middleware
- type Route
- type SameSite
- type Server
- func (server *Server) AddQueue(tasks ...BackgroundTask)
- func (server *Server) AddRoute(method, path string, handlers ...Handler)
- func (server *Server) Client() *http.Client
- func (server *Server) Delete(path string, handlers ...Handler)
- func (server *Server) Get(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) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (server *Server) ServeShutDown(ctx context.Context, hooks ...func()) error
- func (server *Server) Start(address string, CertFile, KeyFile string) error
- func (server *Server) Use(middleware Middleware)
Constants ¶
const ( MethodGet = "GET" MethodPost = "POST" MethodPut = "PUT" MethodDelete = "DELETE" MethodPatch = "PATCH" MethodHead = "HEAD" MethodOptions = "OPTIONS" )
Acceptable methods these are the default at the moment, more coming soon
const (
DefaultBodyLimit = 4 * 1024 * 1024
)
Variables ¶
var DefaultMethods = []string{ MethodGet, MethodPut, MethodDelete, MethodPatch, MethodHead, }
Default methods, more coming soon
Functions ¶
func StatusMessage ¶
You can send just the status message here
Types ¶
type BackgroundTask ¶
type BackgroundTask struct { Fn func() error Time time.Duration // contains filtered or unexported fields }
This is the structure of a background task you can use this to put whatever tasks you want to perform in the background as the server runs and Pine will take care of executing them in the background
time is optional and defaults to 5 minutes according to the server configuration
Fn is the function that will be executed It should always return an error as the error is what will be used to delete the task from the queue
type Config ¶
type Config struct { //defines the body limit for a request. // -1 will decline any body size // // Default: 4 * 1024 * 1024 // Increase this to accept larger files BodyLimit int `json:"body_limit"` // The amount of time allowed to read the full request including body. // It is reset after the request handler has returned. // The connection's read deadline is reset when the connection opens. // // Default: unlimited ReadTimeout time.Duration `json:"read_timeout"` // The maximum duration before timing out writes of the response. // It is reset after the request handler has returned. // // Default: unlimited WriteTimeout time.Duration `json:"write_timeout"` //This is the periodic time in which the server can execute //background tasks background tasks can run infinitely //as long as the server is running //for example you can use this to make requests to other servers //or update your database // // Default: 5 minutes BackgroundTimeout time.Duration `json:"background_timeout"` // When set to true, disables keep-alive connections. // The server will close incoming connections after sending the first response to client. // // Default: false DisableKeepAlive bool `json:"disable_keep_alive"` JSONEncoder JSONMarshal `json:"-"` JSONDecoder JSONUnmarshal `json:"-"` // StreamRequestBody enables request body streaming, // and calls the handler sooner when given body is // larger then the current limit. StreamRequestBody bool // RequestMethods provides customizibility for HTTP methods. You can add/remove methods as you wish. // // Optional. Default: DefaultMethods RequestMethods []string // Client is used to make requests to other servers Client *http.Client }
Config is a struct holding the server settings. TODO: More encoders and decoders coming soon
type Cookie ¶
type Cookie struct { //Name of the cookie // //Required field Name string //what data is stored in the cookie // //Required field Value string //determines the path in which the cookie is supposed to be used on //you can set this to "/" so that every request will contain the cookie Path string //This allows the browser to associate your cookie with a specific domain //when set to example.com cookies from example.com will always be sent //with every request to example.com Domain string //Determines the specific time the cookie expires //Max age is more prefered than expires Expires time.Time //Also sets the expiry date and you can use a string here instead RawExpires string //Max-Age field of the cookie determines how long the cookie // stays in the browser before expiring //if you want the cookies to expire immediately such as when a user logs out //you can set this to -1 // //accepts int value which should be the time in milliseconds you want //the cookie to be stored in the browser MaxAge int //A boolean value that determines whether cookies will be sent over https //or http only. // //Default is false and http can also send the cookies Secure bool //determines whether requests over http only can send the cookie HttpOnly bool //Cookies from the same domain can only be used on the specified domain //Eg: cookies from app.example.com can only be used by app.example.com //if you want all domains associated with example.com you can set this to //*.example.com //Now both app.example.com or dev.example.com can use the same cookie // //Options include the following: // 0 - SameSite=Lax // 1 - SameSite=Strict // 2 - SameSite=None //It will alwas default to Lax SameSite SameSite //All cookie data in string format. You do not need to set this //Pine can handle it for you Raw bool //Pine will also take care of this Unparsed []string }
cookie struct that defines the structure of a 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) Context ¶
Context returns the context of the request (This is the same as c.Request.Context()) as it returns a http.Request.Context()
func (*Ctx) DeleteCookie ¶
This function is used to delete cookies You can pass multiple names of cookies to be deleted at once
func (*Ctx) Header ¶
This is used to retrieve the header value specified by the key This is particularly useful when you want to retrieve specific headers from a request such as the Authorization header
func (*Ctx) JSON ¶
JSON writes a JSON response If you would like to set the status code of the response, you can pass it as the second argument
If you notice using c.Status(http.StatusOk).JSON(...json_payload) is not working properly, you can simply use c.JSON(...json_payload) without specifying the status Default status code is 200
func (*Ctx) Locals ¶
func (c *Ctx) Locals(key interface{}, value ...interface{}) interface{}
This can be used to set the local values of a request This is particulary usefule when unpacking data from a cookie Eg: You can parse a JWT token and decode the data inside it Then you can simply pack this data into the locals value of your request by doing c.Locals("key", data)
now whenever a request is made with that cookie if you set up your middleware to unpack the data in the locals field of your request you can access this data in your route
Eg: in your app.Get("/helloYou", authmiddleware(func(c *pine.Ctx) error { user:=c.Locals("key") return c.SendString("hello"+ user.name) }))
func (*Ctx) Next ¶
Next is used to execute the next handler in the stack This is useful when you want to execute the next handler in the stack but you want to do some additional work before executing the next handler for example, you can use this to authenticate the user
func (*Ctx) Params ¶
used to extract params from a specified request Eg: app.Get("/hello/:user",func(c *Pine.Ctx)error{ user:=c.Params("user")
return c.SendString("hello"+user) })
func (*Ctx) ParamsInt ¶
Same as Params above but saves you the time of converting a string params to an int type and you can extract the int value directly returns the int and error if any you can use the error to send back http.StatusBadRequest or 400 to the user if the user send a params that is not an int type
func (*Ctx) Query ¶
used to obtain queries from requests EG: a user could send the request http://localhost:3000/hello?user=pine you can extract the user value by calling c.Query("user")
func (*Ctx) ReadCookie ¶
Used to read cookies with every request This is particularly useful for middlewares
func (*Ctx) SendStatus ¶
SendStatus sends a status code as the response Does not send any body Does not accept additional helpers like c.Status(200).JSON(...)
func (*Ctx) SendString ¶
SendString sends a string as the response Default status code is 200
type JSONMarshal ¶
type JSONUnmarshal ¶
type Middleware ¶
type Route ¶
type Route struct { // Public fields // HTTP method Method string `json:"method"` // Original registered route path Path string `json:"path"` // Ctx handlers Handlers []Handler `json:"-"` }
Route is a struct that holds all metadata for each registered handler. TODO: More features coming soon
type Server ¶
type Server struct { //in case you want to serve https out of the box //you can set this to an empty string when you start a new server by //doing app:=pine.New(":3000","","") //this will default the server to http CertFile string //in case you want to serve https out of the box //you can set this to an empty string when you start a new server by //doing app:=pine.New(":3000","","") //this will default the server to http KeyFile string // contains filtered or unexported fields }
func New ¶
This is called to start a new Pine server You can set the configuration as per your requirements or you can use the default and let Pine take care of it for you
func (*Server) AddQueue ¶
func (server *Server) AddQueue(tasks ...BackgroundTask)
AddQueue is used put some functions in a queue that can be executed in the background for a specified period of time This is particularly useful for making requests to other servers or for performing some other background task
You can add as many tasks as you want to the queue however the please be mindful of the queue size as it will impact the performance check out examples at https://github.com/BryanMwangi/pine/tree/main/Examples/BackgroundTask/main.go
func (*Server) AddRoute ¶
This method is called to register routes and their respective methods it also accepts handlers in case you want to use specific middlewares for specific routes
func (*Server) Client ¶
this is used to act make the server act as a client the server can be used to make requests to other servers
func (*Server) ServeShutDown ¶
func (*Server) Start ¶
Called to start the server after creating a new server
You can put this in a go routine to handle graceful shut downs You can check out an example on https://github/BryanMwangi/pine/Examples/RunningInGoRoutine/main.go
func (*Server) Use ¶
func (server *Server) Use(middleware Middleware)
Use method is for specifying middleware to be used on specific routes for example you could have an authentication middleware that checks for cookies with every request to authenticate the user request
Directories
¶
Path | Synopsis |
---|---|
Examples
|
|
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. |