minima

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2022 License: MIT Imports: 18 Imported by: 5

README

Minima

Minima

Minima 🦄 is a reliable and lightweight framework for Go to carve the web 💻. Developed with core net/http🔌and other native packages, and with 0 dependencies

⚙️ Setup

Please make sure you have Go version 1.15 or higher

mkdir <project-name> && cd  <project-name>

go mod init github.com/<user-name>/<repo-name>

go get github.com/gominima/minima

go run main.go

🦄 Quickstart

package main

import "github.com/gominima/minima"

func main() {
	app := minima.New()

	app.Get("/", func(res *minima.Response, req *minima.Request) {
		res.OK().Send("Hello World")
	})

	app.Listen(":3000")
}

🔮 Features

  • Reliable - great modular API for building great server side applications
  • Compatible with net/http - use your plain old middlewares written in plain old net/http
  • Lightweight - clocked in ~1000 loc
  • No Dependency - just your plain old go standard libraries
  • Great Documentation - best in class precise documentation
  • Auto Docs - docgen for generating all of your routing docs from jsdoc like comments to json or markdown files

❓ Why Minima

Minima's name is inspired by the word minimal and is the motivation for building this framework. As a Golang developer, I was struggling to learn it in my early days due to the steeper learning curve while using net/http.

Also while checking out some other alternate frameworks, I found out that something like fiber wasn't compatible with net/http modules like gqlgen and other middlewares.

Minima solves this problem as it has a very narrow learning curve as well as a robust structure that supports all net/http modules and other middlewares without compromising performance.

🍵 Examples

Here are some basic examples related to routing and params:

📑 Routing & Router
func UserGetRouter() *minima.Router {
	// router instance which would be used by the main router
	router := minima.NewRouter()

	return router.Get("/user/:id", func(res *minima.Response, req *minima.Request) {
		// getting the id parameter from route
		id := req.Param("id")

		// instead of adding a param in route, you just need to fetch it

		username := req.Query("name")

		// get user from database
		userdata, err := db.FindUser(id, username)

		if err != nil {
			// check for errors
			res.NotFound().Send("No user found with particular id")
		}
		// send user data
		res.OK().Json(userdata)
	})
}

func main() {
	// main minima instance
	app := minima.New()
	// UseRouter method takes minima.router as a param
	// it appends all the routes used in that specific router to the main instance
	app.UseRouter(UserGetRouter())

	// running the app at port 3000
	app.Listen(":3000")
}
📑 Params
func main() {
	app := minima.New()

	app.Get("/getuser/:id", func(res *minima.Response, req *minima.Request) {
		userid := req.Param("id")
		// check if user id is available
		if userid == "" {
			res.Error(404, "No user found")
			panic("No user id found in request")
		}
		fmt.Print(userid)
		//Will print 20048 from router /getuser/200048
	})
}
📑 Query Params
func main() {
	app := minima.New()

	app.Get("/getuser", func(response *minima.Response, request *minima.Request) {
		// query params work a bit differently
		// instead of adding a param in route, you just need to fetch it

		userid := req.Query("id")

		if userid == "" {
			res.Error(404, "No user found")
			panic("No user id found in request")
		}
		fmt.Print(userid)
		// the above will print 20048 from router /getuser?id=20048
	})
}

📒 Minima Interface

Minima is based on a custom implementation of radix tree which makes it extremely performant. The router itself is fully compatible with net/http

🔖 Minima's Interface
type Minima interface {
	// Minima interface is built over net/http so every middleware is compatible with it

	// initializes net/http server with address
	Listen(address string) error
    

	//static file serve methods
	File(pth string, dir string) //serves a single static file to route
	Static(pth string, dir string) //serves whole directory with specified pth ex /static/main.html
	// main handler interface
	ServeHTTP(w http.ResponseWriter, q *http.Request)

	// main router methods
	Get(path string, handler ...Handler) *minima
	Patch(path string, handler ...Handler) *minima
	Post(path string, handler ...Handler) *minima
	Put(path string, handler ...Handler) *minima
	Options(path string, handler ...Handler) *minima
	Head(path string, handler ...Handler) *minima
	Delete(path string, handler ...Handler) *minima

	// takes middlewares as a param and adds them to routes
	// middlewares initializes before route handler is mounted
	Use(handler Handler) *minima

       //Takes http.Handler and appends it to middleware chain
	UseRaw(handler func(http.Handler) http.Handler) *minima

        // an custom handler when route is not matched
	NotFound(handler Handler)*minima

	// mounts routes to specific base path
	Mount(basePath string, router *Router) *minima

	// takes minima.Router as param and adds the routes from router to main instance
	UseRouter(router *Router) *minima

	// works as a config for minima, you can add multiple middlewares and routers at once
	UseConfig(config *Config) *minima

	// shutdowns the net/http server
	Shutdown(ctx context.Context) error

	// prop methods
	SetProp(key string, value interface{}) *minima
	GetProp(key string) interface{}
}
🔖 Response and Request Interfaces

