-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (54 loc) · 1.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/techswarn/backend/routes"
"github.com/techswarn/backend/database"
"github.com/techswarn/backend/utils"
"fmt"
"os"
)
// define the default port of the application
const DEFAULT_PORT = "3000"
// NewFiberApp returns fiber application
func NewFiberApp() *fiber.App {
// create a new fiber application
var app *fiber.App = fiber.New()
app.Use(cors.New())
// app.Use(cors.New(cors.Config{
// AllowOriginsFunc: func(origin string) bool {
// return os.Getenv("ENVIRONMENT") == "development"
// },
// }))
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",
}))
//Loging middleware
app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
}))
// define the routes
routes.SetupRoutes(app)
// return the application
return app
}
func main() {
// create a new fiber application
var app *fiber.App = NewFiberApp()
// Initialize default config
// connect to the database
database.InitDatabase(utils.GetValue("DB_NAME"))
// get the application port from the defined PORT variable
var PORT string = os.Getenv("PORT")
// if the PORT variable is not assigned
// use the default port
if PORT == "" {
PORT = DEFAULT_PORT
}
// start the application
app.Listen(fmt.Sprintf(":%s", PORT))
}