cup/main.go

70 lines
1.7 KiB
Go
Raw Normal View History

2024-09-12 19:15:38 -04:00
// Copyright 2023 Harran Ali <harran.m@gmail.com>. All rights reserved.
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.
package main
import (
2024-09-27 03:31:45 -04:00
"embed"
2024-09-12 19:15:38 -04:00
"log"
"os"
"path"
"git.smarteching.com/goffee/core"
"git.smarteching.com/goffee/core/env"
"git.smarteching.com/goffee/core/logger"
"git.smarteching.com/goffee/cup/config"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
)
2024-09-28 13:30:49 -04:00
//go:embed all:storage/templates
2024-09-27 03:31:45 -04:00
var resources embed.FS
2024-09-12 19:15:38 -04:00
// The main function
func main() {
app := core.New()
basePath, err := os.Getwd()
2024-10-29 07:48:11 -04:00
runMode := "dev"
if len(os.Args) > 1 {
if os.Args[1] == "prod" || os.Args[1] == "dev" {
runMode = os.Args[1]
}
}
2024-09-12 19:15:38 -04:00
if err != nil {
log.Fatal("error getting current working dir")
}
app.SetBasePath(basePath)
2024-10-29 07:48:11 -04:00
app.SetRunMode(runMode)
2024-09-12 19:15:38 -04:00
app.MakeDirs("logs", "storage", "storage/sqlite", "tls")
// Handle the reading of the .env file
if config.GetEnvFileConfig().UseDotEnvFile {
2024-10-29 07:48:11 -04:00
envfile := ".env-dev"
if runMode == "prod" {
envfile = ".env"
}
envVars, err := godotenv.Read(envfile)
2024-09-12 19:15:38 -04:00
if err != nil {
log.Fatal("Error loading .env file")
}
env.SetEnvVars(envVars)
}
// Handle the logs
app.SetLogsDriver(&logger.LogFileDriver{
FilePath: path.Join(basePath, "logs/app.log"),
})
app.SetRequestConfig(config.GetRequestConfig())
app.SetGormConfig(config.GetGormConfig())
app.SetCacheConfig(config.GetCacheConfig())
app.Bootstrap()
2024-09-27 03:31:45 -04:00
app.RegisterTemplates(resources)
2024-09-15 20:19:30 -04:00
registerGlobalHooks()
2024-09-12 19:15:38 -04:00
registerRoutes()
registerEvents()
if config.GetGormConfig().EnableGorm == true {
RunAutoMigrations()
}
app.Run(httprouter.New())
}