ginx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: GPL-3.0 Imports: 17 Imported by: 3

README

ginx

Documentation

Index

Constants

View Source
const (
	ModeTest    = "test"
	ModeDebug   = "debug"
	ModeRelease = "release"
)

define gin run mode constant

Variables

This section is empty.

Functions

func NewApiHandler

func NewApiHandler(r Request, f ApiHandlerFunc, ms ...ApiMiddleware) gin.HandlerFunc

NewApiHandler create a new gin.HandlerFunc

func NewHandler

func NewHandler(f HandlerFunc) gin.HandlerFunc

NewHandler create a new gin.HandlerFunc, with no request, it's often been used to wrap a middleware

func NewPageHandler

func NewPageHandler(v *View, t string, r Request, f PageHandlerFunc, ms ...PageMiddleware) gin.HandlerFunc

NewPageHandler 创建一个页面处理方法 t - template of current page r - request f - handler func ms - middleware list, allow empty

func NewRequest

func NewRequest(r Request) interface{}

NewRequest create a new request by the given request

func RegisterPageInitFunc

func RegisterPageInitFunc(f PageInitFunc)

func RequestParams

func RequestParams(c *gin.Context) interface{}

RequestParams get current request content 此方法用于在日志记录时获取请求内容

func UseApiMiddleware

func UseApiMiddleware(ms ...ApiMiddleware)

UseApiMiddleware register global api middlewares

func UseApiResponser

func UseApiResponser(r ApiResponser)

UseApiResponser register a customized responser

func UseLogger

func UseLogger(l log.Logger)

UseLogger register a global logger

func UsePageMiddleware

func UsePageMiddleware(ms ...PageMiddleware)

UsePageMiddleware register global page middlewares

func Wait

func Wait(releaseFunc func())

Wait 信号监听,当监听到退出信号时,执行资源释放函数,并退出程序 f 程序退出前的资源释放方法

Types

type ApiError

type ApiError interface {
	error
	Code() int
}

ApiError the error should return an extra code that indicate which kind of error it is

type ApiHandlerFunc

type ApiHandlerFunc func(c *gin.Context, r Request) (Response, error)

ApiHandlerFunc the logic to handle the api request

type ApiMiddleware

type ApiMiddleware func(ApiHandlerFunc) ApiHandlerFunc

ApiMiddleware add middleware support to each single handler

type ApiResponser

type ApiResponser interface {
	// Response a common response
	Response(c *gin.Context, code int, v interface{})
	// Success show a success response
	Success(c *gin.Context, v interface{})
	// Fail show a fail response
	Fail(c *gin.Context, v interface{})
}

ApiResponser a responser was used to show request result to client

type Bucket

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

Bucket a bucket is a group of apis, like *gin.RouterGroup

func NewBucket

func NewBucket(r *gin.RouterGroup, handlers ...Handler) *Bucket

NewBucket create a new bucket

func (*Bucket) AddBucket

func (b *Bucket) AddBucket(b1 bucketer)

func (*Bucket) AddBuckets

func (b *Bucket) AddBuckets(bs []bucketer)

func (*Bucket) AddHandler

func (b *Bucket) AddHandler(h Handler)

func (*Bucket) AddHandlers

func (b *Bucket) AddHandlers(hs []Handler)

func (*Bucket) Group

func (b *Bucket) Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup

func (*Bucket) Register

func (b *Bucket) Register()

func (*Bucket) UseMiddlewares

func (b *Bucket) UseMiddlewares(ms ...gin.HandlerFunc)

type DefaultApiResponser

type DefaultApiResponser struct{}

DefaultApiResponser a default responser implements responser interface

func (DefaultApiResponser) Fail

func (r DefaultApiResponser) Fail(c *gin.Context, v interface{})

func (DefaultApiResponser) Response

func (r DefaultApiResponser) Response(c *gin.Context, code int, v interface{})

func (DefaultApiResponser) Success

func (r DefaultApiResponser) Success(c *gin.Context, v interface{})

type HTTPServer

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

HTTPServer define a simple http server

func NewServer

func NewServer(options *ServerOptions) *HTTPServer

NewServer create a http server

func (*HTTPServer) GinEngine

func (s *HTTPServer) GinEngine() *gin.Engine

func (*HTTPServer) PostInit

func (s *HTTPServer) PostInit(f ServerHookFunc)

func (*HTTPServer) PostStop

func (s *HTTPServer) PostStop(f ServerHookFunc)

func (*HTTPServer) PreStop

func (s *HTTPServer) PreStop(f ServerHookFunc)

func (*HTTPServer) Run

func (s *HTTPServer) Run() error

Run start http server in block mode

func (*HTTPServer) Runnable

func (s *HTTPServer) Runnable() bool

Runnable check whether server is runnable

func (*HTTPServer) Start

func (s *HTTPServer) Start() (bool, error)

Start start http server in non-blocked mode

func (*HTTPServer) Stop

func (s *HTTPServer) Stop()

Stop the server

func (*HTTPServer) Wait

func (s *HTTPServer) Wait()

Wait block and wait

type Handler

type Handler interface {
	// RegisterRoute register route internally
	RegisterRoute(g *gin.RouterGroup)
}

Handler a group of apis, support auto register routes to gin.RouterGroup

type HandlerFunc

type HandlerFunc func(c *gin.Context) error

HandlerFunc the logic to handle the api request

type Page

