toushi

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: MIT Imports: 16 Imported by: 0

README

toushi

toushi 投石问路 Godoc

Description

A simple http router.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoToken = errors.New("no token")

ErrNoToken is returned when a token is not found in the request

View Source
var MethodNotAllowed http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
	msg := fmt.Sprintf("the %s mehtod is not supported for this resource", r.Method)
	errResponse(http.StatusMethodNotAllowed, msg)
}

MethodNotAllowed is a handler for method not found

Functions

func FormFile added in v0.0.17

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

FormFile returns the first file for the provided form key.

func GetBeareToken added in v0.0.14

func GetBeareToken(r *http.Request, name string) (string, error)

GetBearerToken returns the bearer token from the request

func GetToken added in v0.0.14

func GetToken(r *http.Request, name string) (string, error)

GetToken returns the token from the request

func GetValue added in v0.0.14

func GetValue(r *http.Request, key interface{}) interface{}

GetValue gets a value from the request context.

func HealthCheck added in v0.0.13

func HealthCheck(w http.ResponseWriter, r *http.Request)

HealthCheckHandler is a handler for health check

func OKJSON added in v0.0.7

func OKJSON(w http.ResponseWriter, data interface{})

OKJSON handle 200 respose

func OKText added in v0.0.15

func OKText(w http.ResponseWriter, text string)

OKText handle 200 respose

func ReadCSVQuery added in v0.0.14

func ReadCSVQuery(r *http.Request, key string, defaultValue []string) []string

ReadCSVQuery returns the csv list query value with a defaut list from the request

func ReadInt64Param added in v0.0.14

func ReadInt64Param(r *http.Request, name string) (int64, error)

ReadInt64Param returns the int64 param value in the request path

func ReadInt64Query added in v0.0.14

func ReadInt64Query(r *http.Request, key string, defaultValue int64) int64

ReadInt64Query returns the int64 query value with a defaut value from the request

func ReadJSON

func ReadJSON(w http.ResponseWriter, r *http.Request, dst interface{}) error

ReadJSON reads the request body and unmarshal it to the given struct, max size is 8MB

func ReadParams added in v0.0.7

func ReadParams(r *http.Request, name string) string

ReadParam returns the string param value in the request path

func ReadQuery added in v0.0.14

func ReadQuery(r *http.Request, key string, defaultValue string) string

ReadQuery returns the string query value with a defaut value from the request

func SaveUploadedFile added in v0.0.17

func SaveUploadedFile(file *multipart.FileHeader, dst string) error

SaveUploadedFile uploads the form file to specific dst.

func SetValue added in v0.0.14

func SetValue(r *http.Request, key, value interface{}) *http.Request

SetValue sets a value on the returned request.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data interface{}, headers http.Header)

writeJSON writes a JSON object to the response.

func WriteStr added in v0.0.7

func WriteStr(w http.ResponseWriter, status int, msg string, headers http.Header)

WriteStr writes a string to the response.

Types

type Config

type Config struct {
	Limiter struct {
		Rps     float64
		Burst   int
		Enabled bool
	}
	CORS struct {
		TrustedOrigins []string
	}
	Metrics bool
}

Config is the configuration for the router

func DefaultConf

func DefaultConf() *Config

DefaultConf return the default config

type Envelope added in v0.0.2

type Envelope map[string]interface{}

Envelope is a JSON envelope for better client response

type HandleHelper added in v0.1.0

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

HandleHelper is a handler for all requests

func NewHandleHelper added in v0.1.0

func NewHandleHelper(w http.ResponseWriter, r *http.Request) *HandleHelper

NewHandleHelper returns a new handler

func (*HandleHelper) AuthenticationRequire added in v0.1.0

func (h *HandleHelper) AuthenticationRequire()

AuthenticationRequire handle 401 response

func (*HandleHelper) BadRequestErr added in v0.1.0

func (h *HandleHelper) BadRequestErr(err error)

BadRequestErr handle 400 response with a error

func (*HandleHelper) BadRequestMsg added in v0.1.0

