mint

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

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

Go to latest
Published: Jan 18, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

🌿 Mint web framework GoDoc Go Report Card

it is simple lightweight web framework, it helps to keep the core simple but extensible. Mint uses gorilla mux router. Mint does not include a database abstraction layer or body validation or anything else

Installation

To use Mint package, you need to install Go first in your system and set its workspace

$ go get -u github.com/5anthosh/mint

A Simple Example

In example.go file

package main

import "github.com/5anthosh/mint"

func main() {
  r := mint.New()
  
  r.GET("/{message}", func(c *mint.Context) {
	c.JSON(200, mint.JSON{
	"message": c.DefaultParam("message", "Hello World !"),
	})
  })

  r.Run(":8080")
}

To run the program

$ go run example.go

Example

Documentation

Index

Constants

View Source
const (
	PayloadKey = "PayLoad"
)

constant

Variables

View Source
var (

	//DefaultHandlerWithLogger middlewares including logger
	DefaultHandlerWithLogger = []HandlerFunc{loggerMW}
)

Functions

func ErrorMessage

func ErrorMessage(c *Context, code int, message string)

ErrorMessage #

func URLVar

func URLVar(urlvar string) string

URLVar formats url var

Types

type BufferPool

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

BufferPool #

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool #

func (*BufferPool) Get

func (bpool *BufferPool) Get() *bytes.Buffer

Get #

func (*BufferPool) Put

func (bpool *BufferPool) Put(b *bytes.Buffer)

Put #

type Context

type Context struct {
	HandlerContext *HandlerContext
	Req            *http.Request
	Res            http.ResponseWriter
	// contains filtered or unexported fields
}

Context provides context for whole request/response cycle It helps to pass variable from one middlware to another

func (*Context) AddHeader

func (c *Context) AddHeader(key string, value string)

AddHeader add header to the response

func (*Context) ClientIP

func (c *Context) ClientIP() string

ClientIP returns ip address of the user using request info

func (*Context) DefaultGet

func (c *Context) DefaultGet(key interface{}, defaultv interface{}) interface{}

DefaultGet #

func (*Context) DefaultParam

func (c *Context) DefaultParam(key string, defaultv string) string

DefaultParam #

func (*Context) DefaultQuery

func (c *Context) DefaultQuery(key string, defaultv string) string

DefaultQuery #

func (*Context) Error

func (c *Context) Error(err error)

func (*Context) Errors

func (c *Context) Errors(err ...error)

Errors records error to be displayed later

func (*Context) Get

func (c *Context) Get(key interface{}) interface{}

Get #

func (*Context) GetComplex128

func (c *Context) GetComplex128(key interface{}) complex128

GetComplex128 gets values associated with key as complex128

func (*Context) GetFloat64

func (c *Context) GetFloat64(key interface{}) float64

GetFloat64 gets values associated with key as float64

func (*Context) GetHeader

func (c *Context) GetHeader(key string) string

GetHeader returns request header shortcut for c.Rewq

func (*Context) GetInt64

func (c *Context) GetInt64(key interface{}) int64

GetInt64 gets values associated with key as int64

func (*Context) GetString

func (c *Context) GetString(key interface{}) string

GetString gets string value using key in context

func (*Context) JSON

func (c *Context) JSON(code int, response interface{})

JSON #

func (*Context) MintParam

func (c *Context) MintParam(key string) (interface{}, bool)

MintParam #

func (*Context) Next

func (c *Context) Next()

Next runs the next handler

func (*Context) Param

func (c *Context) Param(key string) (string, bool)

Param #

func (*Context) ParamMap

func (c *Context) ParamMap() map[string]string

ParamMap #

func (*Context) Query

func (c *Context) Query(key string) (string, bool)

Query #

func (*Context) QueryArray

func (c *Context) QueryArray(key string) ([]string, bool)

QueryArray #

func (*Context) QueryValues

func (c *Context) QueryValues() url.Values

QueryValues #

func (*Context) Reset