Both response and request interfaces of minima are written in net/http so you can use any of your old route middlewares in minima out of the box without any hassle.


type Res interface {
	// response interface is built over http.ResponseWriter for easy and better utility

	// Header methods
	GetHeader(key string) string // gets a header from response body
        
	SetHeader(key string, value string)  *Response // sets a new header to response body

	DelHeader(key string)  *Response // Deletes a header from response body
        
	CloneHeaders() http.Header // clones all headers of the response body

	Setlenght(len string) *Response // sets content length of the response body

	SetBaseHeaders() *Response // sets a good stack of base headers for response body

	FlushHeaders() // flushes headers

	// utility functions for easier usage
	Send(content string) *Response      //send content
	WriteBytes(bytes []byte) error      //writes bytes to the page
	JSON(content interface{}) *Response //sends data in json format
	XML(content interface{}, indent string) //sends data in xml format
	Stream(contentType string read io.Reader) // streams content to the route
	NoContent(code int)
	Error(status int, str string) *Response

	// this functions returns http.ResponseWriter instance which means you could use any of your alrady written middlewares in minima!
	Raw() http.ResponseWriter

	// renders an html file with data to the page
	Render(path string, data interface{}) *Response
        
	// custom method when there's an error
	Error(content interface{}) *Response
        
	// closes io.Writer connection from the route
	CloseConn() *Response

	// redirects to given url
	Redirect(url string) *Response

	// sets header status
	Status(code int) *Response

	//cookie methods
	SetCookie(cookie *http.Cookie) *Response // sets a new cookie to the response

	
	SetCookie(cookie *http.Cookie) *Response // clears a cookie to the response

}

type Req interface {
	// minima request interface is built on http.Request

	// returns param from route 
	Param(name string) string
        
	// sets a new param to the request instance
	SetParam(key string, value) string
	
	//returns query param from route url
	Query(key string) string
        
	//returns query params to a string
	QueryString() string
        
	//returns raw url.Values
	QueryParams() url.Values
        
	//returns the ip of the request origin
	IP() string

	//returns whether the request is tls or not
	IsTLS() bool

	//returns whether the request is a socket or not
	IsSocket() bool

	//returns scheme type of request body
	SchemeType() string

	//Gets form value from request body 
	FormValue(key string) string

	//Gets all the form param values
	FormParams() (url.Values, error)

	//Gets file from request 
	FormFile(key string) (*multipart.FileHeader, error)

	//Gets request Multipart form
	MultipartForm() (*multipart.Form, error)
	// returns path url from the route
	Path() string

	// returns raw request body
	Body() map[string][]string

	// finds given key value from body and returns it
	BodyValue(key string) []string

	// returns instance of minima.IncomingHeader for incoming header requests
	Header() *IncomingHeader

	// returns route method ex.get,post
	Method() string

	// Header methods
	SetHeader(key string, value string) *Response //sets a new header to request body
        
	//Get a header from request body
	GetHeader(key string) string

	//Cookie methods
        Cookies() []*http.Cookie // gets all cookies from request body

	GetCookie(key string) *http.Cookie // gets a specific cookie from request body

}

🔌 Middlewares

Minima's middlewares are written in its own custom res and req interfaces in accordance with the standard libraries maintained by Go. You can use res.Raw() to get the http.ResponseWriter instance and req.Raw() to getthe http.Request instance, meaning all community written middlewares are compatible with Minima.

