pine

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2024 License: MIT Imports: 33 Imported by: 24

README

pine

PineFramework 一个轻量级高性能GO语言开发框架。支持MVC、依赖注入、动态返回值、中间件、 路由分组、子域名路由注册管理。 组件基于接口实现,可以自行实现或定义组件。

动态返回值

此功能只能用于mvc模式,根据方法自动兼容显示内容

  1. 如果没有返回值,并且没有渲染过模板,会自动调用模板渲染方法. 查找路径为 ControllerName/MethodName
  2. 如果返回inerface{} ,会自动打印部分能兼容的数据,返回结果为字符串类型 text/html
  3. 如果返回一个非nil的错误,会直接panic(不包括复合类型里的error)
  4. 如果返回 string,int 等类型,显示为text
  5. 实现controller的结构属性全局静态变量共享(加锁/引用),需要共享的参数(名称以share切为引用类型)

di

(发现有些数据无法解析出来pkgPath,现在只有类型名称)服务注册名称更为interface{}, 如果注册服务类型实例,自动绑定字符串文件路径和pkgPathcontroller自动解析参数是对比参数pkgPath,以确定是否为真实参数类型。

路由

  • 可打印路由如下

$ go run main.go
METHOD | PATH | ALIASES | NAME     | HANDLE
------ | ---- | ------- | ----     | -------
GET    | /    |         | rootPath | path/to/routes_error/actions.HomeHandler

TODO

  • 支持非Post或Get开始的方法名称,支持任何方式请求
  • 自动反射出来的路由需要修改成小驼峰名称
  • 细化input(参照laravel), 抽象convert结构体, 处理各种数据转换.
  • 模板引擎支持传入文件系统对象
  • 实现路由占位符用于自定义路由替换规则
  • 缓存驱动github.com/syndtr/goleveldb/leveldb

Documentation

Index

Constants

View Source
const (
	HeaderContentType = fasthttp.HeaderContentType
	ContentTypeJSON   = "application/json; charset=utf-8"
	ContentTypeHTML   = "text/html; charset=utf-8"
	ContentTypeText   = "text/plain; charset=utf-8"
	ContentTypeXML    = "text/xml; charset=utf-8"
)
View Source
const FilePathParam = "filepath"
View Source
const GoRawBody = "pine://input"
View Source
const Version = "dev 0.1.0"

Variables

View Source
var (
	DefaultErrTemplate = template.Must(template.New("ErrTemplate").Parse(`<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width,initial-scale=1"><title>{{.Code}}|{{.Message}}</title><style type="text/css">body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif}h1{line-height:1;color:#252427;display:inline-block;border-right:1px solid rgba(0,0,0,.3);margin:0;margin-right:20px;padding:10px 23px 10px 0;font-size:24px;font-weight:500;vertical-align:top}h2{margin:100px 0 0;font-weight:600;letter-spacing:0.1em;color:#A299AC;text-transform:uppercase}</style></head><body><div style="color:#000;background:#fff;font-family:-apple-system, BlinkMacSystemFont, Roboto, 'Segoe UI', 'Fira Sans', Avenir, 'Helvetica Neue', 'Lucida Grande', sans-serif;height:100vh;text-align:center;display:flex;flex-direction:column;align-items:center;justify-content:center"><div><style>body{margin:0}</style><h1>{{.Code}}</h1><div style="display:inline-block;text-align:left;line-height:49px;height:49px;vertical-align:middle"><h2 style="font-size:14px;font-weight:normal;line-height:inherit;margin:0;padding:0">{{.Message}}</h2></div></div></div></body></html>`))
)
View Source
var EmptyBytes = []byte("")
View Source
var ErrKeyNotFound = errors.New("key not found")

Functions

func Logger

func Logger() logger.AbstractLogger

Logger 获取日志实例

func Make

func Make(service interface{}, params ...interface{}) interface{}

Make 获取给定参数的实例

func RegisterCodeHandler added in v0.0.6

func RegisterCodeHandler(status int, handler Handler)

func RegisterOnInterrupt

func RegisterOnInterrupt(handler func())

func RegisterViewEngine

func RegisterViewEngine(engine render.AbstractRenderer)

func SetControllerDefaultAction added in v0.0.6

func SetControllerDefaultAction(str string)

Types

type AbstractReadonlyConfiguration

type AbstractReadonlyConfiguration interface {
	GetServerName() string
	GetUseCookie() bool
	GetMaxMultipartMemory() int64
	GetAutoParseControllerResult() bool
	GetCookieTranscoder() sessions.AbstractCookieTranscoder
	GetDefaultResponseType() string
	GetCompressGzip() bool
	GetTimeout() TimeoutConf
}