func (c *Context) Reset()

Reset resets the value the context

func (*Context) Set

func (c *Context) Set(key, val interface{})

Set #

func (*Context) SetHeader

func (c *Context) SetHeader(key string, value []string)

SetHeader #

func (*Context) SetMintParam

func (c *Context) SetMintParam(key string, value interface{})

SetMintParam #

func (*Context) Status

func (c *Context) Status(status int)

Status set http status code'

func (*Context) URI

func (c *Context) URI() string

URI gets request uri

type HandlerContext

type HandlerContext struct {
	Mint *Mint
	// contains filtered or unexported fields
}

HandlerContext #

func HandlerBuilder

func HandlerBuilder() *HandlerContext

HandlerBuilder new handerContext

func (*HandlerContext) Compressed

func (hc *HandlerContext) Compressed(isCompressed bool) *HandlerContext

Compressed #

func (*HandlerContext) Handle

func (hc *HandlerContext) Handle(handlers ...HandlerFunc) *HandlerContext

Handle #

func (*HandlerContext) Headers

func (hc *HandlerContext) Headers(headers ...string) *HandlerContext

Headers #

func (*HandlerContext) Methods

func (hc *HandlerContext) Methods(methods ...string) *HandlerContext

Methods #

func (*HandlerContext) Name

func (hc *HandlerContext) Name(name string) *HandlerContext

Name #

func (*HandlerContext) Path

func (hc *HandlerContext) Path(path string) *HandlerContext

Path #

func (*HandlerContext) Queries

func (hc *HandlerContext) Queries(queries ...string) *HandlerContext

Queries #

func (*HandlerContext) Schemes

func (hc *HandlerContext) Schemes(schemes ...string) *HandlerContext

Schemes #

func (*HandlerContext) ServeHTTP

func (hc *HandlerContext) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP #

func (*HandlerContext) Use

func (hc *HandlerContext) Use(handler ...HandlerFunc) *HandlerContext

Use registers middleware

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc handles requests for an URL

type Handlers

type Handlers []*HandlerContext

Handlers chain of Handler

type HandlersGroup

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

HandlersGroup #

func NewGroup

func NewGroup(pathPrefix string) *HandlersGroup

NewGroup creates new handlers Group

func (*HandlersGroup) AddGroup

func (hg *HandlersGroup) AddGroup(newhg *HandlersGroup) *HandlersGroup

AddGroup add new subgroup

func (*HandlersGroup) AddGroups

func (hg *HandlersGroup) AddGroups(hgs []*HandlersGroup) *HandlersGroup

AddGroups adds new subgroups

func (*HandlersGroup) ChainGroups

func (hg *HandlersGroup) ChainGroups(groups []*HandlersGroup) *HandlersGroup

ChainGroups chains groups in linear

func (*HandlersGroup) Group

func (hg *HandlersGroup) Group(pathPrefix string) *HandlersGroup

Group creates new subgroup

func (*HandlersGroup) Handler

func (hg *HandlersGroup) Handler(hc *HandlerContext) *HandlersGroup

Handler registers new Handler

func (*HandlersGroup) Handlers

func (hg *HandlersGroup) Handlers(hsc []*HandlerContext) *HandlersGroup

Handlers registers chain of handlers

func (*HandlersGroup) Headers

func (hg *HandlersGroup) Headers(headers ...string) *HandlersGroup

Headers #

func (*HandlersGroup) PrefixHandler

func (hg *HandlersGroup) PrefixHandler(hc *HandlerContext)

PrefixHandler registers handler for prefix request

func (*HandlersGroup) Queries

func (hg *HandlersGroup) Queries(queries ...string) *HandlersGroup

Queries #

func (*HandlersGroup) Schemes

func (hg *HandlersGroup) Schemes(schemes ...string) *HandlersGroup

Schemes #

func (*HandlersGroup) SimpleHandler

func (hg *HandlersGroup) SimpleHandler(path string, method string, handler ...HandlerFunc) *HandlerContext