Minima also takes http.Handler while using .UseRaw function and runs it in a chain.

Here is an example of standard net/http middleware being used with minima:

func MyMiddleWare(res *minima.Response, req *minima.Request) {
	w := res.Raw() // raw http.ResponseWriter instance
	r := req.Raw() // raw http.Request instance

	// your normal net/http middleware
	w.Write([]byte(r.URL.Path))
}
app.UseRaw(HttpHandler())

💫 Contributing

If you wanna help grow this project or say a thank you!

  1. Give minima a GitHub star
  2. Fork Minima and Contribute
  3. Write a review or blog on Minima
  4. Join our Discord community
Contributors
Lead Maintainers
Core Team
Community Contributors

Thanks to all the contributors, without whom this project would not have been possible:


Be a part of this contribution list by contributing today!

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

🧾 License

Copyright (c) 2021-present Apoorv and Contributors. Minima is a Free and Open Source Software licensed under MIT License



Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTree added in v1.1.0

func NewTree() *tree

*

  • @info Creates a new radix tree

func ToMap added in v1.1.0

func ToMap(tre *tree) map[string]Handler

*

  • @info Turns a radix tree into a hash map
  • @param {tree} [tre] The tree to convert
  • @returns {map[string]Handler}

Types

type ChainHandler

type ChainHandler struct {
	Endpoint http.Handler

	Middlewares Middlewares
	// contains filtered or unexported fields
}

*

  • @info Create a middleware chain
  • @property {http.Handler} [Endpoint] The endpoint of the chain stack
  • @property {http.Handler} [chain] The actual middleware stack chain
  • @property {Middlewares} Middlewares The middleware stack

func (*ChainHandler) ServeHTTP

func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

*

  • @info Injects the middleware chain to minima instance
  • @param {http.ResponseWriter} [w] The net/http response instance
  • @param {http.Request} [r] The net/http request instance

type Group added in v1.1.3

type Group struct {
	// contains filtered or unexported fields
}

*

  • @info The minima group structure
  • @property {[]cacheroute} [route] The array of cached routes
  • @property {[string} [prefix] The group prefix

func NewGroup added in v1.1.3

func NewGroup(prefix string) *Group

*

  • @info Creates a new minima group
  • @return {Group}

func (*Group) Delete added in v1.1.3

func (g *Group) Delete(path string, handler Handler) *Group

*

  • @info Adds route with Delete method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Group}

func (*Group) Get added in v1.1.3

func (g *Group) Get(path string, handler Handler) *Group

*

  • @info Adds route with Get method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Group) GetGroupRoutes added in v1.1.3

func (g *Group) GetGroupRoutes() []*cacheRoute

*

  • @info Returns all routes for the group
  • @return {[]cachRoute}

func (*Group) Head added in v1.1.3

func (g *Group) Head(path string, handler Handler) *Group

*

  • @info Adds route with Head method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Group}

func (*Group) Options added in v1.1.3

func (g *Group) Options(path string, handler Handler) *Group

*

  • @info Adds route with Options method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Group}

func (*Group) Patch added in v1.1.3

func (g *Group) Patch(path string, handler Handler)

*

  • @info Adds route with Patch method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Group}

func (*Group) Post added in v1.1.3

func (g *Group) Post(path string, handler Handler) *Group

*

  • @info Adds route with Post method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Group}

func (*Group) Put added in v1.1.3

func (g *Group) Put(path string, handler Handler) *Group

*

  • @info Adds route with Put method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Group}

type Handler

type Handler func(res *Response, req *Request)

type Middlewares

type Middlewares []func(http.Handler) http.Handler

func Chain

func Chain(middlewares ...func(http.Handler) http.Handler) Middlewares

* n* @info Create a middleware chain

func (Middlewares) Handler

func (mws Middlewares) Handler(h http.Handler) http.Handler

*

  • @info Creates a new chain handler instance
  • @param {http.Handler} [h] The handler stack to append
  • @returns {http.Handler}

func (Middlewares) HandlerFunc

func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler

*

  • @info Creates a new chain handler instance from handlerfunc
  • @param {http.HandlerFunc} [h] The handlerfunc stack to append
  • @returns {http.Handler}

type Minima

