laya

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: Apache-2.0 Imports: 24 Imported by: 16

README

laya

基本框架, 支持httpServer, rpcServer, 以及后端服务

快速开始

使用模板快速构建项目

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWebServerMiddlewares = []WebHandlerFunc{
	LogParams,
	SetTrace,
	ginHandler2WebHandler(gin.Recovery()),
	recovery,
}

DefaultWebServerMiddlewares 默认的Http Server中间件 其实应该保证TowerLogware 不panic,但是无法保证,多一个recovery来保证业务日志崩溃后依旧有访问日志

Functions

func CheckNoLogParams

func CheckNoLogParams(origin string) bool

CheckNoLogParams 判断是否需要打印入参出参日志, 不需要打印返回true

func LogParams

func LogParams(ctx *WebContext)

LogParams 记录框架出入参

func SetTrace

func SetTrace(ctx *WebContext)

SetTrace 注入链路追踪 优先级 request_id > trace_id

Types

type App

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

func DefaultApp

func DefaultApp() *App

DefaultApp 提供基础的日志服务, ginLog, webServer

func NewApp

func NewApp() *App

func (*App) PbRPCServer

func (app *App) PbRPCServer() *PbRPCServer

PbRPCServer 获取PbRPCServer的指针

func (*App) RunPbRPCServer

func (app *App) RunPbRPCServer()

RunPbRPCServer 运行PbRPC服务

func (*App) RunWebServer

func (app *App) RunWebServer()

RunWebServer 运行Web服务

func (*App) SetNoLogParams

func (app *App) SetNoLogParams(path ...string)

SetNoLogParams 设置不需要打印的路由

func (*App) SetNoLogParamsPrefix

func (app *App) SetNoLogParamsPrefix(path ...string)

SetNoLogParamsPrefix 设置不需要打印入参和出参的路由前缀

func (*App) SetNoLogParamsSuffix

func (app *App) SetNoLogParamsSuffix(path ...string)

SetNoLogParamsSuffix 设置不需要打印的入参和出参的路由后缀

func (*App) Use

func (app *App) Use(fc ...func())

Use 提供一个加载函数

func (*App) WebServer

func (app *App) WebServer() *WebServer

WebServer 获取WebServer的指针

type AppConfig

type AppConfig struct {
	// HttpListen  Web web 服务监听的地址
	HTTPListen string
	// PbPRCListen PbRPC服务监听的地址
	PbRPCListen string
}

type AppOption

type AppOption func(app *App)

type Context

type Context interface {
	context.Context
	DataContext
	LoggerContext
}

Context is the carrier of request and response

type DataContext

type DataContext interface {
	Set(key string, value interface{})
	Get(key string) (value interface{}, exists bool)
	MustGet(key string) interface{}
	GetString(key string) string
	GetBool(key string) bool
	GetInt(key string) int
	GetInt64(key string) int64
	GetFloat64(key string) float64
	GetTime(key string) time.Time
	GetDuration(key string) time.Duration
	GetStringSlice(key string) []string
	GetStringMap(key string) map[string]interface{}
	GetStringMapString(key string) map[string]string
	GetStringMapStringSlice(key string) map[string][]string
}

DataContext interface the extend of context.Context.Value(key string) (value interface{})

type HandlerFunc

type HandlerFunc func(ctx Context)

HandlerFunc 请求的处理者

type LogContext

type LogContext struct {
	RspHTTPStatusCode int
	ErrMsg            string
	// contains filtered or unexported fields
}

LogContext logger

func NewLogContext

func NewLogContext(req *http.Request, traceId string) *LogContext

NewLogContext new obj

func (*LogContext) Alarm

func (ctx *LogContext) Alarm(msg interface{})

Alarm 通知

func (*LogContext) ErrorF

func (ctx *LogContext) ErrorF(template string, args ...interface{})

func (*LogContext) Field

func (ctx *LogContext) Field(key string, value interface{}) glogs.Field

func (*LogContext) GetClientIP

func (ctx *LogContext) GetClientIP() string

GetClientIP 得到clientIP

func (*LogContext) GetTraceId

func (ctx *LogContext) GetTraceId() string

GetTraceId 得到TraceID

func (*LogContext) InfoF

func (ctx *LogContext) InfoF(template string, args ...interface{})

func (*LogContext) StartSpan

func (ctx *LogContext) StartSpan() glogs.Span

StartSpan StopSpan StartSpanP StartSpanR 开启,关闭,通过上级span开启span, 通过request开启span

func (*LogContext) StartSpanP

func (ctx *LogContext) StartSpanP(span glogs.Span, name string) glogs.Span

func (*LogContext) StartSpanR

func (ctx *LogContext) StartSpanR(name string) glogs.Span

