essen

package module
v0.0.0-...-d69ce9d Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2018 License: MIT Imports: 13 Imported by: 0

README

Essen

GoDoc Go Report Card

Essen is a golang based micro web framework, inspired by express.js framework for node.js. It is in a very early stage, with some request and response mapping, With time it will ripe.

Why the name?

The time I started developing it I had that sensation of eating something, in german essen is To eat so the name :P :'|)

10 seconds to code:

package main

import(
	"github.com/akshitgrover/essen"
)

func main(){
	e := essen.App()

	e.Use("/getpath",func(res essen.Response,req essen.Request){
		res.Send(200, "Hello World")
	}) // Use Method is for any request method

	// Similar Methods For Post And Get Request

	e.Listen(<port>) // Specify The PORT
}

P.S.

This project is in very early stage, will be developing it further.

Will love any suggestions and ideas for the same.

MIT License

Copyright (c) 2018 Akshit Grover

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

View Source
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

View Source
var Defaults = Default{UploadDir: "./uploads"}
View Source
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 Default

type Default struct {
	UploadDir string
}

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 App

func App() Essen

App is an entrypoint to essen functions.

app := essen.App()

func (Essen) Get

func (e Essen) Get(route string, f func(Response, Request))

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

func (e Essen) Head(route string, f func(Response, Request))

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

func (e Essen) Listen(port int)

Listen function lets you lift HTTP server.

app := essen.App()
app.Listen(8080) //Will listen for requests on port 8080.

func (Essen) Post

func (e Essen) Post(route string, f func(Response, Request))

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

func (e Essen) Put(route string, f func(Response, Request))

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

func (e Essen) SetMultiPartConfig(configMap map[string]string) bool

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

func (e Essen) Static(route string, path string)

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

func (Essen) Use

func (e Essen) Use(route string, f func(Response, Request))

Use is a function to register a route against any HTTP request method.

app := essen.App()

app.Use("/", func(res app.Response, req app.Request){
	//do something
})

func (Essen) UseRouter

func (e Essen) UseRouter(route string, router router)

UseRouter function is called to register router against a parent route.

Look at Router() method for an example.

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

func (r Request) HasCookie(key string) bool

HasCookie is used to check if a cookie exists in a request

func (Request) HasHeader

func (r Request) HasHeader(key string) bool

HasHeader is used to check if a header is sent for the request.

func (Request) Header

func (r Request) Header(key string) (string, EssenError)

Header function is used to get value of request headers

func (Request) Host

func (r Request) Host() string

Host is used to get host address from the url

func (Request) Method

func (r Request) Method() string

Method is used to check HTTP request method.

func (Request) Path

func (r Request) Path() string

Path functions is used to get URL path

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

func (r Response) Cookie(key string, val string, age int, secure bool, httpOnly bool)

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

func (r Response) Json(status int, v interface{}) error

Json function to send respose in JSON format

res.Json(200, map[string]string{"msg":"Hello Essen"})

func (Response) Redirect

func (r Response) Redirect(status int, url string)

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

func (r Response) Send(status int, v string)

Send function to send response in HTML format

res.Send(200, "Hello World")

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

func (r Response) SendStatus(status int)

SendStatus function is used to write headers.

Used mostly is case to reply with no body (HEAD request - response)

func (Response) Set

func (r Response) Set(k string, v string)

Set function is used to set headers

'k' parameter key

'v' parameter value

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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