2022-10-20 16:58:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"GoWeb/app"
|
|
|
|
"GoWeb/config"
|
|
|
|
"GoWeb/database"
|
|
|
|
"GoWeb/routes"
|
2023-02-05 18:46:47 +00:00
|
|
|
"embed"
|
2022-10-20 16:58:21 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-12-05 00:04:24 +00:00
|
|
|
"time"
|
2022-10-20 16:58:21 +00:00
|
|
|
)
|
|
|
|
|
2023-02-05 18:46:47 +00:00
|
|
|
//go:embed templates static
|
|
|
|
var res embed.FS
|
|
|
|
|
2022-10-20 16:58:21 +00:00
|
|
|
func main() {
|
|
|
|
// Create instance of App
|
2022-12-05 00:04:24 +00:00
|
|
|
appLoaded := app.App{}
|
2022-10-20 16:58:21 +00:00
|
|
|
|
|
|
|
// Load config file to application
|
2022-12-05 00:04:24 +00:00
|
|
|
appLoaded.Config = config.LoadConfig()
|
2022-10-20 16:58:21 +00:00
|
|
|
|
2023-02-05 18:46:47 +00:00
|
|
|
// Load templates
|
|
|
|
appLoaded.Res = &res
|
|
|
|
|
2022-11-01 21:30:54 +00:00
|
|
|
// Create logs directory if it doesn't exist
|
|
|
|
if _, err := os.Stat("logs"); os.IsNotExist(err) {
|
2022-12-05 00:04:24 +00:00
|
|
|
err := os.Mkdir("logs", 0755)
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to create log directory")
|
|
|
|
}
|
2022-11-01 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create log file and set output
|
2023-02-05 19:01:38 +00:00
|
|
|
file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
2022-12-05 00:04:24 +00:00
|
|
|
log.SetOutput(file)
|
2022-10-20 16:58:21 +00:00
|
|
|
|
|
|
|
// Connect to database
|
2022-12-05 00:04:24 +00:00
|
|
|
appLoaded.Db = database.ConnectDB(&appLoaded)
|
2022-10-20 16:58:21 +00:00
|
|
|
|
|
|
|
// Define Routes
|
2022-12-05 00:04:24 +00:00
|
|
|
routes.GetRoutes(&appLoaded)
|
|
|
|
routes.PostRoutes(&appLoaded)
|
2022-10-20 16:58:21 +00:00
|
|
|
|
|
|
|
// Start server
|
2022-12-05 00:04:24 +00:00
|
|
|
log.Println("Starting server and listening on " + appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port)
|
2023-02-05 19:01:38 +00:00
|
|
|
err = http.ListenAndServe(appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, nil)
|
2022-10-20 16:58:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|