type AbstractRouter

type AbstractRouter interface {
	AddRoute(method, path string, handle Handler, mws ...Handler)

	ANY(path string, handle Handler, mws ...Handler)
	GET(path string, handle Handler, mws ...Handler)
	POST(path string, handle Handler, mws ...Handler)
	HEAD(path string, handle Handler, mws ...Handler)
	PUT(path string, handle Handler, mws ...Handler)
	DELETE(path string, handle Handler, mws ...Handler)

	StaticFile(string, string, ...Handler)
	Static(string, string, ...int)
}

type Application

type Application struct {
	*Router

	DI di.AbstractBuilder

	ReadonlyConfiguration AbstractReadonlyConfiguration
	// contains filtered or unexported fields
}

func App added in v0.0.6

func App() *Application

App 获取应用实例

func New

func New() *Application

func (*Application) Close added in v0.0.6

func (a *Application) Close()

func (*Application) NotAllowMethod added in v0.0.6

func (a *Application) NotAllowMethod(handler Handler)

func (*Application) Run

func (a *Application) Run(srv ServerHandler, opts ...Configurator)

func (*Application) SetNotFound

func (a *Application) SetNotFound(handler Handler)

func (*Application) SetRecoverHandler

func (a *Application) SetRecoverHandler(handler Handler)

type Configuration

type Configuration struct {
	CookieTranscoder sessions.AbstractCookieTranscoder
	// contains filtered or unexported fields
}

func (*Configuration) GetAutoParseControllerResult

func (c *Configuration) GetAutoParseControllerResult() bool

func (*Configuration) GetCompressGzip added in v0.0.6

func (c *Configuration) GetCompressGzip() bool

func (*Configuration) GetCookieTranscoder

func (c *Configuration) GetCookieTranscoder() sessions.AbstractCookieTranscoder

func (*Configuration) GetDefaultResponseType added in v0.0.6

func (c *Configuration) GetDefaultResponseType() string

func (*Configuration) GetMaxMultipartMemory

func (c *Configuration) GetMaxMultipartMemory() int64

func (*Configuration) GetServerName

func (c *Configuration) GetServerName() string

func (*Configuration) GetTimeout added in v0.0.6

func (c *Configuration) GetTimeout() TimeoutConf

func (*Configuration) GetUseCookie added in v0.0.4

func (c *Configuration) GetUseCookie() bool

type Configurator

type Configurator func(o *Configuration)

func WithAutoParseControllerResult

func WithAutoParseControllerResult(auto bool) Configurator

func WithCompressGzip added in v0.0.6

func WithCompressGzip(enable bool) Configurator

func WithCookie added in v0.0.4

func WithCookie(open bool) Configurator

func WithCookieTranscoder

func WithCookieTranscoder(transcoder sessions.AbstractCookieTranscoder) Configurator

func WithDefaultResponseType added in v0.0.6

func WithDefaultResponseType(responseType string) Configurator

func WithGracefulShutdown added in v0.0.6

func WithGracefulShutdown() Configurator

func WithMaxMultipartMemory

func WithMaxMultipartMemory(mem int64) Configurator

func WithServerName

func WithServerName(srvName string) Configurator

func WithTimeout added in v0.0.6

func WithTimeout(conf TimeoutConf) Configurator

func WithTlsFile added in v0.0.6

func WithTlsFile(key, secret string) Configurator

func WithoutStartupLog

func WithoutStartupLog(hide bool) Configurator

type Context

type Context struct {
	*fasthttp.RequestCtx

	// Temporary recording error information
	Msg string
	// contains filtered or unexported fields
}

func (*Context) Abort

func (c *Context) Abort(statusCode int, msg ...string)

func (Context) Add added in v0.0.6

func (i Context) Add(key string, value interface{})

Add 新增数据

func (Context) All added in v0.0.6

func (i Context) All() map[string]interface{}

All 返回所有数据

func (*Context) BindForm

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

func (*Context) BindJSON

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

func (Context) Clear added in v0.0.6

func (i Context) Clear()

Clear 清除数据

func (*Context) ClientIP

func (c *Context) ClientIP() string

func (*Context) Clone added in v0.0.6

func (c *Context) Clone()

func (Context) Del added in v0.0.6

func (i Context) Del(keys ...string)

Del 删除数据

func (Context) DelExcept added in v0.0.6

func (i Context) DelExcept(keys ...string)

