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"
|
2024-09-17 01:10:13 -04:00
|
|
|
|
|
|
|
"github.com/nitezs/sub2clash/api"
|
|
|
|
"github.com/nitezs/sub2clash/common"
|
|
|
|
"github.com/nitezs/sub2clash/common/database"
|
|
|
|
"github.com/nitezs/sub2clash/config"
|
|
|
|
"github.com/nitezs/sub2clash/logger"
|
2023-11-02 14:35:30 -04:00
|
|
|
|
|
|
|
"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() {
|
2023-09-23 03:12:48 -04:00
|
|
|
var err error
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2024-04-23 02:47:53 -04:00
|
|
|
err = common.MkEssentialDir()
|
2023-09-23 03:12:48 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Logger.Panic("create essential dir failed", zap.Error(err))
|
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-23 03:12:48 -04:00
|
|
|
err = config.LoadConfig()
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-20 21:08:02 -04:00
|
|
|
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
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2024-04-23 02:47:53 -04:00
|
|
|
err = common.WriteDefalutTemplate(templateMeta, templateClash)
|
2023-09-20 21:08:02 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Logger.Panic("write default template failed", zap.Error(err))
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-20 21:08:02 -04:00
|
|
|
err = database.ConnectDB()
|
2023-09-17 03:52:37 -04:00
|
|
|
if err != nil {
|
2023-09-20 21:08:02 -04:00
|
|
|
logger.Logger.Panic("database connect failed", zap.Error(err))
|
2023-09-17 03:52:37 -04:00
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
logger.Logger.Info("database connect success")
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-12 06:40:24 -04:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-13 01:47:22 -04:00
|
|
|
gin.DefaultWriter = io.Discard
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-12 06:40:24 -04:00
|
|
|
r := gin.Default()
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-12 06:40:24 -04:00
|
|
|
api.SetRoute(r)
|
2023-09-20 21:08:02 -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-20 21:08:02 -04:00
|
|
|
logger.Logger.Error("server running failed", zap.Error(err))
|
2023-09-12 06:40:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|