x

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: Apache-2.0 Imports: 43 Imported by: 1

Documentation

Index

Constants

View Source
const (
	RAND_KIND_NUM   = 0 // 纯数字
	RAND_KIND_LOWER = 1 // 小写字母
	RAND_KIND_UPPER = 2 // 大写字母
	RAND_KIND_ALL   = 3 // 数字、大小写字母
)
View Source
const ACTION_SUFFIX = "Action"
View Source
const CONTROLLER_SUFFIX = "Controller"

controller中以此后缀结尾的方法会参与路由

Variables

View Source
var (
	//多语言时指定默认语言
	DEFAULT_LANG = "CN"
	//成功
	ERR_SUC = &Error{0, "OK"}

	//系统错误码
	ERR_SYSTEM         = &Error{10, MAPS{"CN": "系统错误", "EN": "system error"}}
	ERR_METHOD_INVALID = &Error{11, MAPS{"CN": "请求不合法: %+v", "EN": "invalid request: %+v"}}
	ERR_FREQ           = &Error{12, MAPS{"CN": "接口访问过于频繁: %+v", "EN": "request is too frequently: %+v"}}
	ERR_RPCAUTH        = &Error{13, MAPS{"CN": "rpc认证失败:%+v", "EN": "rpc request unauthorized"}}
	ERR_PARAMS         = &Error{14, MAPS{"CN": "参数错误: %+v", "EN": "invalid param: %+v"}}
	ERR_OTHER          = &Error{15, "%+v"}
)
View Source
var (
	TemplateRoot   = "../templates"
	TemplateSuffix = ".htm"
	TemplateFuncs  = template.FuncMap{
		"include": func(string) string { return "" },
		"replace": strings.ReplaceAll,
		"join":    Join,
		"jsonEncode": func(v interface{}) template.JS {
			js := JsonEncode(v)
			return template.JS(js)
		},
		"jsonDecode": func(v interface{}) interface{} {
			return JsonDecode(v)
		},
		"jscode": func(v interface{}) template.JS {
			return template.JS(fmt.Sprint(v))
		},
		"htmlcode": func(v interface{}) template.HTML {
			return template.HTML(fmt.Sprint(v))
		},
		"formatTime": FormatTime,
		"dateTime":   DateTime,
		"asint":      AsInt,
		"asfloat":    AsFloat,
		"default": func(def, v interface{}) interface{} {
			if v == nil {
				return def
			}

			return v
		},
	}
)
View Source
var (
	Conf_env_mode                      string
	Conf_template_enabled              bool
	Conf_max_post_size                 int64
	Conf_access_log_enabled            bool
	Conf_access_log_success_level_name string
	Conf_access_log_error_level_name   string
	Conf_access_log_omit_params        []string
	Conf_rpc_auth                      map[string]string
	Conf_default_controller            string
	Conf_default_action                string

	UrlRoutes      map[string]map[string]any
	UrlParamRoutes map[int]map[int]map[string]map[string]any
	UrlPrefix      []map[string]string
)

配置文件变量缓存(框架中使用),初始化时赋值

View Source
var AppRoot string

应用程序根路径

View Source
var DB = NewDBProxy()

全局共用db代理

View Source
var Debug bool

全局 Debug 开关

View Source
var LocalCache *cache.Cache

本地缓存

View Source
var Logger *log.Logger

全局共用日志

View Source
var Redis = NewRedisProxy()

全局共用redis代理

View Source
var TIME_ZONE = "Local" // Asia/Shanghai, UTC

在项目代码中指定时区

Functions

func AddApi

func AddApi(c any, groups ...string)

添加http 方法对应的controller实例, 支持分组; 默认url路径: controller/action, 分组时路径: group/controller/action

func AddCli

func AddCli(c any, groups ...string)

添加cli 方法对应的controller实例, 支持分组

func AddGrpcServerOption

func AddGrpcServerOption(o ...grpc.ServerOption)

添加 grpc ServerOption

func AddRouteApiFunc

func AddRouteApiFunc(controller_name, action_name string, f RouteApiFunc)

添加 route 调用方法, 在脚手架生成代码时使用

func AddRouteRpcFunc

func AddRouteRpcFunc(controller_name, action_name string, f RouteRpcFunc)

添加 route 调用方法, 在脚手架生成代码时使用

func AddRpc

func AddRpc(c any, groups ...string)

添加rpc 方法对应的controller实例, 支持分组

func ArrayColumn

func ArrayColumn[T comparable](m []map[string]T, column string, uniqs ...bool) []T

func ArrayColumnMap

func ArrayColumnMap[T comparable](m []map[string]T, column string, index string) map[T]T