DelExcept 删除指定keys之外的数据

func (Context) Files

func (i Context) Files(key string) (*multipart.FileHeader, error)

func (Context) Get added in v0.0.6

func (i Context) Get(key string) interface{}

Get 获取数据

func (Context) GetBool

func (i Context) GetBool(key string, defaultVal ...bool) (val bool, err error)

func (Context) GetBytes added in v0.0.6

func (i Context) GetBytes(key string) ([]byte, error)

GetBytes 获取bytes数据, 仅个别类型

func (*Context) GetCookie

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

func (Context) GetDeep added in v0.0.6

func (i Context) GetDeep(key string) (*fastjson.Value, error)

GetDeep 深层获取value

func (Context) GetFloat64

func (i Context) GetFloat64(key string, defaultVal ...float64) (val float64, err error)

func (Context) GetForm added in v0.0.6

func (i Context) GetForm() *multipart.Form

func (Context) GetFormStrings added in v0.0.6

func (i Context) GetFormStrings(key string) []string

func (Context) GetInt

func (i Context) GetInt(key string, defaultVal ...int) (val int, err error)

func (Context) GetInt64

func (i Context) GetInt64(key string, defaultVal ...int64) (val int64, err error)

func (Context) GetString

func (i Context) GetString(key string, defaultVal ...string) (val string, err error)

func (*Context) Handle

func (c *Context) Handle()

func (*Context) HandlerName added in v0.0.6

func (c *Context) HandlerName() string

func (Context) Has added in v0.0.6

func (i Context) Has(key string) bool

Has 是否存在某个key数据

func (*Context) Header

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

func (*Context) Input added in v0.0.6

func (c *Context) Input() *input

func (*Context) IsAjax

func (c *Context) IsAjax() bool

func (Context) IsJson added in v0.0.6

func (i Context) IsJson() bool

IsJson 判断是否为json提交

func (*Context) IsStopped

func (c *Context) IsStopped() bool

func (Context) LastErr added in v0.0.6

func (i Context) LastErr() error

func (*Context) Logger

func (c *Context) Logger() logger.AbstractLogger

func (*Context) LoggerEntity added in v0.0.6

func (c *Context) LoggerEntity() *logger.LogEntity

func (*Context) Next

func (c *Context) Next()

func (Context) Only added in v0.0.6

func (i Context) Only(keys ...string) map[string]interface{}

Only 获取指定Key的值

func (*Context) Params

func (c *Context) Params() Params

func (*Context) Path added in v0.0.4

func (c *Context) Path() string

func (Context) PostForm added in v0.0.6

func (i Context) PostForm() map[string][]string

func (*Context) Redirect

func (c *Context) Redirect(url string, statusHeader ...int)

func (*Context) RemoveCookie

func (c *Context) RemoveCookie(name string)

func (*Context) Render

func (c *Context) Render() *Render

func (Context) ResetFromContext added in v0.0.6

func (i Context) ResetFromContext()

func (*Context) SendFile

func (c *Context) SendFile(filepath string)

func (*Context) Session

func (c *Context) Session(sessIns ...sessions.AbstractSession) sessions.AbstractSession

func (*Context) Set

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

func (*Context) SetCookie

func (c *Context) SetCookie(name string, value string, maxAge int)

func (*Context) SetStatus

func (c *Context) SetStatus(statusCode int)

func (*Context) Stop

func (c *Context) Stop()

func (*Context) Value

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

func (*Context) Write added in v0.0.5

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

func (*Context) WriteHTMLBytes added in v0.0.5

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

func (*Context) WriteJSON added in v0.0.5

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

func (*Context) WriteString

func (c *Context) WriteString(str string) error

type Controller

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

func (*Controller) Ctx

func (c *Controller) Ctx() *Context

func (*Controller) Input added in v0.0.6

func (c *Controller) Input() *input

func (*Controller) Logger

func (c *Controller) Logger() logger.AbstractLogger

func (*Controller) Render

func (c *Controller) Render() *Render

func (*Controller) Session

func (c *Controller) Session() sessions.AbstractSession

func (*Controller) View

func (c *Controller) View(name string)

func (*Controller) ViewData

func (c *Controller) ViewData(key string, val interface{})

type H

type H map[string]interface{}

type Handler

type Handler func(ctx *Context)

type IController

type IController interface {
	Ctx() *Context
	Input() *input
	Render() *Render

	Logger() logger.AbstractLogger
	Session() sessions.AbstractSession
}

type IRegisterHandler

