Documentation
¶
Overview ¶
Package essen is a micro web framework for golang, It resembles express.js (node.js web framework) functions with nearly one to one mapping.
Index ¶
- Constants
- Variables
- func SetConcurrencyLimit(n int)
- type Default
- type Essen
- func (e Essen) Get(route string, f func(Response, Request))
- func (e Essen) Head(route string, f func(Response, Request))
- func (e Essen) Listen(port int)
- func (e Essen) Post(route string, f func(Response, Request))
- func (e Essen) Put(route string, f func(Response, Request))
- func (e Essen) Router() router
- func (e Essen) SetMultiPartConfig(configMap map[string]string) bool
- func (e Essen) Static(route string, path string)
- func (e Essen) Use(route string, f func(Response, Request))
- func (e Essen) UseRouter(route string, router router)
- type EssenError
- type Request
- func (r Request) Close()
- func (r Request) CookieVal(key string) (string, EssenError)
- func (r Request) HasCookie(key string) bool
- func (r Request) HasHeader(key string) bool
- func (r Request) Header(key string) (string, EssenError)
- func (r Request) Host() string
- func (r Request) Method() string
- func (r Request) Path() string
- type Response
- func (r Response) Cookie(key string, val string, age int, secure bool, httpOnly bool)
- func (r Response) Json(status int, v interface{}) error
- func (r Response) Redirect(status int, url string)
- func (r Response) Render(status int, filename string, data interface{}, f TemplateFunc)
- func (r Response) Send(status int, v string)
- func (r Response) SendFile(status int, path string) (int64, EssenError)
- func (r Response) SendStatus(status int)
- func (r Response) Set(k string, v string)
- type TemplateFunc
Constants ¶
const ( //Minute constant to get numerical value of time unit Minute = 60 //Hour constant to get numerical value of time unit Hour = 60 * Minute //Day constant to get numerical value of time unit Day = 24 * Hour //Week constant to get numerical value of time unit Week = 7 * Day //Month constant to get numerical value of time unit Month = 4 * Week )
Variables ¶
var Defaults = Default{UploadDir: "./uploads"}
var MultiPartConfig = map[string]string{"UploadDir": Defaults.UploadDir} //optional
MultiPartConfig map is used to store configuration data for multipart requests.
Functions ¶
func SetConcurrencyLimit ¶
func SetConcurrencyLimit(n int)
SetConcurrencyLimit is used to set limit on concurrently running request handlers
Types ¶
type Essen ¶
type Essen struct { //To share values between every request handler, Accessible in request handler. //Checkout Request type for more. //Lifetime: Until app is running Locals map[string]interface{} }
Essen is the root struct, It's instance provide the main entrypoint to access all other functions.
Expressjs similarity ¶
let app = express();
func (Essen) Get ¶
Get is a function to register a route against HTTP GET request method
app := essen.App() app.Get("/", func(res app.Response, req app.Request){ //do something })
func (Essen) Head ¶
Head is a function to register a route against HTTP HEAD request method
app := essen.App() app.Head("/", func(res app.Response, req app.Request){ //do something })
func (Essen) Listen ¶
Listen function lets you lift HTTP server.
app := essen.App() app.Listen(8080) //Will listen for requests on port 8080.
func (Essen) Post ¶
Post is a function to register a route against HTTP POST request method
app := essen.App() app.Post("/", func(res app.Response, req app.Request){ //do something })
func (Essen) Put ¶
Put is a function to register a route against HTTP PUT request method
app := essen.App() app.Put("/", func(res app.Response, req app.Request){ //do something })
func (Essen) Router ¶
func (e Essen) Router() router
Router function is used to get router instance which can then be used to group different request handlers.
app := essen.App() router := essen.Router() router.Get("/index", func(res essen.Response, req essen.Request){ //do something }) router.Post("/form", func(res essen.Response, req, essen.Request){ //do something }) app.UseRouter("/user", router) router.Done() //Call Done after using a router, Used to achieve memory efficiency.
func (Essen) SetMultiPartConfig ¶
SetMultiPartConfig function is used to set multipart requests configuration
It can be set directly by changing MultiPartConfig map but calling this function is recommended way of doing so.
app := essen.App() app.SetMultiPartConfig(map[string]string{"UploadDir": "./uploadsFolder"}) //optional
func (Essen) Static ¶
Static is function to serve static files on reciept HTTP request.
`path` parameter can either be a directory path or a file path.
app := essen.App() app.Static("/static", "./static_dir")
type EssenError ¶
type EssenError struct {
// contains filtered or unexported fields
}
EssenError type which interfcaes with native Error type
func CreateDirIfNotExist ¶
func CreateDirIfNotExist(path string) EssenError
CreateDirIfNotExist is used to create a directory if does not exists.
func CreateFileIfNotExist ¶
func CreateFileIfNotExist(path string) (*os.File, EssenError)
CreateFileIfNotExist is used to create a file if does not exists.
func (EssenError) Error ¶
func (err EssenError) Error() string
Error function to return string representation of error occured.
func (EssenError) IsNil ¶
func (err EssenError) IsNil() bool
IsNil function to check if EssenError instance is nil or not.
Used to check if error occured or not.
func (EssenError) Message ¶
func (err EssenError) Message() string
Message function to return unexported message field of EssenError
func (EssenError) Type ¶
func (err EssenError) Type() string
Type function to return unexported errortype field of EssenError
type Request ¶
type Request struct { //Reference to native http.Request instance. Req *http.Request //Copy of Essen.Locals. //Properties set in Essen.Locals are accessed in request handlers through req.App. App map[string]interface{} //Map to pass values within middlewares. //Lifetime: Until request is responded. Locals map[string]interface{} //Field to access request body parameters. // // val, err := req.Body.Params("key") // // if err.IsNil(){ // fmt.Pritln(val) // } Body param //Unique id per request. (Used for request data cleanup) Uid string }
Request type is a wrapper around native http.Request.
This type is used to access request functions built in essen. Essen Request Type
func (Request) Close ¶
func (r Request) Close()
Close method is used to clear request related data (Mostly Multipart Data)
req.Close()
func (Request) CookieVal ¶
func (r Request) CookieVal(key string) (string, EssenError)
CookieVal is used to get value of a cookie
func (Request) Header ¶
func (r Request) Header(key string) (string, EssenError)
Header function is used to get value of request headers
type Response ¶
type Response struct { //Copy of native http.ResponseWriter instance. Res http.ResponseWriter ReqMethod string }
Response type is a wrapper around native http.ResponseWriter.
This type is used to access response functions built in essen.
func (Response) Cookie ¶
Cookie function to set cookies
res.Cookie("key", "val", 60, false, false)
`Age` parameter is given in seconds.
Check https://golang.org/pkg/net/http/#Cookie for more details
func (Response) Json ¶
Json function to send respose in JSON format
res.Json(200, map[string]string{"msg":"Hello Essen"})
func (Response) Redirect ¶
Redirect function is used to redirect to another URL/Route
res.Redirect(302, '/') res.Redirect(302, "google.com")
func (Response) Render ¶
func (r Response) Render(status int, filename string, data interface{}, f TemplateFunc)
Render is used to send a go template as response
res.Render(200, "./templates/index.gohtml", map[string]string{"hello":"world"}, nil)
func (Response) SendFile ¶
func (r Response) SendFile(status int, path string) (int64, EssenError)
SendFile function to send a file in http response
res.Set("Content-Type", "test/plain") res.SendFile(200, "./notes.txt")
func (Response) SendStatus ¶
SendStatus function is used to write headers.
Used mostly is case to reply with no body (HEAD request - response)
type TemplateFunc ¶
type TemplateFunc map[string]interface{}
TemplateFunc type instance is used to pass functions in go templates
func GetTemplateFunc ¶
func GetTemplateFunc() TemplateFunc
GetTemplateFunc returns an instance of TemplateFunc type
tf := essen.GetTemplateFunc()
func (TemplateFunc) Push ¶
func (t TemplateFunc) Push(key string, f interface{})
Push method is used to add function to TemplateFunc instance