teldrive/main.go

63 lines
1.3 KiB
Go
Raw Normal View History

2023-08-07 03:32:46 +08:00
package main
import (
"fmt"
2023-08-26 01:32:05 +08:00
"path/filepath"
2023-08-12 19:21:42 +08:00
"time"
2023-08-16 05:53:02 +08:00
"github.com/divyam234/teldrive/cache"
"github.com/divyam234/teldrive/database"
"github.com/divyam234/teldrive/routes"
"github.com/divyam234/teldrive/utils"
2023-08-07 03:32:46 +08:00
2023-08-12 19:21:42 +08:00
"github.com/divyam234/cors"
2023-08-16 20:48:32 +08:00
"github.com/divyam234/teldrive/utils/cron"
2023-08-07 03:32:46 +08:00
"github.com/gin-gonic/gin"
2023-08-16 20:48:32 +08:00
"github.com/go-co-op/gocron"
2023-08-07 03:32:46 +08:00
)
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
2023-08-12 19:21:42 +08:00
utils.InitConfig()
utils.InitializeLogger()
2023-08-07 03:32:46 +08:00
database.InitDB()
cache.CacheInit()
2023-08-24 02:40:40 +08:00
utils.InitBotClients()
2023-08-28 16:31:51 +08:00
2023-08-16 20:48:32 +08:00
scheduler := gocron.NewScheduler(time.UTC)
2023-08-25 20:02:35 +08:00
scheduler.Every(1).Hours().Do(cron.FilesDeleteJob)
2023-08-16 20:48:32 +08:00
2023-08-12 19:21:42 +08:00
router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return true
},
MaxAge: 12 * time.Hour,
}))
2023-08-07 03:32:46 +08:00
router.Use(gin.ErrorLogger())
routes.GetRoutes(router)
2023-08-17 23:32:40 +08:00
config := utils.GetConfig()
2023-08-26 01:32:05 +08:00
certDir := filepath.Join(config.ExecDir, "sslcerts")
ok, _ := utils.PathExists(certDir)
2023-08-17 23:32:40 +08:00
if ok && config.Https {
2023-08-26 01:32:05 +08:00
router.RunTLS(fmt.Sprintf(":%d", config.Port), filepath.Join(certDir, "cert.pem"), filepath.Join(certDir, "key.pem"))
2023-08-17 23:32:40 +08:00
} else {
router.Run(fmt.Sprintf(":%d", config.Port))
2023-08-17 23:32:40 +08:00
}
2023-08-16 20:48:32 +08:00
scheduler.StartAsync()
2023-08-07 03:32:46 +08:00
}