type Minima struct {
	Timeout time.Duration
	// contains filtered or unexported fields
}

*

  • @info The framework structure
  • @property {*http.Server} [server] The net/http stock server
  • @property {bool} [started] Whether the server has started or not
  • @property {*time.Duration} [Timeout] The router's breathing time
  • @property {*Router} [router] The core router instance running with the server
  • @property {map[string]interface{}} [properties] The properties for the server instance
  • @property {*Config} [Config] The core config file for middlewares and router instances
  • @property {*time.Duration} [drain] The router's drain time

func New

func New() *Minima

*

  • @info Make a new default minima instance
  • @example `
func main() {
	app := minima.New()

	app.Get("/", func(res *minima.Response, req *minima.Request) {
		res.Status(200).Send("Hello World")
	})

	app.Listen(":3000")
}

`

  • @returns {minima}

func (*Minima) Delete

func (m *Minima) Delete(path string, handler Handler) *Minima

*

  • @info Adds route with Delete method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*minima}

func (*Minima) File added in v1.1.4

func (m *Minima) File(pth string, dir string)

*

  • @info Injects a static file to minima instance
  • @param {string} [pth] The route path for static serve
  • @param {string} [dir] The dir of the file
  • @returns {}

func (*Minima) Get

func (m *Minima) Get(path string, handler Handler) *Minima

*

  • @info Adds route with Get method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*minima}

func (*Minima) GetProp

func (m *Minima) GetProp(key string) interface{}

*

  • @info Gets prop from core properties
  • @param {string} [key] Key for the prop
  • @returns {interface{}}

func (*Minima) Head

func (m *Minima) Head(path string, handler Handler) *Minima

*

  • @info Adds route with Head method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*minima}

func (*Minima) Listen

func (m *Minima) Listen(addr string) error

*

  • @info Starts the actual http server
  • @param {string} [addr] The port for the server instance to run on
  • @returns {error}

func (*Minima) NotFound

func (m *Minima) NotFound(handler Handler) *Minima

*

  • @info Injects the NotFound handler to the minima instance
  • @param {Handler} [handler] Minima handler instance
  • @returns {*minima}

func (*Minima) Options

func (m *Minima) Options(path string, handler Handler) *Minima

*

  • @info Adds route with Options method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*minima}

func (*Minima) Patch

func (m *Minima) Patch(path string, handler Handler) *Minima

*

  • @info Adds route with Patch method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*minima}

func (*Minima) Post

func (m *Minima) Post(path string, handler Handler) *Minima

*

  • @info Adds route with Post method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*minima}

func (*Minima) Put

func (m *Minima) Put(path string, handler Handler) *Minima

*

  • @info Adds route with Put method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*minima}

func (*Minima) ServeHTTP

func (m *Minima) ServeHTTP(w http.ResponseWriter, r *http.Request)

*

  • @info Injects the actual minima server logic to http
  • @param {http.ResponseWriter} [w] The net/http response instance
  • @param {http.Request} [r] The net/http request instance
  • @returns {}

func (*Minima) SetProp

func (m *Minima) SetProp(key string, value interface{}) *Minima

*

  • @info Declares prop for core properties
  • @param {string} [key] Key for the prop
  • @param {interface{}} [value] Value of the prop
  • @returns {*minima}

func (*Minima) Shutdown

func (m *Minima) Shutdown(ctx context.Context) error

*

  • @info Shutdowns the core instance
  • @param {context.Context} [ctx] The context for shutdown
  • @returns {error}

func (*Minima) ShutdownTimeout

func (m *Minima) ShutdownTimeout(t time.Duration) *Minima

*

  • @info The drain timeout for the core instance
  • @param {time.Duration} time The time period for drain
  • @returns {*minima}

func (*Minima) Static added in v1.1.4

func (m *Minima) Static(pth string, dir string)

*

  • @info Injects a static directory to minima instance
  • @param {string} [pth] The route path for static serve
  • @param {string} [dir] The dir of the static folder
  • @returns {}

func (*Minima) Use

func (m *Minima) Use(handler Handler) *Minima

*

  • @info Injects minima middleware to the stack
  • @param {Handler} [handler] The handler stack to append
  • @returns {}

func (*Minima) UseGroup added in v1.1.3