func ArrayMerge

func ArrayMerge[T comparable](arr []T, n ...[]T) []T

array string 新增,并去重

func ArrayRem

func ArrayRem[T comparable](arr []T, n T) []T

array string 删除

func ArrayUnique

func ArrayUnique[T comparable](arr []T) []T

array 去重, trim:去除首尾空格,删除空元素

func AsBool

func AsBool(b interface{}, def ...bool) bool

强制转换为bool

func AsBytes

func AsBytes(arg interface{}, def ...[]byte) []byte

强制转换为bytes

func AsFloat

func AsFloat(num interface{}, def ...float64) float64

强制转换为float64

func AsFloat32

func AsFloat32(num interface{}, def ...float32) float32

强制转换为float32

func AsFloat64

func AsFloat64(num interface{}, def ...float64) float64

强制转换为float64

func AsInt

func AsInt(num interface{}, def ...int) int

强制转换为int

func AsInt32

func AsInt32(num interface{}, def ...int32) int32

强制转换为int32

func AsInt32Slice

func AsInt32Slice(v interface{}) []int32

interface{}的切片转换为int32切片

func AsInt64

func AsInt64(num interface{}, def ...int64) int64

强制转换为int64

func AsInt64Slice

func AsInt64Slice(v interface{}) []int64

interface{}的切片转换为int64切片

func AsIntSlice

func AsIntSlice(v interface{}) []int

interface{}的切片转换为int切片

func AsSlice

func AsSlice[T any](arr []T) []any

将[]T 转换为[]any

func AsString

func AsString(v any, def ...string) string

强制转换为string

func AsStringSlice

func AsStringSlice(v any) (strs []string)

interface{}的切片转换为string切片

func AsUint

func AsUint(num interface{}, def ...uint) uint

强制转换为uint

func AsUint32

func AsUint32(num interface{}, def ...uint32) uint32

强制转换为uint32

func AsUint64

func AsUint64(num interface{}, def ...uint64) uint64

强制转换为uint64

func Concat

func Concat(str ...string) string

拼接字符串

func Cost

func Cost(start_time time.Time) int

从start_time开始的消耗时间, 单位毫秒

func Crc32

func Crc32(str string) int

func Date

func Date(times ...any) string

返回2013-01-20 格式的日期, 可以指定时间戳,默认当前时间

func DateHour

func DateHour(times ...any) string

返回`2013-01-20 10` 小时整点格式的时间, 可以指定时间戳,默认当前时间

func DateMin

func DateMin(times ...any) string

返回`2013-01-20 10:20` 分钟整点格式的时间, 可以指定时间戳,默认当前时间

func DateTime

func DateTime(times ...any) string

返回2013-01-20 10:20:01 格式的时间, 可以指定时间戳,默认当前时间

func Expr

func Expr(param string) string

拼装参数时,作为可执行字符,而不是字符串值

func FileGetContents

func FileGetContents(path string, options ...interface{}) (string, error)

func FormatTime

func FormatTime(layout string, times ...any) string

func GetCookie

func GetCookie(r *http.Request, key string) string

func GetIp

func GetIp(r *http.Request) string

func GetLocalIp

func GetLocalIp() string

获取本机ip

func GetMapNode

func GetMapNode(m map[string]interface{}, keys ...string) (interface{}, bool)

获取map树的某个节点 m[k1][k2]...[kn]

func GetNode

func GetNode(i any, keys ...string) any

从interface{}树中获得一个节点, 失败返回nil

func GetSlice

func GetSlice(i any, keys ...string) []any

从interface{}树中获得一个Slice类型, 失败返回nil

func GreenString

func GreenString(s string) string

func Hash

func Hash(s string) uint64

为字符串生成哈希值

func HashMap

func HashMap(m map[string]any) uint64

为map生成哈希值

func HashSlice

func HashSlice(s []any) uint64

为slice生成哈希值

func InArray

func InArray[T comparable](search T, s []T) bool

判断array/slice中是否存在某值

func InMap

func InMap[T, K comparable](search T, m map[K]T) bool

判断map中是否存在某值

func Interceptor

func Interceptor(guard bool, errmsg interface{}, fmts ...interface{})

捕获异常时,可同时返回data(通过fmts参数最后一个类型为map的值)

func Intn

func Intn(n int) int

返回[0,n)范围内的随机整数, 替换math.rand.Intn

func Ip2long

func Ip2long(ipstr string) (ip int)

x.x.x.x格式的IP转换为数字

func IsDir

func IsDir(path string) (bool, error)

func IsFile

