init: 第一次提交
- 以实现登录获取个人信息和课程 - 实现了获取视频记录 - 实现了学习接口
This commit is contained in:
22
pkg/common/rand.go
Normal file
22
pkg/common/rand.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Rand 生成指定长度的随机字符串,字符集为 [0-9a-zA-Z]
|
||||
func Rand(length int) string {
|
||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
||||
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
b[i] = charset[seededRand.Intn(len(charset))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func RandFloat64() float64 {
|
||||
return rand.Float64()
|
||||
}
|
||||
73
pkg/log/func.go
Normal file
73
pkg/log/func.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// GetLogger 获取原生 zap logger
|
||||
func GetLogger() *zap.Logger {
|
||||
return logger
|
||||
}
|
||||
|
||||
// Sync 刷新缓冲区
|
||||
func Sync() {
|
||||
_ = logger.Sync()
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 结构化日志 (Structured Logging) - 推荐高性能场景使用
|
||||
// 用法: log.Info("user login", zap.String("username", "admin"))
|
||||
// ============================================================================
|
||||
|
||||
func Debug(msg string, fields ...zap.Field) {
|
||||
logger.Debug(msg, fields...)
|
||||
}
|
||||
|
||||
func Info(msg string, fields ...zap.Field) {
|
||||
logger.Info(msg, fields...)
|
||||
}
|
||||
|
||||
func Warn(msg string, fields ...zap.Field) {
|
||||
logger.Warn(msg, fields...)
|
||||
}
|
||||
|
||||
func Error(msg string, fields ...zap.Field) {
|
||||
logger.Error(msg, fields...)
|
||||
}
|
||||
|
||||
func Fatal(msg string, fields ...zap.Field) {
|
||||
logger.Fatal(msg, fields...)
|
||||
}
|
||||
|
||||
func Panic(msg string, fields ...zap.Field) {
|
||||
logger.Panic(msg, fields...)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 格式化日志 (Sugared Logging) - 方便使用,类似 fmt.Printf
|
||||
// 用法: log.Infof("user %s login failed, count: %d", "admin", 3)
|
||||
// ============================================================================
|
||||
|
||||
func Debugf(template string, args ...interface{}) {
|
||||
sugar.Debugf(template, args...)
|
||||
}
|
||||
|
||||
func Infof(template string, args ...interface{}) {
|
||||
sugar.Infof(template, args...)
|
||||
}
|
||||
|
||||
func Warnf(template string, args ...interface{}) {
|
||||
sugar.Warnf(template, args...)
|
||||
}
|
||||
|
||||
func Errorf(template string, args ...interface{}) {
|
||||
sugar.Errorf(template, args...)
|
||||
}
|
||||
|
||||
func Fatalf(template string, args ...interface{}) {
|
||||
sugar.Fatalf(template, args...)
|
||||
}
|
||||
|
||||
func Panicf(template string, args ...interface{}) {
|
||||
sugar.Panicf(template, args...)
|
||||
}
|
||||
105
pkg/log/log.go
Normal file
105
pkg/log/log.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
logger *zap.Logger
|
||||
sugar *zap.SugaredLogger
|
||||
)
|
||||
|
||||
// 通用时间格式
|
||||
const (
|
||||
TimeFormatDate = "2006-01-02"
|
||||
TimeFormatDateTime = "2006-01-02 15:04:05"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Level string
|
||||
Filepath string
|
||||
MaxSizeMB int
|
||||
MaxAgeDay int
|
||||
Backups int
|
||||
Compress bool
|
||||
}
|
||||
|
||||
func init() {
|
||||
encoderConfig := zap.NewDevelopmentEncoderConfig()
|
||||
core := zapcore.NewCore(
|
||||
zapcore.NewConsoleEncoder(encoderConfig),
|
||||
zapcore.AddSync(os.Stdout),
|
||||
zap.DebugLevel,
|
||||
)
|
||||
|
||||
logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
|
||||
sugar = logger.Sugar()
|
||||
}
|
||||
|
||||
func Init(cfg Config) {
|
||||
var zapLevel zapcore.Level
|
||||
|
||||
// 日志等级解析
|
||||
switch cfg.Level {
|
||||
case "debug":
|
||||
zapLevel = zap.DebugLevel
|
||||
case "info":
|
||||
zapLevel = zap.InfoLevel
|
||||
case "warning", "warn":
|
||||
zapLevel = zap.WarnLevel
|
||||
case "error":
|
||||
zapLevel = zap.ErrorLevel
|
||||
default:
|
||||
zapLevel = zap.InfoLevel
|
||||
}
|
||||
|
||||
// lumberjack 日志切割配置
|
||||
writeSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: cfg.Filepath,
|
||||
MaxSize: cfg.MaxSizeMB,
|
||||
MaxBackups: cfg.Backups,
|
||||
MaxAge: cfg.MaxAgeDay,
|
||||
Compress: cfg.Compress,
|
||||
})
|
||||
|
||||
// 基础编码配置 (提取公共部分)
|
||||
baseEencoderConfig := zapcore.EncoderConfig{
|
||||
TimeKey: "time",
|
||||
LevelKey: "level",
|
||||
NameKey: "logger",
|
||||
CallerKey: "caller",
|
||||
MessageKey: "msg",
|
||||
StacktraceKey: "Stacktrace",
|
||||
LineEnding: zapcore.DefaultLineEnding,
|
||||
EncodeLevel: zapcore.CapitalLevelEncoder,
|
||||
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(t.Format(TimeFormatDateTime))
|
||||
},
|
||||
EncodeDuration: zapcore.StringDurationEncoder,
|
||||
EncodeCaller: zapcore.ShortCallerEncoder,
|
||||
}
|
||||
|
||||
// 1. 控制台编码器(开启颜色)
|
||||
consoleEncoderConfig := baseEencoderConfig
|
||||
consoleEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
encoderConsole := zapcore.NewConsoleEncoder(consoleEncoderConfig)
|
||||
|
||||
// 2.文件编码器(json 格式, 无色)
|
||||
fileEncoderConfig := baseEencoderConfig
|
||||
fileEncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
encoderJson := zapcore.NewJSONEncoder(fileEncoderConfig)
|
||||
|
||||
core := zapcore.NewTee(
|
||||
zapcore.NewCore(encoderJson, writeSyncer, zapLevel),
|
||||
zapcore.NewCore(encoderConsole, zapcore.AddSync(os.Stdout), zapLevel))
|
||||
|
||||
logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
|
||||
sugar = logger.Sugar()
|
||||
|
||||
zap.ReplaceGlobals(logger)
|
||||
}
|
||||
71
pkg/request/client.go
Normal file
71
pkg/request/client.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"resty.dev/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
NoRedirectClient *resty.Client
|
||||
RestyClient *resty.Client
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0"
|
||||
DefaultTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Timeout time.Duration
|
||||
Proxy string
|
||||
Debug bool
|
||||
UserAgent string
|
||||
VerifySSL bool
|
||||
}
|
||||
|
||||
func DefaultConfg() *Config {
|
||||
return &Config{
|
||||
Timeout: DefaultTimeout,
|
||||
UserAgent: DefaultUserAgent,
|
||||
VerifySSL: true,
|
||||
Debug: false,
|
||||
}
|
||||
}
|
||||
|
||||
// NewClient 创建一个标准的 Resty 客户端
|
||||
func NewClient(cfg *Config) *resty.Client {
|
||||
if cfg == nil {
|
||||
cfg = DefaultConfg()
|
||||
}
|
||||
|
||||
client := resty.New()
|
||||
client.SetHeader("User-Agent", cfg.UserAgent)
|
||||
client.SetTimeout(cfg.Timeout)
|
||||
client.SetRetryCount(3)
|
||||
|
||||
client.SetTLSClientConfig(&tls.Config{
|
||||
InsecureSkipVerify: !cfg.VerifySSL,
|
||||
})
|
||||
|
||||
if cfg.Proxy != "" {
|
||||
client.SetProxy(cfg.Proxy)
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// NewNoRedirectClient 创建一个禁止重定向的客户端
|
||||
func NewNoRedirectClient(cfg *Config) *resty.Client {
|
||||
client := NewClient(cfg)
|
||||
|
||||
client.SetRedirectPolicy(
|
||||
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}),
|
||||
)
|
||||
|
||||
return client
|
||||
}
|
||||
Reference in New Issue
Block a user