func (m *Minima) UseGroup(grp *Group) *Minima

*

  • @info Injects minima group to main router stack
  • @param {Group} [grp] The minima group to append
  • @returns {}

func (*Minima) UseRaw

func (m *Minima) UseRaw(handler ...func(http.Handler) http.Handler) *Minima

*

  • @info Injects net/http middleware to the stack
  • @param {...http.HandlerFunc} [handler] The handler stack to append
  • @returns {}

func (*Minima) UseRouter

func (m *Minima) UseRouter(router *Router) *Minima

*

  • @info Injects the routes from the router to core stack
  • @param {*Router} [router] Minima router instance
  • @returns {*minima}

type Node added in v1.1.0

type Node struct {
	// contains filtered or unexported fields
}

*

  • @info The tree Node structure
  • @property {Handler} [handler] The handler to be used
  • @property {[]*edge} [edges] The array of node edges
  • @property {int} [priority] The priority of the node in the tree
  • @property {int} [depth] The depth of the node in the tree

func (*Node) IsLeaf added in v1.1.0

func (n *Node) IsLeaf() bool

*

  • @info Whether the node is a leaf or not
  • @returns {bool}

type OutgoingHeader

type OutgoingHeader struct {
	// contains filtered or unexported fields
}

*

  • @info The Outgoing header structure
  • @property {http.Request} [req] The net/http request instance
  • @property {http.ResponseWriter} [res] The net/http response instance
  • @property {bool} [body] Whether body has been sent or not
  • @property {int} [status] response status code

func NewResHeader

func NewResHeader(res http.ResponseWriter, req *http.Request) *OutgoingHeader

*

  • @info Make a new default request header instance
  • @param {http.Request} [req] The net/http request instance
  • @param {http.ResponseWriter} [res] The net/http response instance
  • @returns {OutgoingHeader}

func (*OutgoingHeader) BaseHeaders

func (h *OutgoingHeader) BaseHeaders()

*

  • @info Sends good stack of base headers
  • @returns {}

func (*OutgoingHeader) Clone

func (h *OutgoingHeader) Clone() http.Header

*

  • @info Clones all headers from response
  • @returns {OutgoingHeader}

func (*OutgoingHeader) Del

func (h *OutgoingHeader) Del(key string) *OutgoingHeader

*

  • @info Deletes header from respose
  • @param {string} [key] Key of the header
  • @returns {OutgoingHeader}

func (*OutgoingHeader) Flush

func (h *OutgoingHeader) Flush() bool

*

  • @info Flushes and writes header to route
  • @returns {bool}

func (*OutgoingHeader) Get

func (h *OutgoingHeader) Get(key string) string

*

  • @info Gets the header from response headers
  • @param {string} [key] Key of the header
  • @returns {string}

func (*OutgoingHeader) Set

func (h *OutgoingHeader) Set(key string, value string) *OutgoingHeader

*

  • @info Sets and new header to response
  • @param {string} [key] Key of the new header
  • @param {string} [value] Value of the new header
  • @returns {OutgoingHeader}

func (*OutgoingHeader) Setlength

func (h *OutgoingHeader) Setlength(len string) *OutgoingHeader

*

  • @info Sets content lenght
  • @param {string} [len] The lenght of the content
  • @returns {OutgoingHeader}

func (*OutgoingHeader) Status

func (h *OutgoingHeader) Status(code int) *OutgoingHeader

*

  • @info Sets response status
  • @param {int} [code] The status code for the response
  • @returns {OutgoingHeader}

type Request

type Request struct {
	Params map[string]string
	// contains filtered or unexported fields
}

*

  • @info The request structure
  • @property {*http.Request} [ref] The net/http request instance
  • @property {multipart.Reader} [fileReader] file reader instance
  • @property {map[string][]string} [body] Value of the request body
  • @property {string} [method] Request method
  • @property {[]*Params} [Params] Request path parameters
  • @property {query} url.Values Request path query params
  • @property {json.Decoder} json Json decoder instance

func (*Request) Body

func (r *Request) Body() map[string][]string

*

  • @info Gets raw request body
  • @returns {map[string][]string}

func (*Request) BodyValue added in v1.1.1