func IsFile(path string) (bool, error)

func IsTerm

func IsTerm() bool

func Join

func Join(v interface{}, seps ...string) string

[]int, []string, []interface, 连接成字符串

func JsonDecode

func JsonDecode(data any) any

func JsonEncode

func JsonEncode(data any) string

func JsonEncodeBytes

func JsonEncodeBytes(data any) []byte

func Long2ip

func Long2ip(ip int) string

数字格式的IP转换为x.x.x.x

func MD5

func MD5(str string) string

func MD5File

func MD5File(file string) (string, error)

func MapMerge

func MapMerge[T, K comparable](m map[T]K, ms ...map[T]K) map[T]K

合并MAP(一级)

func MkTime

func MkTime(t ...int) int

生成时间戳 参数:小时,分,秒,月,日,年

func NewLocalCache

func NewLocalCache(cache_sizes ...int) *cache.Cache

func NewTCPKeepAliveListener

func NewTCPKeepAliveListener(ln *net.TCPListener, d time.Duration) net.Listener

func Now

func Now() int

unix时间戳

func ParseDate

func ParseDate(a any) time.Time

func ParseDateTime

func ParseDateTime(a any) time.Time

func ParseDateTime64

func ParseDateTime64(a any) time.Time

func ParseNaN

func ParseNaN(num float64) float64

NaN转换0

func ParseTime

func ParseTime(a any) time.Time

func ParseTimestamp

func ParseTimestamp(a any) time.Time

func Printf

func Printf(f string, s ...any)

func Println

func Println(s ...any)

便于调式时直接使用

func Rand

func Rand(size int, kind int) []byte

随机字符串

func RandStr

func RandStr(size int, kind ...int) string

func RedString

func RedString(s string) string

func RegisterSqlDriver

func RegisterSqlDriver(name string, dsnfunc SqlDsn, f ...NewDBFunc)

func RunMonitor

func RunMonitor(port string)

func SetCookie

func SetCookie(rw http.ResponseWriter, key, val string, lifetime int, options ...interface{})

lifetime<0时删除cookie options: domain,secure,httponly,path

func SetHttpMaxHeaderBytes

func SetHttpMaxHeaderBytes(m int)

设置MaxHeaderBytes, 单位M

func SetTcpHandler

func SetTcpHandler(h func(net.Conn))

设置回调处理

func SetWebsocketHandler

func SetWebsocketHandler(p string, h func(net.Conn))

设置路由及回调处理

func SetWebsocketMaxHeaderBytes

func SetWebsocketMaxHeaderBytes(m int)

设置MaxHeaderBytes, 单位M

func Sha1

func Sha1(str string) string

func StaticEmbed

func StaticEmbed(filesys embed.FS, fs_path string)

设置使用embed.FS

func StrToTime

func StrToTime(datetime string) int

日期时间字符串转化为时间戳

func TemplateAddFuncs

func TemplateAddFuncs(vals ...interface{})

添加模板函数(全局)

func TemplateEmbed

func TemplateEmbed(filesys embed.FS, fs_path string)

设置使用embed.FS

func ToFloat

func ToFloat(num string) float64

string转换为float64

func ToFloat32

func ToFloat32(num string) float32

func ToFloat64

func ToFloat64(num string) float64

func ToInt

func ToInt(num string) int

string转换为int

func ToInt32

func ToInt32(v string) int32

string转换为int32

func ToInt32Slice

func ToInt32Slice(nums []string) []int32

string的切片转换为int32切片

func ToInt64

func ToInt64(num string) int64

string转换为int64

func ToInt64Slice

func ToInt64Slice(nums []string) []int64

string的切片转换为int64切片

func ToIntSlice

func ToIntSlice(nums []string) []int

string的切片转换为int切片

func ToString

func ToString(num int) string

int转换为string

func ToStringSlice

func ToStringSlice(nums []int) []string

int的切片转换为string切片

func Uint32

func Uint32() uint32

返回随机uint32 (线程安全)

func VersionCompare

func VersionCompare(ver, oldver string) bool

判断ver是否大于oldver

func YellowString

func YellowString(s string) string

Types

type CliServer

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

func NewCliServer

func NewCliServer(uri, params string) *CliServer

func (*CliServer) AddController

func (this *CliServer) AddController(c any, group ...string)

func (*CliServer) Run

func (this *CliServer) Run()

type ConfVal

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

func (*ConfVal) Bool

func (cv *ConfVal) Bool(def ...bool) bool

func (*ConfVal) Int

func (cv *ConfVal) Int(def ...int) int

func (*ConfVal) Int64

func (cv *ConfVal) Int64(def ...int64) int64

