commons

package
v0.0.0-...-6ab1c3f Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProdEnv                = "prod"
	AppPort                = 10000
	ReportDuration         = 30
	SS5ProxyPort           = 19825
	SS5ProxyConnectTimeOut = 5
	HttpProxyPort          = 21520
	LogsPath               = "./logs"
	InfoLogLevel           = "info"
)

Variables

View Source
var (
	ErrorCodeSystem         = NewError(1000, "系统异常")
	ErrorCodeNotLogin       = NewError(1, "请先登录")
	ErrorCodeParse          = NewError(2, "解析错误")
	ErrorCodeNotFound       = NewError(3, "未找到")
	ErrorCodeRegisterFailed = NewError(4, "用户注册失败")
)
View Source
var (
	Conf    = &ServerConfig{} // 全局配置文件
	Log     = logrus.New()    // Log API
	ProxyIp = ""              // 代理 ip
)

全局实例对象

Functions

func AppendLine

func AppendLine(path string, content string) error

func BytesToInt

func BytesToInt(b []byte) int

func BytesToStruct

func BytesToStruct(data []byte, inter interface{})

func Contains

func Contains(search interface{}, target interface{}) bool

func ContainsIgnoreCase

func ContainsIgnoreCase(search string, target []string) bool

func FormValue

func FormValue(ctx iris.Context, name string) string

func FormValueBool

func FormValueBool(ctx iris.Context, name string) (bool, error)

func FormValueDefault

func FormValueDefault(ctx iris.Context, name, def string) string

func FormValueInt

func FormValueInt(ctx iris.Context, name string) (int, error)

func FormValueInt64

func FormValueInt64(ctx iris.Context, name string) (int64, error)

func FormValueInt64Array

func FormValueInt64Array(ctx iris.Context, name string) []int64

func FormValueInt64Default

func FormValueInt64Default(ctx iris.Context, name string, def int64) int64

func FormValueIntDefault

func FormValueIntDefault(ctx iris.Context, name string, def int) int

func FormValueRequired

func FormValueRequired(ctx iris.Context, name string) (string, error)

func FormValueStringArray

func FormValueStringArray(ctx iris.Context, name string) []string

func InitLog

func InitLog() error

func IsEmpty

func IsEmpty(a interface{}) bool

func MD5

func MD5(str string) string

func MD5Bytes

func MD5Bytes(data []byte) string

func MapToStruct

func MapToStruct(obj interface{}, data map[string]interface{}) error

func PathExists

func PathExists(path string) (bool, error)

func SqlNullString

func SqlNullString(value string) sql.NullString

func StructFields

func StructFields(s interface{}) []reflect.StructField

获取struct字段

func StructName

func StructName(s interface{}) string

获取struct name

func StructToMap

func StructToMap(obj interface{}, excludes ...string) map[string]interface{}

func StructTypeOf

func StructTypeOf(s interface{}) reflect.Type

func WriteString

func WriteString(path string, content string, append bool) error

Types

type CursorResult

type CursorResult struct {
	Results interface{} `json:"results"` // 数据
	Cursor  string      `json:"cursor"`  // 下一页
}

Cursor分页返回数据

type ErrorCode

type ErrorCode struct {
	Code      int
	Message   string
	Data      interface{}
	Success   bool
	ErrorData interface{}
}

func FromError

func FromError(err error) *ErrorCode

func NewError

func NewError(code int, text string) *ErrorCode

func NewErrorData

func NewErrorData(code int, text string, errorData interface{}) *ErrorCode

func (*ErrorCode) Error

func (e *ErrorCode) Error() string

type OrderByCol

type OrderByCol struct {
	Column string // 排序字段
	Asc    bool   // 是否正序
}

排序信息

type PageResult

type PageResult struct {
	Page    *Paging     `json:"page"`    // 分页信息
	Results interface{} `json:"results"` // 数据
}

分页返回数据

type Paging

type Paging struct {
	Page  int `json:"page"`  // 页码
	Limit int `json:"limit"` // 每页条数
	Total int `json:"total"` // 总数据条数
}

