-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (59 loc) · 1.65 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
76
77
78
79
80
81
82
package main
import (
"context"
"log"
"os"
"blanq_invoice/database"
"blanq_invoice/internal/handlers"
"blanq_invoice/internal/repos"
"blanq_invoice/middlewares"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
"github.com/joho/godotenv"
)
// @title Blanq Invoice API
// @version 1.0
// @host localhost:8080
// @BasePath /
func main() {
err := godotenv.Load()
if err != nil {
log.Println("Error Loading .env",err)
}
port := os.Getenv("PORT")
dbAdress := os.Getenv("DBURL")
x := context.Background()
conn, err := pgx.Connect(x, dbAdress)
if err != nil {
panic(err)
}
defer conn.Close(x)
err = conn.Ping(x)
if err != nil {
log.Println("Error pinging database",err)
panic(err)
}
app := fiber.New()
db := database.New(conn)
config := repos.NewApiRepos(repos.ApiReposParams{
ClientRepo: repos.NewClientRepo(db),
BusinessRepo: repos.NewBusinessRepo(db),
AuthRepo: repos.NewAuthRepo(db),
InvoiceRepo: repos.NewInvoiceRepo(db),
UserRepo: repos.NewUserRepo(db),
})
app.Get("/health", func(ctx *fiber.Ctx) error {
return ctx.JSON(map[string]string{"status": "ok"})
})
middlewares.NewUserMustHaveBusinessMiddleware(config)
app.Use(middlewares.ErrorMessageMiddleware)
handlers.NewAuthHandler(config).RegisterHandlers(app)
handlers.NewBusinessHandler(config).RegisterHandlers(app)
handlers.NewClientHandler(config).RegisterHandlers(app)
handlers.NewInvoiceHandler(config).RegisterHandlers(app)
handlers.NewDashboardHandler(config).RegisterHandlers(app)
handlers.NewUserHandler(config).RegisterHandlers(app)
if err := app.Listen(":" + port); err != nil {
log.Fatal("Server failed to start",err)
}
}