添加前端页面以及相关打包脚本和内置 web 的逻辑
This commit is contained in:
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -14,6 +15,11 @@ type Config struct {
|
||||
Storage StorageConfig `yaml:"storage" json:"storage"` // 存储设置
|
||||
APIToken APITokenConfig `yaml:"api_token" json:"api_token"` // API Token 设置
|
||||
Database DatabaseConfig `yaml:"database" json:"database"` // 数据库设置
|
||||
Web WebConfig `yaml:"web" json:"web"` // Web 前端设置
|
||||
}
|
||||
|
||||
type WebConfig struct {
|
||||
Path string `yaml:"path" json:"path"` // Web 前端资源路径
|
||||
}
|
||||
|
||||
type SiteConfig struct {
|
||||
@@ -78,6 +84,26 @@ func LoadConfig(path string) error {
|
||||
configLock.Lock()
|
||||
defer configLock.Unlock()
|
||||
|
||||
// 检查文件是否存在
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
// 创建默认配置
|
||||
cfg := GetDefaultConfig()
|
||||
data, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 确保目录存在
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
GlobalConfig = cfg
|
||||
ConfigPath = path
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -93,6 +119,47 @@ func LoadConfig(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDefaultConfig() *Config {
|
||||
return &Config{
|
||||
Site: SiteConfig{
|
||||
Name: "文件暂存柜",
|
||||
Description: "临时文件中转服务",
|
||||
Logo: "https://www.hxuanyu.com/upload/favicon.png",
|
||||
},
|
||||
Security: SecurityConfig{
|
||||
AdminPasswordHash: "$2a$10$Bm0TEmU4uj.bVHYiIPFBheUkcdg6XHpsanLvmpoAtgU1UnKbo9.vy", // 默认密码: admin123
|
||||
PickupCodeLength: 6,
|
||||
PickupFailLimit: 5,
|
||||
JWTSecret: "file-relay-secret",
|
||||
},
|
||||
Upload: UploadConfig{
|
||||
MaxFileSizeMB: 100,
|
||||
MaxBatchFiles: 20,
|
||||
MaxRetentionDays: 30,
|
||||
RequireToken: false,
|
||||
},
|
||||
Storage: StorageConfig{
|
||||
Type: "local",
|
||||
Local: struct {
|
||||
Path string `yaml:"path" json:"path"`
|
||||
}{
|
||||
Path: "storage_data",
|
||||
},
|
||||
},
|
||||
APIToken: APITokenConfig{
|
||||
Enabled: true,
|
||||
AllowAdminAPI: true,
|
||||
MaxTokens: 20,
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
Path: "file_relay.db",
|
||||
},
|
||||
Web: WebConfig{
|
||||
Path: "web",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func SaveConfig() error {
|
||||
configLock.RLock()
|
||||
defer configLock.RUnlock()
|
||||
|
||||
Reference in New Issue
Block a user