分页请求数据

func GetPaging

func GetPaging(ctx iris.Context) *Paging

func (*Paging) Offset

func (p *Paging) Offset() int

func (*Paging) TotalPage

func (p *Paging) TotalPage() int

type ParamPair

type ParamPair struct {
	Query string        // 查询
	Args  []interface{} // 参数
}

type QueryParams

type QueryParams struct {
	Ctx iris.Context
	SqlCnd
}

func NewQueryParams

func NewQueryParams(ctx iris.Context) *QueryParams

func (*QueryParams) Asc

func (q *QueryParams) Asc(column string) *QueryParams

func (*QueryParams) Desc

func (q *QueryParams) Desc(column string) *QueryParams

func (*QueryParams) EqByReq

func (q *QueryParams) EqByReq(column string) *QueryParams

func (*QueryParams) GtByReq

func (q *QueryParams) GtByReq(column string) *QueryParams

func (*QueryParams) GteByReq

func (q *QueryParams) GteByReq(column string) *QueryParams

func (*QueryParams) LikeByReq

func (q *QueryParams) LikeByReq(column string) *QueryParams

func (*QueryParams) Limit

func (q *QueryParams) Limit(limit int) *QueryParams

func (*QueryParams) LtByReq

func (q *QueryParams) LtByReq(column string) *QueryParams

func (*QueryParams) LteByReq

func (q *QueryParams) LteByReq(column string) *QueryParams

func (*QueryParams) NotEqByReq

func (q *QueryParams) NotEqByReq(column string) *QueryParams

func (*QueryParams) Page

func (q *QueryParams) Page(page, limit int) *QueryParams

func (*QueryParams) PageByReq

func (q *QueryParams) PageByReq() *QueryParams

type SLSHook

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

SLSHook 定义一个实现了logrus.Hook接口的SLSHook结构体

func (*SLSHook) Fire

func (hook *SLSHook) Fire(entry *logrus.Entry) error

func (*SLSHook) Levels

func (hook *SLSHook) Levels() []logrus.Level

type ServerConfig

type ServerConfig struct {
	App struct {
		Env             string   `yaml:"env"`             // 运行环境,dev 与 prod, 默认为生产环境(prod),如果为开发环境,不生成文件日志
		Port            uint64   `yaml:"port"`            // 应用本身暴露出的 http 服务的端口
		ChangeIpCommand string   `yaml:"changeIpCommand"` // agent 端用来更换 ip 的命令
		Platform        string   `yaml:"platform"`        //代理运行的云服务平台
		Tags            []string `yaml:"tags"`            // 标签
		Name            string   `yaml:"name" default:""`
	} `yaml:"app"`

	Report struct {
		Url      string `yaml:"url"`      // 上报的 url
		HostName string `yaml:"hostname"` // 上报信息中的 hostname, 如果没有指定,就获取本机的 hostname
		Ip       string `yaml:"ip"`       // 代理ip,如果不指定,那就请求 http://httpbin.org/ip 获取自己的外网 ip
		Duration int    `yaml:"duration"` // 上报代理信息的周期
	} `yaml:"report"`

	NeedReport bool

	Proxy struct {
		Socks5 struct {
			Enable         bool     `yaml:"enable"` // 是否开启 socks5 代理
			Port           int      `yaml:"port"`
			ConnectTimeout int      `yaml:"connectTimeout"` // 代理服务器与目标网站连接超时时间, 默认5秒
			UserAndPass    []string `yaml:"userAndPass"`
			UserAndPassMap map[string]string
		} `yaml:"socks5"`
		Http struct {
			Enable         bool     `yaml:"enable"`
			Port           int      `yaml:"port"`
			UserAndPass    []string `yaml:"userAndPass"`
			UserAndPassMap map[string]string
		} `yaml:"http"`
		ShadowSocks struct {
			Enable    bool     `yaml:"enable"`    //是否开启shadowSocks代理
			Port      int      `yaml:"port"`      //监听端口
			Cipher    string   `yaml:"cipher"`    //加密方式
			Password  []string `yaml:"password"`  //密码
			WhiteList []string `yaml:"whiteList"` //域名白名单
			BlackList []string `yaml:"blackList"` //域名黑名单
		} `yaml:"shadowSocks"`
	} `yaml:"proxy"`
	Log struct {
		Path  string `yaml:"path"`  // 日志存储的文件目录, 默认为当前目录下的 logs 目录下
		Level string `yaml:"level"` // 日志打印级别, 默认为 info
		SLS   struct {
			AccessKeyId     string `yaml:"accessKeyId"`
			AccessKeySecret string `yaml:"accessKeySecret"`
			Project         string `yaml:"project"`
			Logstore        string `yaml:"logstore"`
			Endpoint        string `yaml:"endpoint"`
			Topic           string `yaml:"topic"`
		}
	} `yaml:"log"`
	Nacos struct {
		Namespace string `yaml:"namespace"` //nacos的命名空间,public或者特定的命名空间
		IpAddr    string `yaml:"ipAddr"`    // nacos服务的ip或者域名
		Port      uint64 `yaml:"port"`      //nacos服务的端口
		RegIp     string `yaml:"regIp"`     //注册到nacos的ip, private / public 内网或者外网,默认外网
	} `yaml:"nacos"`
}

