67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"api/database"
|
|
"api/globals"
|
|
"api/routes"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
)
|
|
|
|
func main() {
|
|
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)
|
|
|
|
log.Println("Starting API", globals.API_VERSION)
|
|
log.Println("OS:", os.Getenv("OS"))
|
|
|
|
file, err := os.OpenFile("./api-homolog.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
log.Fatalf("error opening file: %v", err)
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
app := fiber.New(fiber.Config{
|
|
StrictRouting: false,
|
|
DisableStartupMessage: true,
|
|
})
|
|
|
|
app.Use(logger.New(logger.Config{
|
|
Output: file,
|
|
}))
|
|
|
|
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",
|
|
}))
|
|
|
|
if err := database.ConnectDB(); err != nil {
|
|
panic("Could not connect to database")
|
|
}
|
|
|
|
routes.Setup(app)
|
|
|
|
app.Use(func(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotFound).SendString(c.BaseURL()) // => 404 "Not Found"
|
|
})
|
|
|
|
log.Println("Server started in port " + os.Getenv("API_PORT"))
|
|
if erro := app.Listen(":" + os.Getenv("API_PORT")); err != nil {
|
|
panic(erro)
|
|
}
|
|
}
|