-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (53 loc) · 1.44 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
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/spf13/viper"
"log"
"os"
"tabeldatadotcom/archetype/backend-api/api/routers"
"tabeldatadotcom/archetype/backend-api/config"
"tabeldatadotcom/archetype/backend-api/pkg/repository"
)
func main() {
// read configuration
config.SetupEnvironment()
// connect with database
db, dbErr := config.SetupDatabase()
defer db.Close()
if dbErr != nil {
log.Fatal(dbErr)
}
app := fiber.New(fiber.Config{
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
ctx.Status(fiber.StatusInternalServerError)
return ctx.SendString(err.Error())
},
})
// using cors
app.Use(cors.New())
// Middleware
app.Use(recover.New(
recover.Config{
EnableStackTrace: true,
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
c.Status(fiber.StatusInternalServerError)
},
},
))
// enable logging file
logLocation := viper.GetString("LOG_FILE_LOCATION")
log.Printf("writing logfile in: %s", logLocation)
file, err := os.OpenFile(logLocation, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
// setting logger management
config.SetupLogger(app, file)
// setting routing url
employeService := repository.NewRepo(db)
routers.SetupRouters(app, employeService)
appPort := os.Getenv("APP_PORT")
log.Fatal(app.Listen(":" + appPort))
}