SimpleHandler registers simple handler

func (*HandlersGroup) Use

func (hg *HandlersGroup) Use(handler ...HandlerFunc) *HandlersGroup

Use register new middleware

type JSON

type JSON map[string]interface{}

JSON basic json type

type Logger

type Logger struct {
	TimeStamp  time.Time
	StatusCode int
	Latency    time.Duration
	Method     string
	Path       string
	BodySize   int
	ClientIP   string
	UserName   string
	Errors     []error
}

Logger logger structure

func NewLogger

func NewLogger() *Logger

NewLogger creates a new logger

func (*Logger) Print

func (l *Logger) Print()

Print prints log

type Mint

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

Mint is framework's instance, it contains default middleware, DB, handlers configuration Create Intance of Mint using New() method

func From

func From(r Router, mt *Mint) *Mint

From registers router to mt

func New

func New() *Mint

New creates new application

func Simple

func Simple() *Mint

Simple creates new application without any defualt handlers

func (*Mint) AddGroup

func (mt *Mint) AddGroup(hg *HandlersGroup) *Mint

AddGroup adds a group to router

func (*Mint) AddGroups

func (mt *Mint) AddGroups(hgs []*HandlersGroup) *Mint

AddGroups adds a groups of handlers

func (*Mint) Build

func (mt *Mint) Build() *mux.Router

Build the application

func (*Mint) ChainGroups

func (mt *Mint) ChainGroups(groups []*HandlersGroup) *Mint

ChainGroups chains groups in linear

func (*Mint) DELETE

func (mt *Mint) DELETE(path string, handler HandlerFunc) *HandlerContext

DELETE register simple delete handler

func (*Mint) GET

func (mt *Mint) GET(path string, handler HandlerFunc) *HandlerContext

GET register get handler

func (*Mint) Get

func (mt *Mint) Get(key string) (interface{}, bool)

Get the value from store by key

func (*Mint) Group

func (mt *Mint) Group(pathPrefix string) *HandlersGroup

Group creates new group handlers W

func (*Mint) HandleStatic

func (mt *Mint) HandleStatic(path string, dir string)

HandleStatic registers a new handler to handle static content such as img, css, html, js.

func (*Mint) Handler

func (mt *Mint) Handler(hc *HandlerContext) *Mint

Handler registers single handlers context

func (*Mint) Handlers

func (mt *Mint) Handlers(hsc []*HandlerContext) *Mint

Handlers registers multiple handlers context

func (*Mint) MethodNotAllowedHandler

func (mt *Mint) MethodNotAllowedHandler(hc *HandlerContext)

MethodNotAllowedHandler registers method not allowed handler

func (*Mint) NotFoundHandler

func (mt *Mint) NotFoundHandler(hc *HandlerContext)

NotFoundHandler registers not found handler context

func (*Mint) POST

func (mt *Mint) POST(path string, handler HandlerFunc) *HandlerContext

POST registers post handler

func (*Mint) PUT

func (mt *Mint) PUT(path string, handler HandlerFunc) *HandlerContext

PUT register simple PUT handler

func (*Mint) Path

func (mt *Mint) Path(path string) *HandlerContext

Path sets URL Path to handler

func (*Mint) Run

func (mt *Mint) Run(serverAdd string)

Run runs application

func (*Mint) Set

func (mt *Mint) Set(key string, value interface{})

Set the value to store with key

func (*Mint) SimpleHandler

func (mt *Mint) SimpleHandler(path string, method string, handler ...HandlerFunc) *HandlerContext

SimpleHandler registers simple handler

func (*Mint) StrictSlash

func (mt *Mint) StrictSlash(strictSlash bool) *Mint

StrictSlash enable strictslash in router

func (*Mint) Use

func (mt *Mint) Use(handler ...HandlerFunc)

Use register new middleware

type Router

type Router *mux.Router

Router route

func NewRouter

func NewRouter() Router

NewRouter creates new router for application

Jump to

Keyboard shortcuts

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