1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 15:14:43 -05:00
sub2clash/main.go

80 lines
1.8 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package main
import (
_ "embed"
2023-09-13 01:47:22 -04:00
"io"
2023-09-12 12:46:17 -04:00
"strconv"
"sub2clash/api"
"sub2clash/config"
2023-09-13 01:47:22 -04:00
"sub2clash/logger"
"sub2clash/utils"
2023-09-17 03:52:37 -04:00
"sub2clash/utils/database"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
2023-09-12 06:40:24 -04:00
)
2023-09-12 12:46:17 -04:00
//go:embed templates/template_meta.yaml
2023-09-12 06:40:24 -04:00
var templateMeta string
2023-09-12 12:46:17 -04:00
//go:embed templates/template_clash.yaml
2023-09-12 06:40:24 -04:00
var templateClash string
func init() {
var err error
// 创建文件夹
err = utils.MkEssentialDir()
if err != nil {
logger.Logger.Panic("create essential dir failed", zap.Error(err))
}
// 加载配置
err = config.LoadConfig()
// 初始化日志
logger.InitLogger(config.Default.LogLevel)
if err != nil {
logger.Logger.Panic("load config failed", zap.Error(err))
2023-09-12 06:40:24 -04:00
}
// 检查更新
if config.Dev != "true" {
go func() {
update, newVersion, err := utils.CheckUpdate()
if err != nil {
logger.Logger.Warn("check update failed", zap.Error(err))
}
if update {
logger.Logger.Info("new version is available", zap.String("version", newVersion))
}
}()
} else {
logger.Logger.Info("running in dev mode")
2023-09-12 06:40:24 -04:00
}
// 写入默认模板
err = utils.WriteDefalutTemplate(templateMeta, templateClash)
if err != nil {
logger.Logger.Panic("write default template failed", zap.Error(err))
2023-09-12 06:40:24 -04:00
}
// 连接数据库
err = database.ConnectDB()
2023-09-17 03:52:37 -04:00
if err != nil {
logger.Logger.Panic("database connect failed", zap.Error(err))
2023-09-17 03:52:37 -04:00
}
logger.Logger.Info("database connect success")
2023-09-12 06:40:24 -04:00
}
func main() {
// 设置运行模式
gin.SetMode(gin.ReleaseMode)
2023-09-13 01:47:22 -04:00
// 关闭 Gin 的日志输出
gin.DefaultWriter = io.Discard
2023-09-12 06:40:24 -04:00
// 创建路由
r := gin.Default()
// 设置路由
api.SetRoute(r)
logger.Logger.Info("server is running at http://localhost:" + strconv.Itoa(config.Default.Port))
2023-09-12 12:46:17 -04:00
err := r.Run(":" + strconv.Itoa(config.Default.Port))
2023-09-12 06:40:24 -04:00
if err != nil {
logger.Logger.Error("server running failed", zap.Error(err))
2023-09-12 06:40:24 -04:00
return
}
}