func (*LogContext) StopSpan

func (ctx *LogContext) StopSpan(span glogs.Span)

func (*LogContext) WarnF

func (ctx *LogContext) WarnF(template string, args ...interface{})

type LoggerContext

type LoggerContext interface {
	InfoF(template string, args ...interface{})
	WarnF(template string, args ...interface{})
	ErrorF(template string, args ...interface{})
	Field(key string, value interface{}) glogs.Field

	// Alarm 告警
	Alarm(msg interface{})

	// StartSpan StopSpan StartSpanP StartSpanR 开启,关闭,通过上级span开启span, 通过request开启span
	StartSpan() glogs.Span
	StopSpan(span glogs.Span)
	StartSpanP(span glogs.Span, name string) glogs.Span
	StartSpanR(name string) glogs.Span
}

LoggerContext 包含链路, 日志, 告警

type MemoryContext

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

MemoryContext store data tor memory

func NewMemoryContext

func NewMemoryContext() *MemoryContext

NewMemoryContext new one

func (*MemoryContext) Get

func (c *MemoryContext) Get(key string) (value interface{}, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)

func (*MemoryContext) GetBool

func (c *MemoryContext) GetBool(key string) (b bool)

GetBool returns the value associated with the key as a boolean.

func (*MemoryContext) GetDuration

func (c *MemoryContext) GetDuration(key string) (d time.Duration)

GetDuration returns the value associated with the key as a duration.

func (*MemoryContext) GetFloat64

func (c *MemoryContext) GetFloat64(key string) (f64 float64)

GetFloat64 returns the value associated with the key as a float64.

func (*MemoryContext) GetInt

func (c *MemoryContext) GetInt(key string) (i int)

GetInt returns the value associated with the key as an integer.

func (*MemoryContext) GetInt64

func (c *MemoryContext) GetInt64(key string) (i64 int64)

GetInt64 returns the value associated with the key as an integer.

func (*MemoryContext) GetString

func (c *MemoryContext) GetString(key string) (s string)

GetString returns the value associated with the key as a string.

func (*MemoryContext) GetStringMap

func (c *MemoryContext) GetStringMap(key string) (sm map[string]interface{})

GetStringMap returns the value associated with the key as a map of interfaces.

func (*MemoryContext) GetStringMapString

func (c *MemoryContext) GetStringMapString(key string) (sms map[string]string)

GetStringMapString returns the value associated with the key as a map of strings.

func (*MemoryContext) GetStringMapStringSlice

func (c *MemoryContext) GetStringMapStringSlice(key string) (smss map[string][]string)

GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.

func (*MemoryContext) GetStringSlice

func (c *MemoryContext) GetStringSlice(key string) (ss []string)

GetStringSlice returns the value associated with the key as a slice of strings.

func (*MemoryContext) GetTime

func (c *MemoryContext) GetTime(key string) (t time.Time)

GetTime returns the value associated with the key as time.

func (*MemoryContext) MustGet

func (c *MemoryContext) MustGet(key string) interface{}

MustGet returns the value for the given key if it exists, otherwise it panics.

func (*MemoryContext) Set

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

Set is used to store a new key/value pair exclusively for this context. It also lazy initializes c.data if it was not used previously.

type PbRPCContext

type PbRPCContext struct {
	*LogContext
	*MemoryContext
	// contains filtered or unexported fields
}

PbRPCContext pbrpc context

func NewPbRPCContext

func NewPbRPCContext() *PbRPCContext

NewPbRPCContext new

func (*PbRPCContext) Deadline

func (c *PbRPCContext) Deadline() (deadline time.Time, ok bool)

Deadline returns the time when work done on behalf of this context should be canceled. Deadline returns ok==false when no deadline is set. Successive calls to Deadline return the same results.

func (*PbRPCContext) Done

func (c *PbRPCContext) Done() <-chan struct{}

Done returns a channel that's closed when work done on behalf of this context should be canceled. Done may return nil if this context can never be canceled. Successive calls to Done return the same value.

func (*PbRPCContext) Err

func (c *PbRPCContext) Err() error

Err returns a non-nil error value after Done is closed, successive calls to Err return the same error. If Done is not yet closed, Err returns nil. If Done is closed, Err returns a non-nil error explaining why: Canceled if the context was canceled or DeadlineExceeded if the context's deadline passed.

func (*PbRPCContext) Next

func (c *PbRPCContext) Next()

Next Goto next handler

func (*PbRPCContext) ReadPackage

func (c *PbRPCContext) ReadPackage() (*grpcx.Package, error)

ReadPackage return a package

func (*PbRPCContext) Value

func (c *PbRPCContext) Value(key interface{}) interface{}

Value returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Value with the same key returns the same result.

func (*PbRPCContext) WritePackage