func (*ConfVal) IntMap

func (cv *ConfVal) IntMap(def ...MAPI) MAPI

func (*ConfVal) IntSlice

func (cv *ConfVal) IntSlice(def ...[]int) []int

func (*ConfVal) Map

func (cv *ConfVal) Map(def ...MAP) MAP

func (*ConfVal) MapSlice

func (cv *ConfVal) MapSlice(def ...[]MAP) []MAP

func (*ConfVal) MapsSlice

func (cv *ConfVal) MapsSlice(def ...[]MAPS) []MAPS

func (*ConfVal) Slice

func (cv *ConfVal) Slice(def ...[]string) []string

func (*ConfVal) String

func (cv *ConfVal) String(def ...string) string

func (*ConfVal) StringMap

func (cv *ConfVal) StringMap(def ...MAPS) MAPS

type Config

type Config struct {
	ConfigFile string
	// contains filtered or unexported fields
}
var Conf *Config

全局共用配置

func NewConfig

func NewConfig(conf_files ...string) (*Config, error)

func (*Config) Get

func (this *Config) Get(keys ...string) *ConfVal

func (*Config) GetBool

func (this *Config) GetBool(keys ...string) bool

func (*Config) GetDefBool

func (this *Config) GetDefBool(def bool, keys ...string) bool

func (*Config) GetDefInt

func (this *Config) GetDefInt(def int, keys ...string) int

func (*Config) GetDefInt64

func (this *Config) GetDefInt64(def int64, keys ...string) int64

func (*Config) GetDefIntMap

func (this *Config) GetDefIntMap(def MAPI, keys ...string) MAPI

func (*Config) GetDefIntSlice

func (this *Config) GetDefIntSlice(def []int, keys ...string) []int

func (*Config) GetDefMap

func (this *Config) GetDefMap(def MAP, keys ...string) MAP

func (*Config) GetDefMapSlice

func (this *Config) GetDefMapSlice(def []MAP, keys ...string) []MAP

func (*Config) GetDefMapsSlice

func (this *Config) GetDefMapsSlice(def []MAPS, keys ...string) []MAPS

func (*Config) GetDefSlice

func (this *Config) GetDefSlice(def []string, keys ...string) []string

func (*Config) GetDefString

func (this *Config) GetDefString(def string, keys ...string) string

func (*Config) GetDefStringMap

func (this *Config) GetDefStringMap(def MAPS, keys ...string) MAPS

func (*Config) GetInt

func (this *Config) GetInt(keys ...string) int

func (*Config) GetInt64

func (this *Config) GetInt64(keys ...string) int64

func (*Config) GetIntMap

func (this *Config) GetIntMap(keys ...string) MAPI

func (*Config) GetIntSlice

func (this *Config) GetIntSlice(keys ...string) []int

func (*Config) GetMap

func (this *Config) GetMap(keys ...string) MAP

func (*Config) GetMapSlice

func (this *Config) GetMapSlice(keys ...string) []MAP

func (*Config) GetMapsSlice

func (this *Config) GetMapsSlice(keys ...string) []MAPS

func (*Config) GetSlice

func (this *Config) GetSlice(keys ...string) []string

func (*Config) GetString

func (this *Config) GetString(keys ...string) string

func (*Config) GetStringMap

func (this *Config) GetStringMap(keys ...string) MAPS

type DBProxy

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

func NewDBProxy

func NewDBProxy() *DBProxy

func (*DBProxy) Get

func (this *DBProxy) Get(conf MAP) *db.SqlClient

type Error

type Error struct {
	Code int
	Msg  interface{}
}

func (*Error) Error

func (this *Error) Error() string

func (*Error) GetCode

func (this *Error) GetCode() int

func (*Error) GetMessage

func (this *Error) GetMessage(langs ...string) string

type Errorf

type Errorf struct {
	Code int
	Msg  interface{}
	// contains filtered or unexported fields
}

格式化输出错误信息 用于 Interceptor 拦截抛错 国际化产品,多语言时,Msg 可以设置为MAPS(map[string]string),如:{"CN":"系统错误", "EN":"system error"}

func (*Errorf) Error

func (this *Errorf) Error() string

func (*Errorf) GetCode

func (this *Errorf) GetCode() int

func (*Errorf) GetData

func (this *Errorf) GetData() MAP

func (*Errorf) GetMessage

func (this *Errorf) GetMessage(langs ...string) string

type HttpClient

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

func NewHttpClient

func NewHttpClient(timeouts ...int) *HttpClient

func (*HttpClient) Delete