func (*ServerConfig) InitConfig

func (conf *ServerConfig) InitConfig(filename string) error

func (*ServerConfig) PrettyString

func (conf *ServerConfig) PrettyString() string

PrettyString returns a pretty-printed JSON representation of the ServerConfig

type SqlCnd

type SqlCnd struct {
	SelectCols []string     // 要查询的字段,如果为空,表示查询所有字段
	Params     []ParamPair  // 参数
	Orders     []OrderByCol // 排序
	Paging     *Paging      // 分页
}

func NewSqlCnd

func NewSqlCnd(selectCols ...string) *SqlCnd

selectCols: 需要查询的列

func (*SqlCnd) Asc

func (s *SqlCnd) Asc(column string) *SqlCnd

func (*SqlCnd) Build

func (s *SqlCnd) Build(db *gorm.DB) *gorm.DB

func (*SqlCnd) Count

func (s *SqlCnd) Count(db *gorm.DB, model interface{}) int

func (*SqlCnd) Desc

func (s *SqlCnd) Desc(column string) *SqlCnd

func (*SqlCnd) Ending

func (s *SqlCnd) Ending(column string, str string) *SqlCnd

func (*SqlCnd) Eq

func (s *SqlCnd) Eq(column string, args ...interface{}) *SqlCnd

func (*SqlCnd) Find

func (s *SqlCnd) Find(db *gorm.DB, out interface{})

func (*SqlCnd) FindOne

func (s *SqlCnd) FindOne(db *gorm.DB, out interface{}) error

func (*SqlCnd) Gt

func (s *SqlCnd) Gt(column string, args ...interface{}) *SqlCnd

func (*SqlCnd) Gte

func (s *SqlCnd) Gte(column string, args ...interface{}) *SqlCnd

func (*SqlCnd) In

func (s *SqlCnd) In(column string, params interface{}) *SqlCnd

func (*SqlCnd) Like

func (s *SqlCnd) Like(column string, str string) *SqlCnd

func (*SqlCnd) Limit

func (s *SqlCnd) Limit(limit int) *SqlCnd

func (*SqlCnd) Lt

func (s *SqlCnd) Lt(column string, args ...interface{}) *SqlCnd

func (*SqlCnd) Lte

func (s *SqlCnd) Lte(column string, args ...interface{}) *SqlCnd

func (*SqlCnd) NotEq

func (s *SqlCnd) NotEq(column string, args ...interface{}) *SqlCnd

func (*SqlCnd) Page

func (s *SqlCnd) Page(page, limit int) *SqlCnd

func (*SqlCnd) Starting

func (s *SqlCnd) Starting(column string, str string) *SqlCnd

func (*SqlCnd) Where

func (s *SqlCnd) Where(query string, args ...interface{}) *SqlCnd

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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