regia

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: GPL-3.0 Imports: 24 Imported by: 0

README

regia

Regia is a web framework written with golang !

It is simple, helpful and with high performance. Build your own idea with it !

Installation

Golang version 1.11 + required

go get github.com/eatmoreapple/regia

Quick Start

$ touch main.go
# add all following codes into main.go
package main

import "github.com/eatmoreapple/regia"

func main() {
	engine := regia.New()
	engine.GET("/string", func(context *regia.Context) {
		context.String("string")
	})
	engine.POST("/json", func(context *regia.Context) {
		var body struct {
			Name string `json:"name"`
			Age  uint8  `json:"age"`
		}
		context.BindJSON(&body)
		context.JSON(body)
	})
	engine.Run(":8000")
}
$ go run main.go
# open your brower and visit `localhost:8000/`

Documentation

Index

Constants

View Source
const (
	MethodGet     = "Get"
	MethodPost    = "Post"
	MethodPut     = "Put"
	MethodPatch   = "Patch"
	MethodDelete  = "Delete"
	MethodHead    = "Head"
	MethodOptions = "Options"
	MethodTrace   = "Trace"
	ALLMethods    = "*"
)
View Source
const (
	FilePathParam = "static"
)

Variables

View Source
View Source
var (
	ContextKey = contextKey{}
)

ContextKey is the request context key under which Context are stored.

HttpRequestMethodMapping define request method mapping

Functions

func HandleNotFound

func HandleNotFound(context *Context)

func SetContextIntoRequest

func SetContextIntoRequest(ctx *Context)

SetContextIntoRequest set Context into request context

Types

type BannerStarter

type BannerStarter struct{ Banner string }

func (BannerStarter) Start

func (b BannerStarter) Start(engine *Engine) error

type BluePrint

type BluePrint struct {
	// Name the name of current BluePrint
	Name string

	// data of current BluePrint
	Data interface{}
	// contains filtered or unexported fields
}

func DefaultBluePrint added in v1.1.5

func DefaultBluePrint() *BluePrint

DefaultBluePrint returns default BluePrint It is used to create root BluePrint If you want to create child BluePrint, you should use NewBluePrint DefaultBluePrint add some default attributes to BluePrint Ensure that system could be worked

func NewBluePrint

func NewBluePrint() *BluePrint

NewBluePrint constructor for BluePrint

func (*BluePrint) ANY

func (b *BluePrint) ANY(path string, group ...HandleFunc)

ANY register all method for given handle

func (*BluePrint) Bind

func (b *BluePrint) Bind(path string, v interface{}, mappings ...map[string]string)

Bind legal handleFunc with given mappings from struct

func (*BluePrint) BindMethod

func (b *BluePrint) BindMethod(path string, v interface{}, mappings ...map[string]string)

BindMethod add HttpRequestMethodMapping for bind mappings

func (*BluePrint) DELETE

func (b *BluePrint) DELETE(path string, group ...HandleFunc)

DELETE is a shortcut for Handle("DELETE", path, group...)

func (*BluePrint) FileStorage added in v1.1.5

func (b *BluePrint) FileStorage() FileStorage

FileStorage set FileStorage If is nil, it will be panic

func (*BluePrint) GET

func (b *BluePrint) GET(path string, group ...HandleFunc)

GET is a shortcut for Handle("GET", path, group...)

func (*BluePrint) HEAD

func (b *BluePrint) HEAD(path string, group ...HandleFunc)

HEAD is a shortcut for Handle("HEAD", path, group...)

func (*BluePrint) HTMLLoader added in v1.1.5

func (b *BluePrint) HTMLLoader() HTMLLoader

HTMLLoader HTMLSerializer returns HTMLLoader If not set, it will try to get from parent BluePrint

func (*BluePrint) Handle

func (b *BluePrint) Handle(method, path string, group ...HandleFunc)

Handle register HandleFunc with given method and path

func (*BluePrint) Include

func (b *BluePrint) Include(prefix string, branch *BluePrint)

Include can add another BluePrint

func (*BluePrint) IsRoot added in v1.1.5

func (b *BluePrint) IsRoot() bool

IsRoot returns true if this is root BluePrint

func (*BluePrint) JSONSerializer added in v1.1.5

func (b *BluePrint) JSONSerializer() internal.Serializer

JSONSerializer returns JSONSerializer If not set, it will try to get from parent BluePrint

func (*BluePrint) OPTIONS

func (b *BluePrint) OPTIONS(path string, group ...HandleFunc)

OPTIONS is a shortcut for Handle("OPTIONS", path, group...)

func (*BluePrint) PATCH

func (b *BluePrint) PATCH(path string, group ...HandleFunc)

PATCH is a shortcut for Handle("PATCH", path, group...)

func (*BluePrint) POST

func (b *BluePrint) POST(path string, group ...HandleFunc)

