This commit is contained in:
2025-04-02 10:16:07 +08:00
parent a5caa734c3
commit b5ef982645
24 changed files with 149 additions and 101 deletions

View File

@@ -0,0 +1,7 @@
package config
type App struct {
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务地址
Port int `mapstructure:"port" json:"port" yaml:"port"` // 服务端口
Prod bool `mapstructure:"prod" json:"prod" yaml:"prod"` // 是否正式
}

View File

@@ -0,0 +1,6 @@
package config
type Applet struct {
AppID string `mapstructure:"app_id" json:"app_id" yaml:"app_id"` // appid
AppSecret string `mapstructure:"app_secret" json:"app_secret" yaml:"app_secret"` // secret
}

View File

@@ -0,0 +1,9 @@
package config
type Captcha struct {
OpenCaptcha int `mapstructure:"open_captcha" json:"open_captcha" yaml:"open_captcha"` // 是否开启防爆次数
OpenCaptchaTimeout string `mapstructure:"open_captcha_timeout" json:"open_captcha_timeout" yaml:"open_captcha_timeout"` // 缓存超时时间
ImgWidth int `mapstructure:"img_width" json:"img_width" yaml:"img_width"` // 验证码图片宽度
ImgHeight int `mapstructure:"img_height" json:"img_height" yaml:"img_height"` // 验证码图片高度
KeyLong int `mapstructure:"key_long" json:"key_long" yaml:"key_long"` // 验证码长度
}

View File

@@ -0,0 +1,93 @@
package config
import (
"fmt"
"path/filepath"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var File *Config
const ConfigDefaultFile = "config.dev.yaml"
type Config struct {
App App `mapstructure:"app" json:"app" yaml:"app"`
DB DB `mapstructure:"db" json:"db" yaml:"db"`
Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
Cors Cors `mapstructure:"cors" json:"cors" yaml:"cors"`
JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
AliyunUpload AliyunUpload `mapstructure:"aliyunupload" json:"aliyunupload" yaml:"aliyunupload"`
TencentUpload TencentUpload `mapstructure:"tencentupload" json:"tencentupload" yaml:"tencentupload"`
Captcha Captcha `mapstructure:"captcha" json:"captcha"`
Applet Applet `mapstructure:"applet" json:"applet" yaml:"applet"`
Smb Smb `mapstructure:"smb" json:"smb" yaml:"smb"`
}
func New(path string) (*Config, error) {
fp := "."
fn := ConfigDefaultFile
if len(path) > 0 {
fp, fn = filepath.Split(path)
if len(fp) == 0 {
fp = "."
}
}
v := viper.New()
v.AddConfigPath(fp)
v.SetConfigName(fn)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
return nil, err
}
v.WatchConfig()
var conf *Config
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := v.Unmarshal(&conf); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&conf); err != nil {
return nil, err
}
return conf, nil
}
func Init(path string) error {
fp := "."
fn := ConfigDefaultFile
if len(path) > 0 {
fp, fn = filepath.Split(path)
if len(fp) == 0 {
fp = "."
}
}
v := viper.New()
v.AddConfigPath(fp)
v.SetConfigName(fn)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
return err
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err := v.Unmarshal(&File); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&File); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,5 @@
package config
type Cors struct {
Host string `mapstructure:"host" json:"host" yaml:"host"`
}

12
internal/pkg/config/db.go Normal file
View File

@@ -0,0 +1,12 @@
package config
type DB struct {
Driver string `mapstructure:"driver" json:"driver" yaml:"driver"` // 数据库类型
Host string `mapstructure:"host" json:"host" yaml:"host"` // 数据库地址
Port int `mapstructure:"port" json:"port" yaml:"port"` // 数据库端口
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
DBName string `mapstructure:"db_name" json:"db_name" yaml:"db_name"` // 数据库名称
MaxOpenConns int `mapstructure:"max_open_conns" json:"max_open_conns" yaml:"max_open_conns"` // 数据库名称
MaxIdleConns int `mapstructure:"max_idle_conns" json:"max_idle_conns" yaml:"max_idle_conns"` // 数据库名称
}

View File

@@ -0,0 +1,10 @@
package config
import "time"
type JWT struct {
SigningKey string `mapstructure:"signing_key" json:"signing_key" yaml:"signing_key"` // jwt签名
ExpiresTime time.Duration `mapstructure:"expires_time" json:"expires_time" yaml:"expires_time"` // 过期时间
RefreshTime time.Duration `mapstructure:"refresh_time" json:"refresh_time" yaml:"refresh_time"` // 刷新过期时间
Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者
}

View File

@@ -0,0 +1,8 @@
package config
type Redis struct {
Host string `mapstructure:"host" json:"host" yaml:"host"` // redis地址
Port int `mapstructure:"port" json:"port" yaml:"port"` // redis端口
Password string `mapstructure:"password" json:"password" yaml:"password"` // redis密码
DB int `mapstructure:"db" json:"db_name" yaml:"db"` // redis数据库
}

View File

@@ -0,0 +1,8 @@
package config
type Smb struct {
Host string `mapstructure:"host" json:"host" yaml:"host"`
Name string `mapstructure:"name" json:"name" yaml:"name"`
Pass string `mapstructure:"pass" json:"pass" yaml:"pass"`
Mount string `mapstructure:"mount" json:"mount" yaml:"mount"`
}

View File

@@ -0,0 +1,18 @@
package config
type AliyunUpload struct {
Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"`
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
AccessKeyID string `mapstructure:"access_key_id" json:"access_key_id" yaml:"access_key_id"`
AccessKeySecret string `mapstructure:"access_key_secret" json:"access_key_secret" yaml:"access_key_secret"`
}
type TencentUpload struct {
Region string `mapstructure:"region" json:"region" yaml:"region"`
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
AccessKeyID string `mapstructure:"access_key_id" json:"access_key_id" yaml:"access_key_id"`
AccessKeySecret string `mapstructure:"access_key_secret" json:"access_key_secret" yaml:"access_key_secret"`
AllowImageMaxSize int64 `mapstructure:"allow_image_max_size" json:"allow_image_max_size" yaml:"allow_image_max_size"`
AllowImageExtension string `mapstructure:"allow_image_extension" json:"allow_image_extension" yaml:"allow_image_extension"`
AllowFileMaxSize int64 `mapstructure:"allow_file_max_size" json:"allow_file_max_size" yaml:"allow_file_max_size"`
}

View File

@@ -4,7 +4,7 @@ import (
"os"
"time"
"management/internal/config"
"management/internal/pkg/config"
"github.com/natefinch/lumberjack"
"github.com/rs/zerolog"

View File

@@ -44,7 +44,7 @@ func RandomString(n int) string {
var sb strings.Builder
k := len(alphabet)
for i := 0; i < n; i++ {
for range n {
c := alphabet[randv2.IntN(k)]
sb.WriteByte(c)
}

View File

@@ -8,7 +8,7 @@ import (
"fmt"
"time"
"management/internal/config"
"management/internal/pkg/config"
"github.com/redis/go-redis/v9"
)

View File

@@ -4,7 +4,7 @@ import (
"io/fs"
"net"
"management/internal/config"
"management/internal/pkg/config"
"github.com/hirochachacha/go-smb2"
)

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"time"
"management/internal/config"
"management/internal/pkg/config"
"github.com/aead/chacha20poly1305"
"github.com/o1egl/paseto"