teldrive/main.go

65 lines
1.4 KiB
Go
Raw Normal View History

2023-08-07 03:32:46 +08:00
package main
import (
"fmt"
2023-09-10 20:17:08 +08:00
"mime"
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/database"
"github.com/divyam234/teldrive/routes"
2023-09-08 18:37:11 +08:00
"github.com/divyam234/teldrive/ui"
2023-08-16 05:53:02 +08:00
"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()
2023-08-16 20:48:32 +08:00
scheduler := gocron.NewScheduler(time.UTC)
2023-09-11 14:10:03 +08:00
scheduler.Every(1).Hour().Do(cron.FilesDeleteJob)
scheduler.StartAsync()
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"},
2023-09-23 20:11:11 +08:00
AllowHeaders: []string{"Authorization", "Content-Length", "Content-Type"},
2023-08-12 19:21:42 +08:00
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return true
},
MaxAge: 12 * time.Hour,
}))
2023-08-07 03:32:46 +08:00
2023-09-10 20:17:08 +08:00
mime.AddExtensionType(".js", "application/javascript")
2023-08-07 03:32:46 +08:00
router.Use(gin.ErrorLogger())
2023-09-08 18:37:11 +08:00
routes.AddRoutes(router)
ui.AddRoutes(router)
2023-08-07 03:32:46 +08:00
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-07 03:32:46 +08:00
}