func (this *HttpClient) Delete(requrl string, headers ...http.Header) (*HttpResponse, error)

func (*HttpClient) Get

func (this *HttpClient) Get(requrl string, headers ...http.Header) (*HttpResponse, error)

func (*HttpClient) Patch

func (this *HttpClient) Patch(requrl string, post_data interface{}, headers ...http.Header) (*HttpResponse, error)

func (*HttpClient) Post

func (this *HttpClient) Post(requrl string, post_data interface{}, headers ...http.Header) (*HttpResponse, error)

func (*HttpClient) Put

func (this *HttpClient) Put(requrl string, post_data interface{}, headers ...http.Header) (*HttpResponse, error)

func (*HttpClient) Request

func (this *HttpClient) Request(method, requrl string, post_data interface{}, headers ...http.Header) (*HttpResponse, error)

post_data 支持map[string]interface{} 和 io.Reader 两种参数类型

type HttpResponse

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

func (*HttpResponse) GetCode

func (r *HttpResponse) GetCode() int

func (*HttpResponse) GetHeader

func (r *HttpResponse) GetHeader() http.Header

func (*HttpResponse) GetResponse

func (r *HttpResponse) GetResponse() string

type HttpServer

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

func NewHttpServer

func NewHttpServer(addr string, port, timeout int, useGraceful, enable_pprof, enable_static bool, static_path, static_root string) *HttpServer

func (*HttpServer) AddController

func (this *HttpServer) AddController(c interface{}, group ...string)

func (*HttpServer) Run

func (this *HttpServer) Run()

type IMAP

type IMAP = map[int]any

使用MAP替代map[int]any

type IMAPI

type IMAPI = map[int]int

使用IMAPI替代map[int]int

type IMAPS

type IMAPS = map[int]string

使用IMAPS替代map[int]string

type MAP

type MAP = map[string]any

简化业务代码 使用MAP替代map[string]any

func AsMap

func AsMap(a any) MAP

func AsMapSlice

func AsMapSlice(a any) []MAP

func GetMap

func GetMap(i interface{}, keys ...string) MAP

从interface{}树中获得一个MAP类型, 失败返回nil

type MAPA

type MAPA = map[any]any

type MAPI

type MAPI = map[string]int

使用MAPI替代map[string]int

func AsIntMap

func AsIntMap(a any) MAPI

type MAPS

type MAPS = map[string]string

使用MAPS替代map[string]string

func AsStringMap

func AsStringMap(a any) MAPS

func ParseRoute

func ParseRoute(uri, method string) (string, string, MAPS)

解析 uri 得到 controller action params

type Mapper

type Mapper interface {
	ToMap() map[string]any
}

type NewDBFunc

type NewDBFunc func() *db.SqlClient

type NewDBFunc func() db.DBClient

type RedisProxy

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

func NewRedisProxy

func NewRedisProxy() *RedisProxy

func (*RedisProxy) Get

func (this *RedisProxy) Get(conf_name string) (*redis.RedisClient, error)

type RouteApiFunc

type RouteApiFunc func(http.ResponseWriter, *http.Request)

执行路由方法的函数

type RouteRpcFunc

type RouteRpcFunc func(map[string]any, context.Context) map[string]any

执行路由方法的函数

type RpcServer

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

func NewRpcServer

func NewRpcServer(addr string, port, timeout int, useGraceful bool) *RpcServer

func (*RpcServer) AddController

func (this *RpcServer) AddController(c any, group ...string)

func (*RpcServer) Run

func (this *RpcServer) Run()

type SqlClient

type SqlClient = db.SqlClient

方便直接从x引用

type SqlDsn

type SqlDsn func(MAP) string

type TcpServer

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

func NewTcpServer

func NewTcpServer(addr string, port int, useGraceful bool) *TcpServer

func (*TcpServer) Run

func (this *TcpServer) Run()

func (*TcpServer) Serve

func (this *TcpServer) Serve(listener net.Listener) error

func (*TcpServer) SetTcpHandler

func (this *TcpServer) SetTcpHandler(h func(net.Conn))

type Template

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

func NewTemplate

func NewTemplate() *Template

func (*Template) AddFunc

func (this *Template) AddFunc(vals ...interface{})

添加模板函数

func (*Template) Assign

func (this *Template) Assign(vals ...interface{})

解析模板变量

func (*Template) Render

func (this *Template) Render(w http.ResponseWriter, uri, file string) error

type WebsocketServer

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

func NewWebsocketServer

func NewWebsocketServer(addr string, port, timeout int, useGraceful bool) *WebsocketServer

func (*WebsocketServer) Run

func (this *WebsocketServer) Run()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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