package main import ( "api/config" "api/database" "api/globals" "api/routes" "fmt" "io" "log" "os" "strconv" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/logger" ) func main() { // 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) // Get release type from command line args := os.Args[1:] if len(args) == 0 { fmt.Printf("Missing release type\n") return } // Load configurations for the release type if !config.LoadConfigurations(args[0]) { fmt.Printf("Invalid release type: %s\n", args[0]) return } // Starts process log.Println("Starting API", globals.API_VERSION) log.Println("OS:", os.Getenv("OS")) file, err := os.OpenFile("./api-endpoints.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { log.Fatalf("Error opening file: %v", err) } defer file.Close() // 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", AllowOrigins: "*", AllowCredentials: true, AllowMethods: "GET,POST,HEAD,PUT,DELETE,PATCH,OPTIONS", })) // Connects to database if err := database.ConnectDB(); err != nil { panic("Could not connect to database") } // Setup routes routes.Setup(app) port := strconv.Itoa(config.Configurations.Data.API_PORT) log.Println("Server started in port " + port) // os.Getenv("API_PORT")) if err = app.Listen(":" + port); err != nil { panic(err) } }