func (r *Request) BodyValue(key string) []string

*

  • @info Gets specified request body
  • @param {string} [key] Key of the request body
  • @returns {[]string}

func (*Request) CloneHeader added in v1.1.5

func (r *Request) CloneHeader() http.Header

*

  • @info Clones all headers from request body
  • @returns {http.Header}

func (*Request) Cookies

func (r *Request) Cookies() []*http.Cookie

*

  • @info Get all the cookies from the request
  • @returns {[]*http.Cookie}

func (*Request) DelHeader added in v1.1.5

func (r *Request) DelHeader(key string)

*

  • @info Deletes a paticular Header by its key
  • @param {string} [key] key of the Header

func (*Request) FormFile added in v1.1.1

func (r *Request) FormFile(key string) (*multipart.FileHeader, error)

*

  • @info Gets a file from request form
  • @returns {multipart.FileHeader, error}

func (*Request) FormParams added in v1.1.1

func (r *Request) FormParams() (url.Values, error)

*

  • @info Gets all the form param values
  • @returns {url.Values, error}

func (*Request) FormValue added in v1.1.1

func (r *Request) FormValue(key string) string

*

  • @info Gets the values from request form
  • @param {string} [key] The key of the value
  • @returns {string}

func (*Request) GetCookie

func (r *Request) GetCookie(key string) *http.Cookie

*

  • @info Get a paticular cookie by its key
  • @param {string} [key] key of the cookie
  • @returns {*http.Cookie}

func (*Request) GetHeader

func (r *Request) GetHeader(key string) string

*

  • @info Get a paticular Header by its key
  • @param {string} [key] key of the Header
  • @returns {string}

func (*Request) IP added in v1.1.1

func (r *Request) IP() string

*

  • @info Gets ip of the request origin
  • @returns {string}

func (*Request) IsSocket added in v1.1.1

func (r *Request) IsSocket() bool

*

  • @info Whether the request is a websocket or not
  • @returns {bool}

func (*Request) IsTLS added in v1.1.1

func (r *Request) IsTLS() bool

*

  • @info Whether the request is TLS or not
  • @returns {bool}

func (*Request) Json

func (r *Request) Json() *json.Decoder

*

  • @info Gets raw json decoder instance
  • @returns {json.Decoder}

func (*Request) Method

func (r *Request) Method() string

*

  • @info Gets method of request
  • @returns {string}

func (*Request) MultipartForm added in v1.1.1

func (r *Request) MultipartForm() (*multipart.Form, error)

*

  • @info Gets a Multi part form from request form
  • @returns {multipart.Form, error}

func (*Request) Param added in v1.1.1

func (r *Request) Param(key string) string

*

  • @info Gets param from route path
  • @param {string} [key] Key of the route param
  • @returns {string}

func (*Request) Path added in v1.1.1

func (r *Request) Path() string

*

  • @info Gets request path url
  • @returns {string}

func (*Request) Query added in v1.0.1

func (r *Request) Query(key string) string

*

  • @info Gets request path query
  • @param {string} [key] key of the request query
  • @returns {string}

func (*Request) QueryParams added in v1.1.1

func (r *Request) QueryParams() url.Values

*

  • @info Gets request path query in an array
  • @param {string} [key] key of the request query
  • @returns {string}

func (*Request) QueryString added in v1.1.1

func (r *Request) QueryString() string

*

  • @info Gets request path query in a string
  • @returns {string}

func (*Request) Raw

func (r *Request) Raw() *http.Request

*

  • @info Gets raw net/http request instance
  • @returns {http.Request}

func (*Request) SchemeType added in v1.1.1

func (r *Request) SchemeType() string

*

  • @info Gets the scheme type of the request body
  • @returns {bool}

func (*Request) SetHeader

func (r *Request) SetHeader(key string, value string) *Request

*

  • @info Set a paticular Header
  • @param {string} [key] key of the Header
  • @param {string} [value] value of the Header
  • @returns {*Request}

func (*Request) SetParam

func (r *Request) SetParam(key string, value string) *Request

*

  • @info Sets param for route path
  • @param {string} [key] Key of the route param
  • @param {string} [value] Value of the route param
  • @returns {Respone}

type Response

