gotk

package module
v0.0.0-...-fce6733 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 30 Imported by: 4

README

gox

golang toolkits and implments

migrate from github.com/d2jvkpn/go-web/pkg/misc

Documentation

Index

Constants

View Source
const (
	RFC3339ms = "2006-01-02T15:04:05.000Z07:00"
)

Variables

This section is empty.

Functions

func BasenameWithoutExt

func BasenameWithoutExt(cf string) (base string)

func BuildInfo

func BuildInfo() (info map[string]any)

func BuildInfoText

func BuildInfoText(info map[string]any) string

func Caller

func Caller(skips ...int) (string, string)

func CheckDuplicateFilename

func CheckDuplicateFilename(p string) (out string, err error)

func EqualVector

func EqualVector[T constraints.Ordered](arr1, arr2 []T) (ok bool)

func FileSize2Str

func FileSize2Str(n int64) string

func First

func First[T any](v []T) *T

func GetIP

func GetIP(addrs ...string) (ip string, err error)

func GetOClock

func GetOClock(shift int) (clock time.Time)

func Hello

func Hello()

func Last

func Last[T any](v []T) *T

func LoadPprof

func LoadPprof(mux *http.ServeMux)

create new Pprof and run server

func LoadYamlBytes

func LoadYamlBytes(bts []byte) (vp *viper.Viper, err error)

func LoadYamlConfig

func LoadYamlConfig(fp, name string) (vp *viper.Viper, err error)

func MiddlewareFunc

func MiddlewareFunc(origin string, hf http.HandlerFunc) http.HandlerFunc

func MiddlewareHandler

func MiddlewareHandler(origin string, next http.Handler) http.Handler

func NewTlsConfig

func NewTlsConfig(vp *viper.Viper, field string) (cert *tls.Certificate, err error)

func NowMs

func NowMs() string

func ParseDatetime

func ParseDatetime(value string) (at time.Time, err error)

datetime format: "2021-06-24", "09:00:01", "2021-06-24 09:10:11" or "2021-06-24T09:10:11"

func ParsePorts

func ParsePorts(str string) (ports []uint64, err error)

parse ports from string: 8000,8001,8002-8009

func PortFromAddr

func PortFromAddr(addr string) (port int, err error)

func PprofCollect

func PprofCollect(dir string, secs, hz int) (out string, err error)

hz = 100 is recommended

```bash

go install github.com/google/pprof@latest
pprof -eog wk_data/2022-12-01T20-38-26_10s_100hz_cpu.pprof.gz
go tool pprof -eog wk_data/2022-12-01T20-38-26_10s_100hz_cpu.pprof.gz

```

func PprofFuncKeys

func PprofFuncKeys() []string

func PprofHandlerFuncs

func PprofHandlerFuncs() map[string]http.HandlerFunc

web browser address

http://localhost:8080/debug/pprof/

#### get profiles and view in browser

```bash go tool pprof -http=:8081 http://localhost:8080/debug/pprof/allocs?seconds=30

go tool pprof http://localhost:8080/debug/pprof/block?seconds=30

go tool pprof http://localhost:8080/debug/pprof/goroutine?seconds=30

go tool pprof http://localhost:8080/debug/pprof/heap?seconds=30

go tool pprof http://localhost:8080/debug/pprof/mutex?seconds=30

go tool pprof http://localhost:8080/debug/pprof/profile?seconds=30

go tool pprof http://localhost:8080/debug/pprof/threadcreate?seconds=30 ````

#### download profile file and convert to svg image ```bash wget -O profile.out localhost:8080/debug/pprof/profile?seconds=30

go tool pprof -svg profile.out > profile.svg ```

####get pprof in 30 seconds and save to svg image ```bash go tool pprof -svg http://localhost:8080/debug/pprof/allocs?seconds=30 > allocs.svg ```

#### get trace in 5 seconds ```bash wget -O trace.out http://localhost:8080/debug/pprof/trace?seconds=5

go tool trace trace.out ```

#### get cmdline and symbol binary data ```bash wget -O cmdline.out http://localhost:8080/debug/pprof/cmdline

wget -O symbol.out http://localhost:8080/debug/pprof/symbol ```

func RandString

func RandString(n int) string

func RegisterLogPrinter

func RegisterLogPrinter()

func RootDir

func RootDir() (dir string, err error)

get root path of project by recursively match go.mod in parent directory

func RootFile

func RootFile(p2f ...string) (fp string, err error)

func RootModule

func RootModule() (mod string, err error)

func SliceGet

func SliceGet[T any](slice []T, index int) (val T, exists bool)

func Stack

func Stack(prefix string) (slice []string)

func TimeCeil

func TimeCeil(at time.Time, tu string) (out time.Time, err error)

ceil time, e.g. TimeCeil('2020-12-01T17:39:07.123+08:00', "M") -> '2020-12-01T17:40:00+08:00'

