Documentation ¶
Overview ¶
Common and useful utils for the Go project development.
Inclusion criteria:
- Only rely on the Go standard package
- Functions or lightweight packages
- Non-business related general tools
Index ¶
- Constants
- Variables
- func AESDecrypt(cipherkey, ciphertext []byte) ([]byte, error)
- func AESEncrypt(cipherkey, src []byte) []byte
- func BytesToString(b []byte) string
- func CamelString(s string) string
- func CopyFile(dstName, srcName string) (written int64, err error)
- func CreateDB() (err error)
- func EncryPassword(pwd string, key ...string) (string, error)
- func ExtranetIP() (ip string, err error)
- func FileExists(name string) bool
- func GetFileSize(file *os.File) (int64, error)
- func GetPID(pidFile ...string) (pid int64, err error)
- func GrepFile(patten string, filename string) (lines []string, err error)
- func InSlice(str string, slice []string) bool
- func InitCache()
- func Initialize()
- func IntranetIP() (string, error)
- func IsExportedName(name string) bool
- func IsExportedOrBuiltinType(t reflect.Type) bool
- func JsQueryEscape(s string) string
- func JsQueryUnescape(s string) (string, error)
- func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc
- func MaxSliceInt64(slice []int64) int64
- func Md5(b []byte) string
- func Md5string(in string) string
- func MidCors() echo.MiddlewareFunc
- func MidGzip() echo.MiddlewareFunc
- func MidLogger() echo.MiddlewareFunc
- func MinSliceInt64(slice []int64) int64
- func ObjectName(obj interface{}) string
- func PanicTrace(kb int) []byte
- func RandomBytes(n int) []byte
- func ReadObjInformation(ptr Init) error
- func Recover() echo.MiddlewareFunc
- func RelPath(targpath string) string
- func SearchFile(filename string, paths ...string) (fullpath string, err error)
- func SelfChdir()
- func SelfDir() string
- func SelfName() string
- func SelfPath() string
- func SnakeString(s string) string
- func StatusText(code int) string
- func StringToBytes(s string) []byte
- func TodayDir(file *os.File) string
- func TouchFile(fileName string) error
- func URLRandomString(n int) string
- func VerifyPassword(pwd string, encryptPwd string, key ...string) (bool, error)
- func WalkDirs(targpath string, suffixes ...string) (dirlist []string)
- func WritePidFile(pidFile ...string)
- type CacheInterface
- type CurrentIP
- type Database
- type HTTPError
- type Init
- type JSONTime
- func (p *JSONTime) GobDecode(data []byte) error
- func (p JSONTime) GobEncode() ([]byte, error)
- func (p JSONTime) MarshalJSON() ([]byte, error)
- func (p *JSONTime) Scan(v interface{}) error
- func (p JSONTime) String() string
- func (p *JSONTime) UnmarshalJSON(data []byte) error
- func (p JSONTime) Value() (driver.Value, error)
- type Log
- type Logger
- type LoggerConfig
- type Logs
- type Map
- type MemoryCache
- func (ce *MemoryCache) Bind(key string, bean interface{}) error
- func (ce *MemoryCache) Del(key string) error
- func (ce *MemoryCache) DelAll() error
- func (ce *MemoryCache) Exists(key string) bool
- func (ce *MemoryCache) Get(key string) ([]byte, error)
- func (ce *MemoryCache) GetInt64(key string) (int64, error)
- func (ce *MemoryCache) GetValue(key string) (str string, err error)
- func (ce *MemoryCache) Remember(key string, set func() error) (b []byte, err error)
- func (ce *MemoryCache) RememberBind(key string, bean interface{}, set func() error) error
- func (ce *MemoryCache) Set(key string, value interface{}, exp time.Duration) error
- type MemoryCache2
- func (m *MemoryCache2) Bind(key string, bean interface{}) error
- func (m *MemoryCache2) Del(key string) error
- func (m *MemoryCache2) DelAll() error
- func (m *MemoryCache2) Exists(key string) bool
- func (m *MemoryCache2) Get(key string) ([]byte, error)
- func (m *MemoryCache2) GetInt64(key string) (i int64, err error)
- func (m *MemoryCache2) GetValue(key string) (string, error)
- func (m *MemoryCache2) Remember(key string, set func() error) (b []byte, err error)
- func (m *MemoryCache2) RememberBind(key string, bean interface{}, set func() error) error
- func (m *MemoryCache2) Set(key string, value interface{}, exp time.Duration) error
- type Process
- type PullBind
- type Random
- type Redis
- type RedisCache
- func (r *RedisCache) Bind(key string, bean interface{}) error
- func (r *RedisCache) Del(key string) error
- func (r *RedisCache) DelAll() error
- func (r *RedisCache) Exists(key string) bool
- func (r *RedisCache) Get(key string) (b []byte, err error)
- func (r *RedisCache) GetInt64(key string) (int64, error)
- func (r *RedisCache) GetValue(key string) (string, error)
- func (r *RedisCache) JSONSet(key string, value interface{}, exp time.Duration) error
- func (r *RedisCache) Remember(key string, set func() error) (b []byte, err error)
- func (r *RedisCache) RememberBind(key string, bean interface{}, set func() error) error
- func (r *RedisCache) Set(key string, value interface{}, exp time.Duration) error
- type ReturnJSON
- type WxDeveloper
- type XOrmConf
Constants ¶
const ( CONNECT = "CONNECT" DELETE = "DELETE" GET = "GET" HEAD = "HEAD" OPTIONS = "OPTIONS" PATCH = "PATCH" POST = "POST" PROPFIND = "PROPFIND" PUT = "PUT" TRACE = "TRACE" )
HTTP methods
const ( MIMEApplicationJSON = "application/json" MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8 MIMEApplicationJavaScript = "application/javascript" MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8 MIMEApplicationXML = "application/xml" MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8 MIMETextXML = "text/xml" MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8 MIMEApplicationForm = "application/x-www-form-urlencoded" MIMEApplicationProtobuf = "application/protobuf" MIMEApplicationMsgpack = "application/msgpack" MIMETextHTML = "text/html" MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8 MIMETextPlain = "text/plain" MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8 MIMEMultipartForm = "multipart/form-data" MIMEOctetStream = "application/octet-stream" )
MIME types
const ( HeaderAccept = "Accept" HeaderAcceptEncoding = "Accept-Encoding" HeaderAllow = "Allow" HeaderAuthorization = "Authorization" HeaderContentDisposition = "Content-Disposition" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderCookie = "Cookie" HeaderSetCookie = "Set-Cookie" HeaderIfModifiedSince = "If-Modified-Since" HeaderLastModified = "Last-Modified" HeaderLocation = "Location" HeaderUpgrade = "Upgrade" HeaderVary = "Vary" HeaderWWWAuthenticate = "WWW-Authenticate" HeaderXForwardedFor = "X-Forwarded-For" HeaderXForwardedProto = "X-Forwarded-Proto" HeaderXForwardedProtocol = "X-Forwarded-Protocol" HeaderXForwardedSsl = "X-Forwarded-Ssl" HeaderXUrlScheme = "X-Url-Scheme" HeaderXHTTPMethodOverride = "X-HTTP-Method-Override" HeaderXRealIP = "X-Real-IP" HeaderXRequestID = "X-Request-ID" HeaderXRequestedWith = "X-Requested-With" HeaderServer = "Server" HeaderOrigin = "Origin" HeaderExpires = "Expires" HeaderCacheControl = "Cache-Control" HeaderPragma = "Pragma" // Access control HeaderAccessControlRequestMethod = "Access-Control-Request-Method" HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" HeaderAccessControlMaxAge = "Access-Control-Max-Age" // Security HeaderStrictTransportSecurity = "Strict-Transport-Security" HeaderXContentTypeOptions = "X-Content-Type-Options" HeaderXXSSProtection = "X-XSS-Protection" HeaderXFrameOptions = "X-Frame-Options" HeaderContentSecurityPolicy = "Content-Security-Policy" HeaderXCSRFToken = "X-CSRF-Token" )
Headers
const ( KB int64 = 1 << (10 * iota) // 1 << (10*1) MB // 1 << (10*2) GB // 1 << (10*3) TB // 1 << (10*4) LogPackSize = 1 * MB LogBackUpDays = 3 )
const ( Version = "v2.0.0" ServeName = "ty-chat" )
const (
Crs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
)
const (
Custom = "2006-01-02 15:04:05"
)
const (
ErrCodeNotFound = iota + 1
)
const (
UTF8 = "UTF-8"
)
Variables ¶
var ( DefCache CacheInterface RedisGlobalCache *RedisCache MeCache2 *MemoryCache2 )
DefCache ...
var ( ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType) ErrNotFound = NewHTTPError(http.StatusNotFound) ErrForbidden = NewHTTPError(http.StatusForbidden) ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed) ErrStatusRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge) ErrValidatorNotRegistered = errors.New("validator not registered") ErrRendererNotRegistered = errors.New("renderer not registered") ErrInvalidRedirectCode = errors.New("invalid redirect status code") ErrCookieNotFound = errors.New("cookie not found") )
Errors
var ( DefaultProcess = Process{ ListenPort: 9001, Mode: "release", CacheType: "memory", Secret: random.String(32, random.Uppercase+random.Numeric), } DefaultRedis = Redis{ Addr: "127.0.0.1:6379", Password: "", DB: 1, } DefaultWxDeveloper = WxDeveloper{ AppID: "", AppSecret: "", } DefaultPull = PullBind{ PullDomain: "yh8.in:39001", SoftName: "ty-chat", Branch: "dev-v1-charles", Domain: "127.0.0.1:9001", } WebFilePath = filepath.Join(SelfDir(), "conf", "web.ini") InIConf = func() *ini.File { var ( f *ini.File err error ) if err = TouchFile(WebFilePath); err != nil { panic(err.Error()) } if f, err = ini.Load(WebFilePath); err != nil { panic("web.ini 配置文件加载错误:" + err.Error()) } return f }() //Queue External configuration, you must pass the queue in the use method. Queue = make(chan func(), 100) Job = grpool.NewPool(runtime.NumCPU()*100, runtime.NumCPU()*2) )
var ( Cron = func() *cron.Cron { cb := cron.New() cb.Start() return cb }() )
var DefSecretString = "rYtY0RD5hvN2T0McxjNWfH1MM7PExE0w"
DefSecretString ...
var ( // DefaultLoggerConfig is the default Logger middleware config. DefaultLoggerConfig = LoggerConfig{ Skipper: middleware.DefaultSkipper, Format: `{"time":"${time_custom}","id":"${id}","remote_ip":"${remote_ip}","referer":"${referer}","host":"${host}",` + `"method":"${method}","uri":"${uri}","status":${status},"error":"${error}","latency":${latency},` + `"latency_human":"${latency_human}","bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n", CustomTimeFormat: "2006-01-02 15:04:05.00000", Output: nil, // contains filtered or unexported fields } )
var DefaultPidFile = filepath.Join(SelfDir(), "PID")
DEFAULT_PID_FILE the default PID file name
var Delimiter = func() string { if runtime.GOOS == "windows" { return "\\" } return "/" }()
Delimiter ... 文件夹目录分隔符
var (
Engine *xorm.EngineGroup
)
Functions ¶
func AESDecrypt ¶
AESDecrypt decrypts a piece of data. The cipherkey argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func AESEncrypt ¶
AESEncrypt encrypts a piece of data. The cipherkey argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func BytesToString ¶
BytesToString convert []byte type to string type.
func CamelString ¶
CamelString converts the accepted string to a camel string (xx_yy to XxYy)
func EncryPassword ¶
EncryPassword ... 加密一个密码
func FileExists ¶
FileExists reports whether the named file or directory exists.
func GetFileSize ¶
GetFileSize 获取当前文件大小 kb >>10 , mb >>1e2
func GrepFile ¶
GrepFile like command grep -E for example: GrepFile(`^hello`, "hello.txt") \n is striped while read
func Initialize ¶
func Initialize()
Initialize ... This method needs to be referenced when the configuration file needs to be initialized
func IsExportedName ¶
IsExportedName is this an exported - upper case - name?
func IsExportedOrBuiltinType ¶
IsExportedOrBuiltinType is this type exported or a builtin?
func JsQueryEscape ¶
JsQueryEscape escapes the string in javascript standard so it can be safely placed inside a URL query.
func JsQueryUnescape ¶
JsQueryUnescape does the inverse transformation of JsQueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.
func LoggerWithConfig ¶
func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc
LoggerWithConfig returns a Logger middleware with config. See: `Logger()`.
func MidCors ¶
func MidCors() echo.MiddlewareFunc
func MidGzip ¶
func MidGzip() echo.MiddlewareFunc
func MidLogger ¶
func MidLogger() echo.MiddlewareFunc
Logger returns a middleware that logs HTTP requests.
func ObjectName ¶
func ObjectName(obj interface{}) string
ObjectName gets the type name of the object
func RandomBytes ¶
RandomBytes returns securely generated random bytes. It will panic if the system's secure random number generator fails to function correctly.
func ReadObjInformation ¶
ReadObjInformation Read information from the configuration file into a global variable, and if there is no information about the object in the configuration file, write the initial properties of the object to the configuration file.
func Recover ¶
func Recover() echo.MiddlewareFunc
Recover returns a middleware which recovers from panics anywhere in the chain and handles the control to the centralized HTTPErrorHandler.
func SearchFile ¶
SearchFile Search a file in paths. this is often used in search config file in /etc ~/
func SnakeString ¶
SnakeString converts the accepted string to a snake string (XxYy to xx_yy)
func StatusText ¶
func StringToBytes ¶
StringToBytes convert string type to []byte type. NOTE: panic if modify the member value of the []byte.
func TouchFile ¶
TouchFile If the file does not exist, it is created automatically. Do not do anything if the file already exists.
func URLRandomString ¶
URLRandomString returns a URL-safe, base64 encoded securely generated random string. It will panic if the system's secure random number generator fails to function correctly. The length n must be an integer multiple of 4, otherwise the last character will be padded with `=`.
func VerifyPassword ¶
VerifyPassword ... 验证一个密码
func WalkDirs ¶
WalkDirs traverses the directory, return to the relative path. You can specify the suffix.
func WritePidFile ¶
func WritePidFile(pidFile ...string)
WritePidFile writes the current PID to the specified file.
Types ¶
type CacheInterface ¶
type CacheInterface interface { Get(key string) ([]byte, error) //if cannot get value,return 0 GetInt64(key string) (int64, error) //return "" if can't get value GetValue(key string) (string, error) //bind value to struct point Bind(key string, bean interface{}) error Set(key string, value interface{}, exp time.Duration) error Remember(key string, set func() error) ([]byte, error) RememberBind(key string, bean interface{}, set func() error) error Exists(key string) bool Del(key string) error DelAll() error }
CacheInterface ... 缓存接口
type CurrentIP ¶
func NewCurrentIP ¶
type Database ¶
type Database struct { Type string `yaml:",omitempty"` Address string Port int User string `yaml:",omitempty"` Password string `yaml:",omitempty"` DB string `yaml:",omitempty"` }
Database configuration Settings
type HTTPError ¶
type HTTPError struct { Code int `json:"code"` Message interface{} `json:"message"` Internal error `json:"-"` //Errors returned by external dependencies can be stored }
HTTPError represents an error that occurred while handling a request.
func NewHTTPError ¶
NewHTTPError creates a new HTTPError instance.
type JSONTime ¶
JSONTime ...
func (*JSONTime) UnmarshalJSON ¶
UnmarshalJSON ...
type Log ¶
type Log struct { ConsoleEnable bool FileEnable bool FilePath string // Program current directory;`logs/web` }
Log Log system Settings The server log system is placed in the "logs/web" directory. Whenever the log file size exceeds 1MB, the system will automatically backup the log file. The system will only back up the log file for the last 3 days.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func GetWebErrLogger ¶
func GetWebErrLogger() *Logger
func (*Logger) DelDirForDays ¶
func (l *Logger) DelDirForDays()
func (*Logger) GetGLogger ¶
type LoggerConfig ¶
type LoggerConfig struct { // Skipper defines a function to skip middleware. Skipper middleware.Skipper // Tags to constructed the logger format. // // - time_unix // - time_unix_nano // - time_rfc3339 // - time_rfc3339_nano // - time_custom // - id (Request ID) // - remote_ip // - uri // - host // - method // - path // - protocol // - referer // - user_agent // - status // - error // - latency (In nanoseconds) // - latency_human (Human readable) // - bytes_in (Bytes received) // - bytes_out (Bytes sent) // - header:<NAME> // - query:<NAME> // - form:<NAME> // // Example "${remote_ip} ${status}" // // Optional. Default value DefaultLoggerConfig.Format. Format string `yaml:"format"` // Optional. Default value DefaultLoggerConfig.CustomTimeFormat. CustomTimeFormat string `yaml:"custom_time_format"` // Output is a writer where logs in JSON format are written. // Optional. Default value os.Stdout. Output io.Writer // contains filtered or unexported fields }
LoggerConfig defines the config for Logger middleware.
type Logs ¶
type Logs interface { Printf(format string, args ...interface{}) Fatalf(format string, args ...interface{}) Panicf(format string, args ...interface{}) }
Logger defines the logging interface.
type Map ¶
type Map interface { // Load returns the value stored in the map for a key, or nil if no // value is present. // The ok result indicates whether value was found in the map. Load(key interface{}) (value interface{}, ok bool) // Store sets the value for a key. Store(key, value interface{}) // LoadOrStore returns the existing value for the key if present. // Otherwise, it stores and returns the given value. // The loaded result is true if the value was loaded, false if stored. LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) // Range calls f sequentially for each key and value present in the map. // If f returns false, range stops the iteration. Range(f func(key, value interface{}) bool) // Random returns a pair kv randomly. // If exist=false, no kv data is exist. Random() (key, value interface{}, exist bool) // Delete deletes the value for a key. Delete(key interface{}) // Clear clears all current data in the map. Clear() // Len returns the length of the map. Len() int }
Map is a concurrent map with loads, stores, and deletes. It is safe for multiple goroutines to call a Map's methods concurrently.
type MemoryCache ¶
type MemoryCache struct {
cache.Cache
}
var (
MeCache *MemoryCache
)
func NewMemoryCache ¶
func NewMemoryCache() *MemoryCache
func (*MemoryCache) Bind ¶
func (ce *MemoryCache) Bind(key string, bean interface{}) error
func (*MemoryCache) Del ¶
func (ce *MemoryCache) Del(key string) error
func (*MemoryCache) DelAll ¶
func (ce *MemoryCache) DelAll() error
func (*MemoryCache) Exists ¶
func (ce *MemoryCache) Exists(key string) bool
func (*MemoryCache) Remember ¶
func (ce *MemoryCache) Remember(key string, set func() error) (b []byte, err error)
func (*MemoryCache) RememberBind ¶
func (ce *MemoryCache) RememberBind(key string, bean interface{}, set func() error) error
type MemoryCache2 ¶
MemoryCache ...
func (*MemoryCache2) Bind ¶
func (m *MemoryCache2) Bind(key string, bean interface{}) error
Bind ...
func (*MemoryCache2) GetInt64 ¶
func (m *MemoryCache2) GetInt64(key string) (i int64, err error)
GetInt64 ...
func (*MemoryCache2) GetValue ¶
func (m *MemoryCache2) GetValue(key string) (string, error)
GetValue ...
func (*MemoryCache2) Remember ¶
func (m *MemoryCache2) Remember(key string, set func() error) (b []byte, err error)
Remember ...
func (*MemoryCache2) RememberBind ¶
func (m *MemoryCache2) RememberBind(key string, bean interface{}, set func() error) error
RememberBind ...
type Process ¶
type Process struct { // server listen port ListenPort int `json:"listen_port" ini:"listen_port" comment:"监听端口"` // program run mode,debug or release Mode string `json:"mode" ini:"mode" comment:"模式,debug,release"` // redis or memory CacheType string `json:"cache_type" ini:"cache_type" comment:"redis or memory"` // program secret , use to jwt Secret string `json:"secret" ini:"secret" comment:"program secret , use to jwt,一定不能修改/删除"` ReadTimeOut time.Duration `json:"read_time_out" ini:"read_time_out"` WriteTimeOut time.Duration `json:"write_time_out" ini:"write_time_out"` }
Process Program global configuration items
type PullBind ¶
type PullBind struct { PullDomain string `json:"pull_domain" ini:"pull_domain"` SoftName string `json:"soft_name" ini:"soft_name"` Branch string `json:"branch" ini:"branch"` Domain string `json:"domain" ini:"domain"` }
pull config
type Random ¶
type Random struct {
// contains filtered or unexported fields
}
Random random string creater.
func (*Random) RandomString ¶
RandomString returns a base64 encoded securely generated random string. It will panic if the system's secure random number generator fails to function correctly. The length n must be an integer multiple of 4, otherwise the last character will be padded with `=`.
type Redis ¶
type Redis struct { Addr string `json:"addr" ini:"addr"` // redis server address;example:127.0.0.1:6379 Password string `json:"password" ini:"password"` DB int `json:"db" ini:"db"` }
Redis set;need cacheType = `redis`
type RedisCache ¶
RedisCache ...
func (*RedisCache) GetValue ¶
func (r *RedisCache) GetValue(key string) (string, error)
GetValue ...
func (*RedisCache) JSONSet ¶
func (r *RedisCache) JSONSet(key string, value interface{}, exp time.Duration) error
JSONSet ... 将一个对象序列化成 json 字符串,并进行存储
func (*RedisCache) Remember ¶
func (r *RedisCache) Remember(key string, set func() error) (b []byte, err error)
Remember ...
func (*RedisCache) RememberBind ¶
func (r *RedisCache) RememberBind(key string, bean interface{}, set func() error) error
RememberBind ...
type ReturnJSON ¶
type ReturnJSON struct { Code int `json:"code"` StatusText string `json:"status_text"` Message interface{} `json:"message,omitempty"` }
func NewReturnJSON ¶
func NewReturnJSON() *ReturnJSON
func (*ReturnJSON) Ok ¶
func (r *ReturnJSON) Ok(i interface{}) *ReturnJSON
func (*ReturnJSON) Set ¶
func (r *ReturnJSON) Set(code int, msg interface{}) *ReturnJSON
type WxDeveloper ¶
type WxDeveloper struct { AppID string `json:"app_id" ini:"AppID" comment:""` AppSecret string `json:"app_secret" ini:"AppSecret" comment:""` }
Init ...
func (*WxDeveloper) InitFunc ¶
func (p *WxDeveloper) InitFunc() error