2023-09-12 06:40:24 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-09-13 01:47:22 -04:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"io"
|
2023-09-12 06:40:24 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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-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 writeTemplate(path string, template string) error {
|
|
|
|
tPath := filepath.Join(
|
|
|
|
"templates", path,
|
|
|
|
)
|
|
|
|
if _, err := os.Stat(tPath); os.IsNotExist(err) {
|
|
|
|
file, err := os.Create(tPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func(file *os.File) {
|
2023-09-13 01:47:22 -04:00
|
|
|
_ = file.Close()
|
2023-09-12 06:40:24 -04:00
|
|
|
}(file)
|
|
|
|
_, err = file.WriteString(template)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-09-13 01:47:22 -04:00
|
|
|
if err := utils.MKDir("subs"); err != nil {
|
2023-09-12 06:40:24 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-09-13 01:47:22 -04:00
|
|
|
if err := utils.MKDir("templates"); err != nil {
|
2023-09-12 06:40:24 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err := writeTemplate(config.Default.MetaTemplate, templateMeta); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err := writeTemplate(config.Default.ClashTemplate, templateClash); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-09-13 01:47:22 -04:00
|
|
|
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 {
|
2023-09-13 01:47:22 -04:00
|
|
|
logger.Logger.Error("Server run error", zap.Error(err))
|
2023-09-12 06:40:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|