type Response struct {
	HasEnded bool
	// contains filtered or unexported fields
}

*

  • @info The response instance structure
  • @property {http.ResponseWriter} [ref] The net/http response instance
  • @property {string} url The route url
  • @property {string} [method] The route http method
  • @property {OutgoingHeader} [header] The response header instance
  • @property {string} [host] The minima host
  • @property {bool} [HasEnded] Whether the response has ended

func (*Response) BadGateway

func (res *Response) BadGateway() *Response

*

  • @info Set status code as 502
  • @returns {Response}

func (*Response) BadRequest

func (res *Response) BadRequest() *Response

*

  • @info Set status code as 400
  • @returns {Response}

func (*Response) ClearCookie

func (res *Response) ClearCookie(cookie *http.Cookie) *Response

*

  • @info Clear a cookie
  • @param {*http.Cookie} [cookie]
  • @returns {Response}

func (*Response) CloneHeaders

func (res *Response) CloneHeaders() http.Header

*

  • @info Clones all header from response
  • @param {} []
  • @returns {http.Header}

func (*Response) CloseConn

func (res *Response) CloseConn() error

*

  • @info Ends connection to the route page
  • @returns {error}

func (*Response) DelHeader

func (res *Response) DelHeader(key string) *Response

*

  • @info Deletes header from response
  • @param {string} [key] Key of the header
  • @returns {string}

func (*Response) Error

func (res *Response) Error(status int, err string) *Response

*

  • @info Returns error to the route
  • @param {int} [status] The status code of the error
  • @param {string} [err] The error to write
  • @returns {Response}

func (*Response) File added in v1.1.4

func (res *Response) File(dir string) error

*

  • @info Sends a file to the server
  • @returns {error}

func (*Response) FlushHeaders

func (res *Response) FlushHeaders() *Response

*

  • @info Flushes headers to the response body
  • @returns {*Response}

func (*Response) Forbidden

func (res *Response) Forbidden() *Response

*

  • @info Set status code as 403
  • @returns {Response}

func (*Response) GetHeader

func (res *Response) GetHeader(key string) string

*

  • @info Gets header from response
  • @param {string} [key] Key of the header
  • @returns {string}

func (*Response) InternalServerError

func (res *Response) InternalServerError() *Response

*

  • @info Set status code as 500
  • @returns {Response}

func (*Response) JSON added in v1.1.1

func (res *Response) JSON(content interface{}) *Response

*

  • @info Writes json content to the route
  • @param {interface{}} [content] The json struct to write to the page
  • @returns {Response}

func (*Response) MovedPermanently

func (res *Response) MovedPermanently() *Response

*

  • @info Set status code as 301
  • @returns {Response}

func (*Response) NoContent added in v1.1.1

func (res *Response) NoContent(code int) error

*

  • @info Sets page's content to none
  • @param {int} [code] The status code
  • @returns {error}

func (*Response) NotFound

func (res *Response) NotFound() *Response

*

  • @info Set status code as 404
  • @returns {Response}

func (*Response) OK

func (res *Response) OK() *Response

*

  • @info Set status code as 200
  • @returns {Response}

func (*Response) Raw

func (res *Response) Raw() http.ResponseWriter

*

  • @info Returns raw http.ResponseWriter instance
  • @returns {http.ResponseWriter}

func (*Response) Redirect

func (res *Response) Redirect(url string) *Response

*

  • @info Redirects to a different route
  • @param {string} url The url of the route to redirect
  • @returns {Response}

func (*Response) Render

func (res *Response) Render(path string, data interface{}) *Response

*

  • @info Renders a html page with payload data to the route
  • @param {string} path The dir path of the html page
  • @param {interface{}} [data] The payload data to pass in html page
  • @returns {Response}

func (*Response) Send

func (res *Response) Send(content string) *Response

*

  • @info Sends string to the route
  • @param {string} [content] The content to write
  • @returns {Response}

func (*Response) ServiceUnavailable added in v1.0.2

func (res *Response) ServiceUnavailable() *Response

*

  • @info Set status code as 503
  • @returns {Response}

func (*Response) SetBaseHeaders

func (res *Response) SetBaseHeaders() *Response

*

  • @info Sets a good stack of base headers for response
  • @returns {*Response}