type IRegisterHandler interface {
	RegisterRoute(IRouterWrapper)
}

type IRouterWrapper

type IRouterWrapper interface {
	ANY(path string, handle string, mws ...Handler)
	GET(path string, handle string, mws ...Handler)
	POST(path string, handle string, mws ...Handler)
	PUT(path string, handle string, mws ...Handler)
	HEAD(path string, handle string, mws ...Handler)
	DELETE(path string, handle string, mws ...Handler)
	// contains filtered or unexported methods
}

type Params added in v0.0.6

type Params map[string]string

func (Params) Get added in v0.0.6

func (c Params) Get(key string) string

func (Params) GetBool added in v0.0.6

func (c Params) GetBool(key string, defaultVal bool) bool

func (Params) GetDefault added in v0.0.6

func (c Params) GetDefault(key, defaultVal string) string

func (Params) GetFloat64 added in v0.0.6

func (c Params) GetFloat64(key string, defaultVal ...float64) (val float64, err error)

func (Params) GetInt added in v0.0.6

func (c Params) GetInt(key string, defaultVal ...int) (val int, err error)

func (Params) GetInt64 added in v0.0.6

func (c Params) GetInt64(key string, defaultVal ...int64) (val int64, err error)

func (Params) Set added in v0.0.6

func (c Params) Set(key, value string)

type Render

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

func (*Render) Bytes

func (c *Render) Bytes(v []byte) error

func (*Render) ContentType

func (c *Render) ContentType(typ string)

func (*Render) GetEngine

func (c *Render) GetEngine(ext string) render.AbstractRenderer

func (*Render) GetViewData

func (c *Render) GetViewData() map[string]interface{}

func (*Render) HTML

func (c *Render) HTML(viewPath string)

func (*Render) JSON

func (c *Render) JSON(v interface{}) error

func (*Render) JSONP

func (c *Render) JSONP(callback string, v interface{}) error

func (*Render) Text

func (c *Render) Text(v string) error

func (*Render) ViewData

func (c *Render) ViewData(key string, val interface{})

func (*Render) XML

func (c *Render) XML(v interface{}) error

type RouteEntry

type RouteEntry struct {
	Method            string
	Middleware        []Handler
	ExtendsMiddleWare []Handler
	Handle            Handler
	HandlerName       string

	Param   []string
	Pattern string
	// contains filtered or unexported fields
}

type Router

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

func (*Router) ANY

func (r *Router) ANY(path string, handle Handler, mws ...Handler)

func (*Router) AddRoute

func (r *Router) AddRoute(method, path string, handle Handler, mws ...Handler)

func (*Router) DELETE

func (r *Router) DELETE(path string, handle Handler, mws ...Handler)

func (*Router) DumpRouteTable added in v0.0.6

func (r *Router) DumpRouteTable()

func (*Router) Favicon added in v0.0.6

func (r *Router) Favicon(file interface{})

func (*Router) GET

func (r *Router) GET(path string, handle Handler, mws ...Handler)

func (*Router) Group

func (r *Router) Group(prefix string, middleWares ...Handler) *Router

func (*Router) HEAD

func (r *Router) HEAD(path string, handle Handler, mws ...Handler)

func (*Router) Handle

func (r *Router) Handle(c IController, prefix ...string) *Router

func (*Router) POST

func (r *Router) POST(path string, handle Handler, mws ...Handler)

func (*Router) PUT

func (r *Router) PUT(path string, handle Handler, mws ...Handler)

func (*Router) Static

func (r *Router) Static(urlPath, dir string, stripSlashes ...int)

func (*Router) StaticFS added in v0.0.6

func (r *Router) StaticFS(urlPath string, f fs.FS, filePrefix string, indexfile ...string)

func (*Router) StaticFile

func (r *Router) StaticFile(path, file string, mws ...Handler)

func (*Router) Subdomain

func (r *Router) Subdomain(subdomain string) *Router

func (*Router) Use

func (r *Router) Use(middleWares ...Handler)

type RouterTableRow added in v0.0.6

type RouterTableRow struct {
	Method string `header:"METHOD"`
	Path   string `header:"PATH"`
	// Alias   string `header:"ALIASES"`
	// Name    string `header:"NAME"`
	Handler string `header:"HANDLER"`
}

type ServerHandler

type ServerHandler func(*Application) error

func Addr

func Addr(addr string) ServerHandler

type TimeoutConf added in v0.0.6

type TimeoutConf struct {
	Enable   bool
	Duration time.Duration
	Msg      string
}

Jump to

Keyboard shortcuts

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