POST is a shortcut for Handle("POST", path, group...)

func (*BluePrint) PUT

func (b *BluePrint) PUT(path string, group ...HandleFunc)

PUT is a shortcut for Handle("PUT", path, group...)

func (*BluePrint) Parent added in v1.1.5

func (b *BluePrint) Parent() *BluePrint

Parent returns parent BluePrint

func (*BluePrint) Parsers added in v1.1.5

func (b *BluePrint) Parsers() Parsers

Parsers returns Parsers

func (*BluePrint) RAW

func (b *BluePrint) RAW(method, path string, handlers ...http.HandlerFunc)

RAW register http.HandlerFunc with all method

func (*BluePrint) SetFileStorage added in v1.1.5

func (b *BluePrint) SetFileStorage(fileStorage FileStorage)

SetFileStorage set FileStorage If is nil, it will be panic

func (*BluePrint) SetHTMLLoader added in v1.1.5

func (b *BluePrint) SetHTMLLoader(htmlLoader HTMLLoader)

SetHTMLLoader set HTMLLoader If is nil, it will be panic

func (*BluePrint) SetJSONSerializer added in v1.1.5

func (b *BluePrint) SetJSONSerializer(jsonSerializer internal.Serializer)

SetJSONSerializer set JSONSerializer If is nil, it will be panic

func (*BluePrint) SetParsers added in v1.1.5

func (b *BluePrint) SetParsers(parsers Parsers)

SetParsers set Parsers If is nil, it will be panic

func (*BluePrint) SetPrefix

func (b *BluePrint) SetPrefix(path string)

SetPrefix add prefix for this BluePrint

func (*BluePrint) SetXMLSerializer added in v1.1.5

func (b *BluePrint) SetXMLSerializer(xmlSerializer internal.Serializer)

SetXMLSerializer set XMLSerializer If is nil, it will be panic

func (*BluePrint) Static

func (b *BluePrint) Static(url, dir string, group ...HandleFunc)

Static Serve static files

BluePrint.Static("/static/", "./static")

func (*BluePrint) Use

func (b *BluePrint) Use(group ...HandleFunc)

Use add middleware for this BluePrint

func (*BluePrint) XMLSerializer added in v1.1.5

func (b *BluePrint) XMLSerializer() internal.Serializer

XMLSerializer returns XMLSerializer If not set, it will try to get from parent BluePrint

type Context

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

func GetCurrentContext

func GetCurrentContext(req *http.Request) *Context

GetCurrentContext get current Context from the request

func (*Context) Abort

func (c *Context) Abort()

Abort skip current handle and will call Context.abort exit and do nothing by default

func (*Context) AbortHandler

func (c *Context) AbortHandler() HandleFunc

AbortHandler returns a handler which called at lasted

func (*Context) AbortWithJSON

func (c *Context) AbortWithJSON(data interface{})

AbortWithJSON write json response and exit

func (*Context) AbortWithStatus

func (c *Context) AbortWithStatus(code int)

AbortWithStatus set response status and exit

func (*Context) AbortWithString added in v1.0.8

func (c *Context) AbortWithString(text string, data ...interface{})

AbortWithString write string response and exit

func (*Context) AbortWithXML

func (c *Context) AbortWithXML(data interface{})

AbortWithXML write xml response and exit

func (*Context) Bind

func (c *Context) Bind(binder binders.Binder, v interface{}) error

Bind bind request to destination

func (*Context) BindForm

func (c *Context) BindForm(v interface{}) error

BindForm bind PostForm to destination

func (*Context) BindHeader added in v1.1.3

func (c *Context) BindHeader(v interface{}) error

BindHeader bind the request header to destination

func (*Context) BindJSON

func (c *Context) BindJSON(v interface{}) error

BindJSON bind the request body according to the format of json

func (*Context) BindMultipartForm

func (c *Context) BindMultipartForm(v interface{}) error

BindMultipartForm bind MultipartForm to destination

func (*Context) BindQuery

func (c *Context) BindQuery(v interface{}) error

BindQuery bind Query to destination

func (*Context) BindURI added in v1.1.5

func (c *Context) BindURI(v interface{}) error

BindURI bind the request uri to destination

func (*Context) BindXML

func (c *Context) BindXML(v interface{}) error

BindXML bind the request body according to the format of xml

func (*Context) BluePrint added in v1.1.5

func (c *Context) BluePrint() *BluePrint

BluePrint return current blueprint

func (*Context) ContentType added in v1.1.0

func (c *Context) ContentType() string

ContentType return Content-Type header

func (*Context) Data

func (c *Context) Data(v interface{}) (err error)

Data analysis request body to destination and validate Call Context.AddParser to add more support

func (*Context) Engine

func (c *Context) Engine() *Engine

Engine return Engine of current Context

func (*Context) Escape

