package main import ( "api/config" "api/database" "api/globals" "api/models" "api/routes" "api/services" "io" "log" "os" "runtime" "strconv" "strings" "time" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/logger" gormlogger "gorm.io/gorm/logger" ) func main() { // Configures timezone time.Local, _ = time.LoadLocation("America/Sao_Paulo") // Configures log file logFile, err := os.OpenFile("api.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) if err != nil { panic(err) } mw := io.MultiWriter(os.Stdout, logFile) log.SetOutput(mw) gormLogger := gormlogger.New( log.New(logFile, "\r\n", log.LstdFlags), gormlogger.Config{ SlowThreshold: time.Second, LogLevel: gormlogger.Info, IgnoreRecordNotFoundError: true, Colorful: false, }, ) log.Println("==================================================================") // Get release type from command line args := os.Args[1:] var releaseType string if len(args) == 0 { releaseType = "prod" } else { releaseType = args[0] } // Load configurations for the release type if !config.LoadConfigurations(releaseType) { log.Printf("Invalid release type: %s\n", args[0]) return } globals.API_VERSION = config.Conf.Version globals.API_RELEASE = strings.ToUpper(config.Configurations.ReleaseType) // Starts process log.Println("Starting API", globals.API_VERSION+" ("+globals.API_RELEASE+")") log.Println("OS:", runtime.GOOS) // Configures Fiber app := fiber.New(fiber.Config{ StrictRouting: false, DisableStartupMessage: true, }) app.Use(logger.New(logger.Config{ Output: logFile, // Use the Lumberjack logger instance Format: "${time} ${ip} - ${status} - ${method} ${path}\n", // Specify log format TimeFormat: "2006/01/02 15:04:05", // Specify the date and time format })) // CORS app.Use(cors.New(cors.Config{ AllowHeaders: "Origin,Content-Type,Accept,Content-Length,Accept-Language,Accept-Encoding,Connection,Access-Control-Allow-Origin,X-API-KEY,Authorization", AllowOrigins: "*", AllowCredentials: false, AllowMethods: "GET,POST,HEAD,PUT,DELETE,PATCH,OPTIONS", })) // Connects to database if err = database.ConnectDB(gormLogger); err != nil { panic("Could not connect to database") } // // WebSocket configuration // app.Use("/ws", func(c *fiber.Ctx) error { // if websocket.IsWebSocketUpgrade(c) { // c.Locals("allowed", true) // return c.Next() // } // return fiber.ErrUpgradeRequired // }) // Setup routes routes.Setup(app) // Starts transmissions processing globals.Transmissions = make(map[string]*models.CurrentTransmission, 100) log.Println("Starting transmissions limits verification process") ticker := time.NewTicker(1 * time.Minute) go func() { for range ticker.C { services.VerifyTransmissionsLimits() } }() port := strconv.Itoa(config.Configurations.Data.API_PORT) log.Println("Server started in port " + port) if err = app.Listen(":" + port); err != nil { panic(err) } }