func (h *HandleHelper) BadRequestMsg(msg string)

HandleBadRequest handle 400 response with custom message

func (*HandleHelper) EditConflict added in v0.1.0

func (h *HandleHelper) EditConflict()

EditConflict handle 409 response

func (*HandleHelper) FailedValidation added in v0.1.0

func (h *HandleHelper) FailedValidation(errs map[string]string)

FailedValidation handle 400 response

func (*HandleHelper) InactiveAccount added in v0.1.0

func (h *HandleHelper) InactiveAccount()

InactiveAccount handle 403 response

func (*HandleHelper) InvalidAuthenticationToken added in v0.1.0

func (h *HandleHelper) InvalidAuthenticationToken()

InvalidAuthenticationToken handle 401 response

func (*HandleHelper) InvalidCredentials added in v0.1.0

func (h *HandleHelper) InvalidCredentials()

InvalidCredentials handle 400 response

func (*HandleHelper) MethodNotAllow added in v0.1.0

func (h *HandleHelper) MethodNotAllow()

MethodNotAllow handle 405 response

func (*HandleHelper) NotFound added in v0.1.0

func (h *HandleHelper) NotFound()

NotFound handle 404 response

func (*HandleHelper) NotPermitted added in v0.1.0

func (h *HandleHelper) NotPermitted()

NotPermitted handle 403 response

func (*HandleHelper) RateLimitExceede added in v0.1.0

func (h *HandleHelper) RateLimitExceede()

RateLimitExceede handle 429 response

func (*HandleHelper) ServerErr added in v0.1.0

func (h *HandleHelper) ServerErr(err error)

ServerErr handle 500 response

type Middleware added in v0.0.11

type Middleware func(http.HandlerFunc) http.HandlerFunc

Middleware is a function that takes a http.HandlerFunc and returns a http.HandlerFunc.

type RouterGroup added in v0.0.9

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

RouterGroup is a group of routes

func DefaultRouterGroup added in v0.0.12

func DefaultRouterGroup() *RouterGroup

DefaultRouterGroup return a new routergroup with default router config

func NewRouterGroup added in v0.0.12

func NewRouterGroup(conf *Config) (*RouterGroup, error)

NewRouterGroup return a new routergroup

func (*RouterGroup) Delete added in v0.0.9

func (g *RouterGroup) Delete(path string, handler http.HandlerFunc)

Delete is a shortcut for NewHandler(http.MethodDelete, path, handler)

func (*RouterGroup) Get added in v0.0.9

func (g *RouterGroup) Get(path string, handler http.HandlerFunc)

Get is a shortcut for NewHandler(http.MethodGet, path, handler)

func (*RouterGroup) Group added in v0.0.9

func (g *RouterGroup) Group(path string, mds ...Middleware) *RouterGroup

Group add a prefix to all path, for each Gropu call prefix will accumulate while middleware don't

func (*RouterGroup) NewHandler added in v0.0.13

func (g *RouterGroup) NewHandler(method, path string, handler http.HandlerFunc)

NewHandler handle new http request

func (*RouterGroup) Patch added in v0.0.9

func (g *RouterGroup) Patch(path string, handler http.HandlerFunc)

Patch is a shortcut for NewHandler(http.MethodPatch, path, handler)

func (*RouterGroup) Post added in v0.0.9

func (g *RouterGroup) Post(path string, handler http.HandlerFunc)

Post is a shortcut for NewHandler(http.MethodPost, path, handler)

func (*RouterGroup) Put added in v0.0.9

func (g *RouterGroup) Put(path string, handler http.HandlerFunc)

Put is a shortcut for NewHandler(http.MethodPut, path, handler)

func (*RouterGroup) Routes added in v0.0.10

func (g *RouterGroup) Routes(middlewares ...Middleware) http.Handler

Routes return http.Handler for http server must be call after add all custum path handler

func (*RouterGroup) Static added in v0.0.17

func (g *RouterGroup) Static(prefix string, dst string)

Static is a shortcut for NewHandler(http.MethodDelete, path, handler)

Jump to

Keyboard shortcuts

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