-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (67 loc) · 1.78 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
70
71
72
73
74
75
package main
import (
"fmt"
"goarif-api/config"
"goarif-api/database"
"goarif-api/routes"
"log"
"os"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/helmet"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/masb0ymas/go-utils/pkg"
)
func main() {
// Connect to database
if err := database.Connect(); err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer database.DB.Close()
// Create Fiber app
app := fiber.New(fiber.Config{
EnableTrustedProxyCheck: true,
ErrorHandler: func(c *fiber.Ctx, err error) error {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
},
})
// Middleware
app.Use(logger.New())
app.Use(recover.New())
app.Use(cors.New(config.Cors()))
app.Use(helmet.New())
app.Use(compress.New())
app.Use(requestid.New())
app.Use(limiter.New(limiter.Config{
Next: func(c *fiber.Ctx) bool {
return c.IP() == "127.0.0.1"
},
Max: 100,
Expiration: 1 * time.Minute,
LimitReached: func(c *fiber.Ctx) error {
return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{
"error": "Too Many Requests",
})
},
}))
// Routes
routes.Root(database.DB, app)
// Get port from environment variable or use default
port := os.Getenv("APP_PORT")
if port == "" {
port = "8000"
}
// Start server
logMessage := pkg.PrintLog("Fiber", "Server starting on port "+port+"")
fmt.Println(logMessage)
if err := app.Listen(":" + port); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}