func (c *Context) Escape()

Escape can let context not return to the pool

func (*Context) Flusher

func (c *Context) Flusher() http.Flusher

Flusher Make http.ResponseWriter as http.Flusher

func (*Context) Form

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

Form is a shortcut for c.Request.PostForm but value for current context

func (*Context) FormValue

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

FormValue get Value from post value

func (*Context) FormValues

func (c *Context) FormValues(key string) []string

FormValues get Values slice from post value

func (*Context) FullPath added in v1.1.0

func (c *Context) FullPath() string

FullPath return full path of current request

func (*Context) GetValue added in v1.0.2

func (c *Context) GetValue(key string) (value interface{}, exist bool)

GetValue get value from context

func (*Context) HTML added in v1.0.9

func (c *Context) HTML(name string, data interface{}) error

HTML write html response

func (*Context) IsAborted

func (c *Context) IsAborted() bool

IsAborted return that context is aborted

func (*Context) IsAjax

func (c *Context) IsAjax() bool

IsAjax check current if is an ajax request

func (*Context) IsEscape added in v1.0.6

func (c *Context) IsEscape() bool

IsEscape return escape status

func (*Context) IsMatched

func (c *Context) IsMatched() bool

IsMatched return that route matched

func (*Context) IsWebsocket

func (c *Context) IsWebsocket() bool

IsWebsocket returns true if the request headers indicate that a websocket

func (*Context) JSON

func (c *Context) JSON(data interface{}) error

JSON write json response

func (*Context) Next

func (c *Context) Next()

Next call handle

func (*Context) Params

func (c *Context) Params() Params

func (*Context) Query

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

Query is a shortcut for c.Request.URL.Query() but cached value for current context

func (*Context) QueryValue

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

QueryValue get Value from url query

func (*Context) QueryValues

func (c *Context) QueryValues(key string) []string

QueryValues get Value slice from url query

func (*Context) Redirect

func (c *Context) Redirect(code int, url string) error

Redirect Shortcut for http.Redirect

func (*Context) RemoteIP added in v1.1.6

func (c *Context) RemoteIP() string

RemoteIP return remote ip address

func (*Context) Render

func (c *Context) Render(render renders.Render, data interface{}) error

Render write response data with given Render

func (*Context) SaveUploadFile

func (c *Context) SaveUploadFile(name string) (string, error)

SaveUploadFile will call Context.FileStorage default save file to local path

func (*Context) SaveUploadFileWith

func (c *Context) SaveUploadFileWith(fs FileStorage, name string) (string, error)

SaveUploadFileWith call given FileStorage with upload file

func (*Context) ServeContent

func (c *Context) ServeContent(name string, modTime time.Time, content io.ReadSeeker) error

ServeContent Shortcut for http.ServeContent

func (*Context) ServeFile

func (c *Context) ServeFile(filepath, filename string) error

ServeFile Shortcut for http.ServeFile

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie is a shortcut for http.SetCookie

func (*Context) SetHeader

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

SetHeader set response header

func (*Context) SetStatus

func (c *Context) SetStatus(code int)

SetStatus set response status code SetStatus will not affect the response data that has been written

func (*Context) SetValue added in v1.0.2

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

SetValue set value to context

func (*Context) Status added in v1.1.7

func (c *Context) Status() int

Status get response status code

func (*Context) String

func (c *Context) String(format string, data ...interface{}) (err error)

String write string response

func (*Context) Write

func (c *Context) Write(data []byte) error

Write []byte into response writer

func (*Context) XML

func (c *Context) XML(data interface{}) error

XML write xml response

type Engine

type Engine struct {
	// BluePrint is used for store the handler.
	// All handlers are going to register into it
	*BluePrint

	// Router is a module used to register handle and distribute request
	Router HttpRouter

	// NotFoundHandle replies to the request with an HTTP 404 not found error.
	NotFoundHandle func(context *Context)

	// MultipartMemory defined max request body size
	MultipartMemory int64
	// contains filtered or unexported fields
}

Engine is a collection of core components of the whole service

func Default

func Default() *Engine

Default Engine for use

func New

func New() *Engine

New Constructor for Engine

func (*Engine) AddInterceptors

func (e *Engine) AddInterceptors(interceptors ...HandleFunc)

AddInterceptors Add interceptor to Engine All interceptors will be called before any handler Such as authorization, rate limiter, etc

func (*Engine) AddStarter

func (e *Engine) AddStarter(starters ...Starter)

AddStarter Add starter to Engine It will be called when the service starts

func (*Engine) ListenAndServe

func (e *Engine) ListenAndServe(addr string) error

func (*Engine) ListenAndServeTLS

func (e *Engine) ListenAndServeTLS(addr, certFile, keyFile string) error

ListenAndServeTLS acts identically to Run

func (*Engine) Run

func (e *Engine) Run(addr string) error

