kamux

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: BSD-3-Clause Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GET int = iota
	POST
	PUT
	PATCH
	DELETE
	WS
	SSE
)

Variables

View Source
var COOKIE_EXPIRE = time.Now().Add(7 * 24 * time.Hour)

COOKIE_EXPIRE global cookie expiry time

View Source
var MultipartSize = 10 << 20
View Source
var Static embed.FS
View Source
var Templates embed.FS

Functions

func LoadTranslations

func LoadTranslations()

Types

type Context

type Context struct {
	http.ResponseWriter
	*http.Request
	Params map[string]string
	// contains filtered or unexported fields
}

Context is a wrapper of responseWriter, request, and params map

func (*Context) BodyJson added in v0.5.2

func (c *Context) BodyJson() map[string]any

BodyJson get json body from request and return map

func (*Context) DeleteCookie

func (c *Context) DeleteCookie(key string)

DeleteCookie delete cookie with specific key

func (*Context) DeleteFile

func (c *Context) DeleteFile(path string) error

DELETE FILE

func (*Context) Download

func (c *Context) Download(data_bytes []byte, asFilename string)

Download download data_bytes(content) asFilename(test.json,data.csv,...) to the client

func (*Context) EnableTranslations

func (c *Context) EnableTranslations()

EnableTranslations get user ip, then location country using nmap, so don't use it if u don't have it install, and then it parse csv file to find the language spoken in this country, to finaly set cookie 'lang' to 'en' or 'fr'...

func (*Context) GetCookie

func (c *Context) GetCookie(key string) (string, error)

GetCookie get cookie with specific key

func (*Context) GetUserIP

func (c *Context) GetUserIP() string

func (*Context) Html added in v0.6.0

func (c *Context) Html(template_name string, data map[string]any)

Html return template_name with data to the client

func (*Context) Json added in v0.6.0

func (c *Context) Json(body any)

Json return json to the client

func (*Context) JsonIndent added in v0.6.0

func (c *Context) JsonIndent(code int, body any)

JsonIndent return json indented to the client

func (*Context) QueryParam

func (c *Context) QueryParam(name string) string

QueryParam get query param

func (*Context) Redirect added in v0.6.0

func (c *Context) Redirect(path string)

Redirect redirect the client to the specified path with a custom code

func (*Context) ServeEmbededFile

func (c *Context) ServeEmbededFile(content_type string, embed_file []byte)

ServeEmbededFile serve an embeded file from handler

func (*Context) ServeFile

func (c *Context) ServeFile(content_type, path_to_file string)

ServeFile serve a file from handler

func (*Context) SetCookie

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

SetCookie set cookie given key and value

func (*Context) Status added in v0.6.0

func (c *Context) Status(code int) *Context

set status to context, will not be writed to header

func (*Context) StreamResponse

func (c *Context) StreamResponse(response string) error

StreamResponse send SSE Streaming Response

func (*Context) Text added in v0.6.0

func (c *Context) Text(body string)

Text return text with custom code to the client

func (*Context) UploadFile

func (c *Context) UploadFile(received_filename, folder_out string, acceptedFormats ...string) (string, []byte, error)

UploadFile upload received_filename into folder_out and return url,fileByte,error

func (*Context) UploadFiles added in v0.5.0

func (c *Context) UploadFiles(received_filenames []string, folder_out string, acceptedFormats ...string) ([]string, [][]byte, error)

type Handler

type Handler func(c *Context)

Handler

type Route

type Route struct {
	Method  string
	Pattern *regexp.Regexp
	Handler
	WsHandler
	Clients         map[string]*websocket.Conn
	AllowedOrigines []string
}

Route

type Router

type Router struct {
	Routes       map[int][]Route
	DefaultRoute Handler
	Server       *http.Server
}

Router

func New

func New(envFiles ...string) *Router

New Create New Router from env file default: '.env'

func (*Router) AddEmbededTemplates

func (router *Router) AddEmbededTemplates(template_embed embed.FS, rootDir string) error

func (*Router) AddLocalTemplates

func (router *Router) AddLocalTemplates(pathToDir string) error

func (*Router) DELETE

func (router *Router) DELETE(pattern string, handler Handler, allowed_origines ...string)

DELETE handle DELETE to a route

func (*Router) Embed

func (r *Router) Embed(staticDir *embed.FS, templateDir *embed.FS)

GetEmbeded get embeded files and make them global

func (*Router) GET

func (router *Router) GET(pattern string, handler Handler)

GET handle GET to a route

func (*Router) LoadEnv

func (router *Router) LoadEnv(files ...string)

LoadEnv load env vars from multiple files

func (*Router) NewFuncMap

func (router *Router) NewFuncMap(funcName string, function any)

func (*Router) PATCH

func (router *Router) PATCH(pattern string, handler Handler, allowed_origines ...string)

PATCH handle PATCH to a route

func (*Router) POST

func (router *Router) POST(pattern string, handler Handler, allowed_origines ...string)

POST handle POST to a route

func (*Router) PUT

func (router *Router) PUT(pattern string, handler Handler, allowed_origines ...string)

PUT handle PUT to a route

func (*Router) PrintServerStart added in v0.5.3

func (router *Router) PrintServerStart()

func (*Router) Run

func (router *Router) Run()

Run start the server

func (*Router) RunTLS

func (router *Router) RunTLS(certFile string, keyFile string)

RunTLS start the server TLS

func (*Router) SSE

func (router *Router) SSE(pattern string, handler Handler, allowed_origines ...string)

Delete handle DELETE to a route

func (*Router) ServeEmbededDir

func (router *Router) ServeEmbededDir(pathLocalDir string, embeded embed.FS, webPath string)

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serveHTTP by handling methods,pattern,and params

func (*Router) ServeLocalDir

func (router *Router) ServeLocalDir(dirPath, webPath string)

func (*Router) UseMiddlewares

func (router *Router) UseMiddlewares(midws ...func(http.Handler) http.Handler)

UseMiddlewares chain global middlewares applied on the router

func (*Router) WS

func (router *Router) WS(pattern string, wsHandler WsHandler, allowed_origines ...string)

WS handle WS connection on a pattern

type WsContext

type WsContext struct {
	Ws     *websocket.Conn
	Params map[string]string
	Route
}

func (*WsContext) AddClient

func (c *WsContext) AddClient(key string)

AddClient add client to clients_list

func (*WsContext) Broadcast added in v0.6.0

func (c *WsContext) Broadcast(data any) error

Broadcast send message to all clients in c.Clients

func (*WsContext) BroadcastExceptCaller

func (c *WsContext) BroadcastExceptCaller(data map[string]any) error

BroadcastExceptCaller send message to all clients in c.Clients

func (*WsContext) Json added in v0.6.0

func (c *WsContext) Json(data map[string]any) error

Json send json to the client

func (*WsContext) ReceiveJson

func (c *WsContext) ReceiveJson() (map[string]any, error)

ReceiveJson receive json from ws and disconnect when stop receiving

func (*WsContext) ReceiveText

func (c *WsContext) ReceiveText() (string, error)

ReceiveText receive text from ws and disconnect when stop receiving

func (*WsContext) RemoveRequester

func (c *WsContext) RemoveRequester(name ...string)

RemoveRequester remove the client from Clients list in context

func (*WsContext) Text added in v0.6.0

func (c *WsContext) Text(data string) error

Text send text to the client

type WsHandler

type WsHandler func(c *WsContext)

Jump to

Keyboard shortcuts

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