func (*Response) SetCookie

func (res *Response) SetCookie(cookie *http.Cookie) *Response

*

  • @info Set a cookie
  • @param {*http.Cookie} [cookie]
  • @returns {Response}

func (*Response) SetHeader

func (res *Response) SetHeader(key string, value string) *Response

*

  • @info Sets headers for response
  • @param {string} [key] Key of the header
  • @param {string} [value] Value of the header
  • @returns {string}

func (*Response) Setlength

func (res *Response) Setlength(len string) *Response

*

  • @info Sets length of the response body
  • @param {string} [len] length value of the header
  • @returns {*Response}

func (*Response) Status

func (res *Response) Status(status int) *Response

*

  • @info Sets response status
  • @param {int} [status] The status code for the response
  • @returns {Response}

func (*Response) Stream added in v1.1.1

func (res *Response) Stream(contentType string, read io.Reader) error

*

  • @info Streams content to the route
  • @param {string} [contentType] The content type to stream
  • @param {io.Reader} [read] The io.Reader instance
  • @returns {error}

func (*Response) TemporaryRedirect

func (res *Response) TemporaryRedirect() *Response

*

  • @info Set status code as 307
  • @returns {Response}

func (*Response) Unauthorized

func (res *Response) Unauthorized() *Response

*

  • @info Set status code as 401
  • @returns {Response}

func (*Response) WriteBytes

func (res *Response) WriteBytes(bytes []byte) error

*

  • @info Writes bytes to the route
  • @param {[]bytes} bytes The bytes to write
  • @returns {Response}

func (*Response) XML added in v1.1.1

func (res *Response) XML(content interface{}, indent string) error

*

  • @info Writes xml content to the route
  • @param {interface{}} [content] The xml content to write to the page
  • @param {string} [indent] The indentation of the content
  • @returns {error}

type Router

type Router struct {
	// contains filtered or unexported fields
}

*

  • @info The router structure
  • @property {map[string][]*tree} [routes] The radix-tree based routes
  • @property {Handler} [notfound] The handler for the non matching routes
  • @property {[]Handler} [minmiddleware] The minima handler middleware stack
  • @property {[]func(http.Handler)http.Handler} [middleware] The http.Handler middleware stack
  • @property {bool} [isCache] Whether the router is cache or not
  • @property {[]*cacheRoute} [cacheRoute] Slice of cached routes
  • @property {http.Handler} [handler] The single http.Handler built on chaining the whole middleware stack

func NewRouter

func NewRouter() *Router

*

  • @info Make new default router interface

return {Router}

func (*Router) Delete

func (r *Router) Delete(path string, handler Handler) *Router

*

  • @info Adds route with Delete method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Router) Get

func (r *Router) Get(path string, handler Handler) *Router

*

  • @info Adds route with Get method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Router) GetCacheRoutes added in v1.1.2

func (r *Router) GetCacheRoutes() []*cacheRoute

*

  • @info Returns all the routes in router
  • @returns {map[string][]*mux}

func (*Router) Head

func (r *Router) Head(path string, handler Handler) *Router

*

  • @info Adds route with Head method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Router) NotFound

func (r *Router) NotFound(handler Handler) *Router

func (*Router) Options

func (r *Router) Options(path string, handler Handler) *Router

*

  • @info Adds route with Options method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Router) Patch

func (r *Router) Patch(path string, handler Handler)

*

  • @info Adds route with Patch method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Router) Post

func (r *Router) Post(path string, handler Handler) *Router

*

  • @info Adds route with Post method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Router) Put

func (r *Router) Put(path string, handler Handler) *Router

*

  • @info Adds route with Put method
  • @param {string} path The route path
  • @param {...Handler} [handler] The handler for the given route
  • @returns {*Router}

func (*Router) Register

func (r *Router) Register(method string, path string, handler Handler) error

*

  • @info Registers a new route to router interface
  • @param {string} path The route path

return {string, []string}

func (*Router) UseRouter

func (r *Router) UseRouter(Router *Router)

*

  • @info Appends all routes to core router instance
  • @param {Router} Router The router instance to append
  • @returns {Router}

Directories

Path Synopsis
rtr

Jump to

Keyboard shortcuts

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