func (c *PbRPCContext) WritePackage(pkg *grpcx.Package) (int, error)

WritePackage write a package

type PbRPCHandlerFunc

type PbRPCHandlerFunc func(*PbRPCContext)

PbRPCHandlerFunc handler for PbRPC

type PbRPCServer

type PbRPCServer struct {
	Server
	// contains filtered or unexported fields
}

PbRPCServer struct

func NewPbRPCServer

func NewPbRPCServer() *PbRPCServer

NewPbRPCServer create new PbRPCServer with default configuration

func (*PbRPCServer) AddHandler

func (pbs *PbRPCServer) AddHandler(handlers ...PbRPCHandlerFunc)

AddHandler 添加处理函数

type RouterRegister

type RouterRegister func(*WebServer)

RouterRegister 路由注册者

type Server

type Server struct {

	// Exported fields
	Logger        *zap.Logger
	Mode          string
	MaxWorker     uint32
	PrepareWorker uint32
	ReadTimeout   time.Duration
	WriteTimeout  time.Duration

	// Exported Method needs to apply
	RequestHandler func(con net.Conn)
	// contains filtered or unexported fields
}

Server 提供较为通用的服务器程序

func (*Server) CountAvailableWorkers

func (gs *Server) CountAvailableWorkers() uint32

func (*Server) CountBusyWorkers

func (gs *Server) CountBusyWorkers() uint32

func (*Server) Run

func (gs *Server) Run(listen ...string) (err error)

func (*Server) RunGrace

func (gs *Server) RunGrace(addr string, timeouts ...time.Duration) error

func (*Server) RunUnix

func (gs *Server) RunUnix(path ...string) (err error)

func (*Server) SetAcceptErrorHandler

func (gs *Server) SetAcceptErrorHandler(h func(error))

func (*Server) SetLogger

func (gs *Server) SetLogger(logger *zap.Logger)

func (*Server) SetReadTimeout

func (gs *Server) SetReadTimeout(ms int64)

func (*Server) SetRejectHandler

func (gs *Server) SetRejectHandler(h func(net.Conn, error))

func (*Server) SetWriteTimeout

func (gs *Server) SetWriteTimeout(ms int64)

type WebContext

type WebContext struct {
	*gin.Context
	*LogContext
}

WebContext http 的context WebContext 继承了 gin.Context, 并且扩展了日志功能

func NewWebContext

func NewWebContext(ginContext *gin.Context) *WebContext

NewWebContext 创建 http context

type WebHandlerFunc

type WebHandlerFunc func(*WebContext)

WebHandlerFunc http请求的处理者

type WebRoute

type WebRoute struct {
	RouterGroup *gin.RouterGroup
	// contains filtered or unexported fields
}

WebRoute struct 它实现了gin.IRoutes, gin.IRoute

func (*WebRoute) Any

func (wrc *WebRoute) Any(pattern string, handlers ...WebHandlerFunc) WebRouter

Any 注册所有的方法

func (*WebRoute) DELETE

func (wrc *WebRoute) DELETE(pattern string, handlers ...WebHandlerFunc) WebRouter

DELETE 注册DELETE方法

func (*WebRoute) GET

func (wrc *WebRoute) GET(pattern string, handlers ...WebHandlerFunc) WebRouter

GET 注册GET方法

func (*WebRoute) Group

func (wrc *WebRoute) Group(pattern string, handlers ...WebHandlerFunc) WebRouter

Group creates a new web router group

func (*WebRoute) HEAD

func (wrc *WebRoute) HEAD(pattern string, handlers ...WebHandlerFunc) WebRouter

HEAD 注册HEAD方法

func (*WebRoute) OPTIONS

func (wrc *WebRoute) OPTIONS(pattern string, handlers ...WebHandlerFunc) WebRouter

OPTIONS 注册OPTIONS方法

func (*WebRoute) PATCH

func (wrc *WebRoute) PATCH(pattern string, handlers ...WebHandlerFunc) WebRouter

PATCH 注册PATCH方法

func (*WebRoute) POST

func (wrc *WebRoute) POST(pattern string, handlers ...WebHandlerFunc) WebRouter

POST 注册POST方法

func (*WebRoute) PUT

func (wrc *WebRoute) PUT(pattern string, handlers ...WebHandlerFunc) WebRouter

PUT 注册PUT方法

func (*WebRoute) Static

func (wrc *WebRoute) Static(relativePath, root string) WebRouter

Static 静态文件

func (*WebRoute) StaticFS

func (wrc *WebRoute) StaticFS(relativePath string, fs http.FileSystem) WebRouter

StaticFS 静态文件

func (*WebRoute) StaticFile

func (wrc *WebRoute) StaticFile(relativePath, filepath string) WebRouter