type Page struct {
	Ctx *gin.Context

	Request Request                // 页面请求数据
	Params  url.Values             // 请求参数列表
	Tpl     string                 `json:"tpl"`    // 定义模板
	Title   string                 `json:"title"`  // 页面标题
	Data    map[string]interface{} `json:"data"`   // 页面数据,可能会向用户展示
	Sess    map[string]interface{} `json:"Sess"`   // 保存会话数据,用于服务端业务处理,不对用户展示
	Errors  []*PageError           `json:"errors"` // 错误列表
	// contains filtered or unexported fields
}

Page 定义一个页面数据

func NewPage

func NewPage(c *gin.Context, v *View, tpl string) *Page

NewPage 创建一个Page对象

func NewPageWithData

func NewPageWithData(c *gin.Context, v *View, tpl string, data map[string]interface{}) *Page

NewPageWithData 创建一个Page对象,并初始化数据

func (*Page) AddData

func (p *Page) AddData(k string, d interface{})

AddData 添加数据

func (*Page) AddError

func (p *Page) AddError(e error)

AddError 添加错误信息

func (*Page) BatchAddData

func (p *Page) BatchAddData(d map[string]interface{})

BatchAddData 批量添加数据

func (*Page) ContentType

func (p *Page) ContentType() string

ContentType 获取请求的Content-Type

func (*Page) HasError

func (p *Page) HasError() bool

HasError 判断页面是否有错误

func (*Page) SetData

func (p *Page) SetData(d map[string]interface{})

SetData 设置页面数据

func (*Page) SetTitle

func (p *Page) SetTitle(t string)

SetTitle 设置页面标题

func (*Page) Show

func (p *Page) Show() error

Show 显示页面内容

func (*Page) ShowDirect

func (p *Page) ShowDirect()

ShowDirect 显示页面内容

func (*Page) ShowDirectWithError

func (p *Page) ShowDirectWithError(e interface{})

ShowDirectWithError 显示页面内容

func (*Page) ShowWithError

func (p *Page) ShowWithError(e interface{}) error

ShowWithError 将错误添加进页面并显示

type PageError

type PageError struct {
	Message string
	Trace   string
}

PageError 定义一个页面错误, 用于保存错误以及堆栈信息

func NewPageError

func NewPageError(e error) *PageError

func (*PageError) Error

func (pe *PageError) Error() string

type PageHandlerFunc

type PageHandlerFunc func(c *gin.Context, p *Page, r Request) error

PageHandlerFunc the logic to handle the page request

type PageInitFunc

type PageInitFunc func() map[string]interface{}

PageInitFunc 定义一个页面初始化方法,返回map[string]interface{},返回的数据将放到页面的会话数据(Page.Session)中

type PageMiddleware

type PageMiddleware func(PageHandlerFunc) PageHandlerFunc

PageMiddleware add middleware support to each single handler

type Request

type Request interface{}

Request a request from remote client

type Response

type Response interface{}

Response any response send to client

type ServerHookFunc

type ServerHookFunc func(r *gin.Engine) error

ServerHookFunc http server init & stop hooks

type ServerOptions

type ServerOptions struct {
	Port     int    `json:"port" yaml:"port" toml:"port"`                // 服务端口
	Mode     string `json:"mode" yaml:"mode" toml:"mode"`                // 运行模式, debug or release
	Tls      bool   `json:"tls" yaml:"tls" toml:"tls"`                   // 是否启用HTTPS
	CertFile string `json:"cert_file" yaml:"cert_file" toml:"cert_file"` // 证书文件
	KeyFile  string `json:"key_file" yaml:"key_file" toml:"key_file"`    // 密钥文件
}

ServerOptions http server run options

func DefaultServerOptions

func DefaultServerOptions() *ServerOptions

DefaultServerOptions create a default options

type ValidatableRequest

type ValidatableRequest interface {
	Validate() error
}

ValidatableRequest a request should be validated by call it's Validate function manually

type View

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

func NewView

func NewView(options ...ViewOption) *View

NewView create a new view

func (*View) AddTplFiles

func (view *View) AddTplFiles(files ...string)

AddTplFiles 添加通用模板文件

func (*View) ContainsString

func (view *View) ContainsString(arr []string, s string) bool

ContainsString 检查字符串列表是否包含某个值

func (*View) Render

func (view *View) Render(w http.ResponseWriter, f string, v interface{}) error

Render 渲染文件

func (*View) RenderDirect

func (view *View) RenderDirect(w http.ResponseWriter, name string, files []string, v interface{}) error

RenderDirect 直接渲染指定的文件

func (*View) RenderPage

func (view *View) RenderPage(w http.ResponseWriter, p *Page) error

RenderPage 根据Page信息渲染页面

func (*View) ResetTplFiles

func (view *View) ResetTplFiles()

ResetTplFiles 情况模板文件列表

func (*View) SetFuncMap

func (view *View) SetFuncMap(m template.FuncMap)

SetFuncMap 设置自定义方法列表

func (*View) SetTplDir

func (view *View) SetTplDir(d string)

SetTplDir 设置模板目录

func (*View) SetTplExtension

func (view *View) SetTplExtension(ext string)

SetTplExtension 设置模板文件扩展名

func (*View) Show

func (view *View) Show(w http.ResponseWriter, p *Page)

Show 根据Page信息渲染页面

func (*View) ShowDirect

func (view *View) ShowDirect(w http.ResponseWriter, p *Page) error

ShowDirect 根据Page信息渲染页面

type ViewOption

type ViewOption func(*View)

ViewOption option for view

func WithTplDir

func WithTplDir(d string) ViewOption

func WithTplExtension

func WithTplExtension(ext string) ViewOption

func WithTplFiles

func WithTplFiles(f ...string) ViewOption

Directories

Path Synopsis
example
api_example Module

Jump to

Keyboard shortcuts

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