Run is a shortcut for ListenAndServe

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP implement http.Handle

func (*Engine) Server

func (e *Engine) Server() *http.Server

Server is a getter for Engine

type FileStorage

type FileStorage interface {
	Save(fileHeader *multipart.FileHeader) (string, error)
}

type FormParser

type FormParser struct{}

FormParser Parser for form data.

func (FormParser) Match

func (f FormParser) Match(context *Context) bool

func (FormParser) Parse

func (f FormParser) Parse(context *Context, v interface{}) error

type HTMLLoader added in v1.0.9

type HTMLLoader interface {
	Load(name string) (renders.Render, error) // Load a template by name
	ParseGlob(pattern string) error           // ParseGlob templates by pattern
}

type HandleFunc

type HandleFunc func(context *Context)

func Gzip added in v1.1.4

func Gzip(level int) HandleFunc

Gzip is a middleware for gzip compression it will compress the response body if the client accepts gzip encoding param is the compression level, choose from gzip.BestSpeed to gzip.BestCompression

func RawHandlerFunc

func RawHandlerFunc(handler http.HandlerFunc) HandleFunc

func RecoverHandler added in v1.1.0

func RecoverHandler(h func(ctx *Context, rec interface{})) HandleFunc

RecoverHandler recovers from panics and call given handler

type HandleFuncGroup

type HandleFuncGroup []HandleFunc

func RawHandlerFuncGroup

func RawHandlerFuncGroup(handlers ...http.HandlerFunc) HandleFuncGroup

type HttpRouter

type HttpRouter map[string]*routerNode

HttpRouter implement Router

func (HttpRouter) Insert

func (r HttpRouter) Insert(method, path string, handle handleFuncNodeGroup)

func (HttpRouter) Match

func (r HttpRouter) Match(ctx *Context) bool

type JsonParser

type JsonParser struct{}

JsonParser Parses JSON-serialized data.

func (JsonParser) Match

func (j JsonParser) Match(context *Context) bool

func (JsonParser) Parse

func (j JsonParser) Parse(context *Context, v interface{}) error

type LocalFileStorage

type LocalFileStorage struct {
	MediaRoot string
	// contains filtered or unexported fields
}

func (*LocalFileStorage) Save

func (l *LocalFileStorage) Save(fileHeader *multipart.FileHeader) (string, error)

Save implement FileStorage Uploads the form file to local

func (*LocalFileStorage) SetMediaRouter

func (l *LocalFileStorage) SetMediaRouter(mediaRoot string)

SetMediaRouter Set file base save path for LocalFileStorage

type Map

type Map map[string]interface{}

Map is a shortcut fot map[string]interface{}

type MultipartFormParser

type MultipartFormParser struct{}

MultipartFormParser Parser for multipart form data, which may include file data.

func (MultipartFormParser) Match

func (m MultipartFormParser) Match(context *Context) bool

func (MultipartFormParser) Parse

func (m MultipartFormParser) Parse(context *Context, v interface{}) error

type Param

type Param struct {
	Key   string
	Value string
}

type Params

type Params []Param

func (Params) Get

func (ps Params) Get(key string) string

func (Params) ToURLValues added in v1.1.5

func (ps Params) ToURLValues() url.Values

ToURLValues converts a Params to an url.Values This is useful for building a URL query string

type Parser

type Parser interface {
	// Parse parsed incoming byte stream and return an error if parse failed
	Parse(context *Context, v interface{}) error
	// Match define that if we should parse this request
	Match(context *Context) bool
}

type Parsers

type Parsers []Parser

func (Parsers) Parse

func (p Parsers) Parse(context *Context, v interface{}) error

Parse start to parse request data

type QueryParser added in v1.0.8

type QueryParser struct{}

func (QueryParser) Match added in v1.0.8

func (q QueryParser) Match(context *Context) bool

func (QueryParser) Parse added in v1.0.8

func (q QueryParser) Parse(context *Context, v interface{}) error

type Starter

type Starter interface {
	Start(engine *Engine) error
}

Starter will be called while engine is running

type TemplateLoader added in v1.0.9

type TemplateLoader struct {
	*template.Template
}

func (*TemplateLoader) Load added in v1.0.9

func (h *TemplateLoader) Load(name string) (renders.Render, error)

func (*TemplateLoader) ParseGlob added in v1.0.9

func (h *TemplateLoader) ParseGlob(pattern string) error

type UrlInfoStarter

type UrlInfoStarter struct{}

func (UrlInfoStarter) Start

func (u UrlInfoStarter) Start(engine *Engine) error

type XMLParser added in v1.0.6

type XMLParser struct{}

func (XMLParser) Match added in v1.0.6

func (x XMLParser) Match(context *Context) bool

func (XMLParser) Parse added in v1.0.6

func (x XMLParser) Parse(context *Context, v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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