valid unit(key or value)
H: hour, M: minute, S: second
y: year, s: season, m: month, w: week, d: day

func TimeFloor

func TimeFloor(at time.Time, tu string) (out time.Time, err error)

floor time, e.g. TimeFloor('2020-12-01T17:39:07.123+08:00', "M") -> '2020-12-01T17:39:00+08:00'

valid unit(key or value)
H: hour, M: minute, S: second
y: year, s: season, m: month, w: week, d: day

func TimeTag

func TimeTag(at time.Time) string

func UniqVector

func UniqVector[T constraints.Ordered](arr []T) (list []T)

func UnmarshalYamlBytes

func UnmarshalYamlBytes(bts []byte, obj any) (err error)

func UnmarshalYamlObjects

func UnmarshalYamlObjects(fp string, objects map[string]any) (err error)

func VectorIndex

func VectorIndex[T constraints.Ordered](list []T, v T) int

Types

type BasicAuthUser

type BasicAuthUser struct {
	Username string `mapstructure:"username"`
	Password string `mapstructure:"password"`
	Value    string `mapstructure:"value"`
}

type BasicAuths

type BasicAuths struct {
	Enable bool            `mapstructure:"enable"`
	Method string          `mapstructure:"method"`
	Users  []BasicAuthUser `mapstructure:"users"`
	// contains filtered or unexported fields
}

func NewBasicAuths

func NewBasicAuths(vp *viper.Viper, field string) (auth *BasicAuths, err error)

func (*BasicAuths) Handle

func (auth *BasicAuths) Handle(w http.ResponseWriter, r *http.Request) (
	user *BasicAuthUser, code string, err error)

func (*BasicAuths) Validate

func (auth *BasicAuths) Validate() (err error)

type DefaultLogger

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

func NewDefaultLogger

func NewDefaultLogger(writer io.Writer, withTs bool) (logger *DefaultLogger)

func (*DefaultLogger) Debug

func (logger *DefaultLogger) Debug(format string, a ...any)

func (*DefaultLogger) Error

func (logger *DefaultLogger) Error(format string, a ...any)

func (*DefaultLogger) Info

func (logger *DefaultLogger) Info(format string, a ...any)

func (*DefaultLogger) Printf

func (logger *DefaultLogger) Printf(format string, a ...any) (n int, err error)

func (*DefaultLogger) Trace

func (logger *DefaultLogger) Trace(format string, a ...any)

func (*DefaultLogger) Warn

func (logger *DefaultLogger) Warn(format string, a ...any)

type HttpContext

type HttpContext struct {
	*http.Request
	// contains filtered or unexported fields
}

func NewHttpContext

func NewHttpContext(key string, r *http.Request) *HttpContext

func (*HttpContext) GetData

func (ctx *HttpContext) GetData() map[string]any

func (*HttpContext) GetValue

func (ctx *HttpContext) GetValue(key string) (any, bool)

func (*HttpContext) Set

func (ctx *HttpContext) Set(key string, value any) bool

type LogIntf

type LogIntf interface {
	// Trace(string, ...any)
	Debug(string, ...any)
	Info(string, ...any)
	Warn(string, ...any)
	Error(string, ...any)
}

customize yourself, no concurrency safety guaranteed

type LogPrinter

type LogPrinter struct{}

func (*LogPrinter) Write

func (w *LogPrinter) Write(bts []byte) (int, error)

type LogWriter

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

a simple log writer

func NewLogWriter

func NewLogWriter(prefix string, w io.Writer) (lw *LogWriter, err error)

func (*LogWriter) Close

func (lw *LogWriter) Close() (err error)

func (*LogWriter) Register

func (lw *LogWriter) Register()

set as output of log pkg

func (*LogWriter) Write

func (lw *LogWriter) Write(bts []byte) (int, error)

type Retry

type Retry[T any] struct {
	// contains filtered or unexported fields
}

func NewRetry

func NewRetry[T any](timeout, periodMax time.Duration) Retry[T]

func (*Retry[T]) Do

func (self *Retry[T]) Do(call func() (*T, error)) (ans *T, err error)

type RuntimeInfo

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

func NewRuntimeInfo

func NewRuntimeInfo(handler func(map[string]string), seconds int64) *RuntimeInfo

func (*RuntimeInfo) End

func (item *RuntimeInfo) End()

func (*RuntimeInfo) Start

func (item *RuntimeInfo) Start()

type TlsConfig

type TlsConfig struct {
	Enable bool   `mapstructure:"enable"`
	Cert   string `mapstructure:"cert"`
	Key    string `mapstructure:"key"`
}

Directories

Path Synopsis
aliyun module
aws module
ginx module
impls module
kafka module
orm module
rate_limit module
trace_error module
vocechat module

Jump to

Keyboard shortcuts

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