StaticFile 静态文件

func (*WebRoute) Use

func (wrc *WebRoute) Use(middleware ...WebHandlerFunc) WebRouter

Use attachs a global middleware to the router. ie. the middleware attached though Use() will be included in the handlers chain for every single request. Even 404, 405, static files... For example, this is the right place for a logger or error management middleware.

type WebRouter

type WebRouter interface {
	// Group gin.IRoute.Group
	Group(string, ...WebHandlerFunc) WebRouter

	// Use gin.IRoutes.Use
	Use(...WebHandlerFunc) WebRouter

	Any(pattern string, handlers ...WebHandlerFunc) WebRouter
	GET(pattern string, handlers ...WebHandlerFunc) WebRouter
	POST(pattern string, handlers ...WebHandlerFunc) WebRouter
	DELETE(pattern string, handlers ...WebHandlerFunc) WebRouter
	PATCH(pattern string, handlers ...WebHandlerFunc) WebRouter
	PUT(pattern string, handlers ...WebHandlerFunc) WebRouter
	OPTIONS(pattern string, handlers ...WebHandlerFunc) WebRouter
	HEAD(pattern string, handlers ...WebHandlerFunc) WebRouter

	StaticFile(relativePath, filepath string) WebRouter
	Static(relativePath, root string) WebRouter
	StaticFS(relativePath string, fs http.FileSystem) WebRouter
}

WebRouter interface WebRequest Router 它合并了 gin.IRoute 和 gin.IRoutes

type WebServer

type WebServer struct {
	// 重写所有的路由相关的方法
	*WebRoute
	// 继承引擎本身的其他方法
	*gin.Engine
}

WebServer 基于http协议的服务 这里的实现是基于gin框架, 封装了gin的所有的方法 gin 的核心是高效路由, 但是gin.Engine和gin.IRouter(s)的高耦合让我们无法复用, gin的作者认为它的路由就是引擎吧

func NewWebServer

func NewWebServer(mode string) *WebServer

NewWebServer 创建WebServer

func (*WebServer) Delims

func (webServer *WebServer) Delims(left, right string) *WebServer

Delims 设置模板的分解符 重写gin方法

func (*WebServer) HandleContext

func (webServer *WebServer) HandleContext(wc *WebContext)

HandleContext re-enter a context that has been rewritten. This can be done by setting c.Request.URL.Path to your new target. Disclaimer: You can loop yourself to death with this, use wisely.

func (*WebServer) NoMethod

func (webServer *WebServer) NoMethod(handlers ...WebHandlerFunc)

NoMethod sets the handlers called when... TODO. 重写gin方法

func (*WebServer) NoRoute

func (webServer *WebServer) NoRoute(handlers ...WebHandlerFunc)

NoRoute adds handlers for NoRoute. It return a 404 code by default. 重写gin方法

func (*WebServer) RegisterRouter

func (webServer *WebServer) RegisterRouter(rr RouterRegister)

RegisterRouter 注册路由

func (*WebServer) RunGrace

func (webServer *WebServer) RunGrace(addr string, timeouts ...time.Duration) error

RunGrace 实现Server接口

func (*WebServer) SecureJsonPrefix

func (webServer *WebServer) SecureJsonPrefix(prefix string) *WebServer

SecureJsonPrefix sets the secureJsonPrefix used in Context.SecureJSON. 重写gin方法

func (*WebServer) Use

func (webServer *WebServer) Use(middleware ...WebHandlerFunc) WebRouter

Use adds middleware to the group, see example code in github.

Directories

Path Synopsis
cmdx module
Package gcal 提供了一个支持多种交互协议和打包格式的扩展包。
Package gcal 提供了一个支持多种交互协议和打包格式的扩展包。
context
Package context 提供每次 RAL 请求的上下文对象,主要用来输出日志。
Package context 提供每次 RAL 请求的上下文对象,主要用来输出日志。
converter
Package converter 提供了一系列对请求的数据序列化和响应的数据格式化方法
Package converter 提供了一系列对请求的数据序列化和响应的数据格式化方法
protocol
Package protocol 提供了 HTTP、HTTPS、NSHead、ProtoBuffer 协议支持
Package protocol 提供了 HTTP、HTTPS、NSHead、ProtoBuffer 协议支持
service
Package service 提供了一些列资源管理的方法
Package service 提供了一些列资源管理的方法
Package glogs is a global internal glogs glogs: this is extend package, use https://github.com/uber-go/zap
Package glogs is a global internal glogs glogs: this is extend package, use https://github.com/uber-go/zap
logger
Package logger is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
Package logger is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
Package gversion storage versions info for laya
Package gversion storage versions info for laya
laya module

Jump to

Keyboard shortcuts

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