-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (66 loc) · 1.88 KB
/
main.go
File metadata and controls
79 lines (66 loc) · 1.88 KB
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
76
77
78
79
package main
import (
"context"
"errors"
"log"
"main/app/api"
. "main/app/pkg/configs"
db "main/app/pkg/db/sqlc"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/logger"
"github.com/gofiber/fiber/v3/middleware/static"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
InitConfigs()
DB_URI := Configs.String("db.uri")
if DB_URI == "" {
panic(errors.New("'db.uri' may not be empty"))
}
config, err := pgxpool.ParseConfig(DB_URI)
if err != nil {
log.Fatalf("Impossibile leggere il DSN: %v", err)
}
// config.MaxConns = int32(max(10, runtime.NumCPU() * 2))
log.Printf("Impostazione pgxpool MaxConns a: %d", config.MaxConns)
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
log.Fatalf("Impossibile creare il pool pgx: %v", err)
}
defer pool.Close()
err = pool.Ping(context.Background())
if err != nil {
log.Fatalf("Impossibile fare il ping del database via pool: %v", err)
}
log.Println("Connessione al database (via pool) riuscita!")
queries := db.New(pool)
proxyHeader := Configs.String("general.proxyHeader")
app := fiber.New(fiber.Config{
ProxyHeader: proxyHeader,
})
app.Use(logger.New())
app.Use(func(c fiber.Ctx) error {
ctx := context.WithValue(c.Context(), "db", queries)
c.SetContext(ctx)
return c.Next()
})
app.Get("/ping", func(c fiber.Ctx) error {
return c.SendString("pong")
})
api.SetRoutes(app)
if Configs.String("general.env") == "production" {
app.Get("/*", static.New("./web/dist/", static.Config{Compress: true}))
app.Get("/*", static.New("./web/dist/index.html", static.Config{Compress: true}))
} else {
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
}
PORT := Configs.String("general.port")
if PORT == "" {
PORT = ":8080"
}
if err := app.Listen(PORT); err != nil {
log.Panicf("Oops... Server is not running! Reason: %v", err)
}
}