/
main.go
123 lines (104 loc) · 3.35 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"log"
"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/etag"
"github.com/gofiber/fiber/v2/middleware/favicon"
"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"
authApi "github.com/nunenuh/iquote-fiber/internal/auth/api/v1"
authInfra "github.com/nunenuh/iquote-fiber/internal/auth/infra"
"github.com/nunenuh/iquote-fiber/internal/router"
authorApi "github.com/nunenuh/iquote-fiber/internal/author/api/v1"
authorInfra "github.com/nunenuh/iquote-fiber/internal/author/infra"
categoryApi "github.com/nunenuh/iquote-fiber/internal/category/api/v1"
categoryInfra "github.com/nunenuh/iquote-fiber/internal/category/infra"
"github.com/nunenuh/iquote-fiber/internal/database"
quoteApi "github.com/nunenuh/iquote-fiber/internal/quote/api/v1"
quoteInfra "github.com/nunenuh/iquote-fiber/internal/quote/infra"
userApi "github.com/nunenuh/iquote-fiber/internal/user/api/v1"
userInfra "github.com/nunenuh/iquote-fiber/internal/user/infra"
"github.com/nunenuh/iquote-fiber/pkg/config"
"go.uber.org/fx"
)
func createApp(config config.Configuration) *fiber.App {
app := fiber.New(fiber.Config{
AppName: "IQuote Fiber Clean Arch",
ServerHeader: "Fiber",
})
authInfra.InitAuthMiddleware(config.JWTSecret)
setupMiddleware(app)
return app
}
func setupMiddleware(app *fiber.App) {
app.Use(cors.New())
app.Use(compress.New())
app.Use(etag.New())
app.Use(favicon.New())
app.Use(limiter.New(limiter.Config{
Max: 100,
LimitReached: func(c *fiber.Ctx) error {
return c.Status(fiber.StatusTooManyRequests).JSON(&fiber.Map{
"status": "fail",
"message": "You have requested too many in a single time-frame! Please wait another minute!",
})
},
}))
app.Use(logger.New())
app.Use(recover.New())
app.Use(requestid.New())
}
func startApp(lc fx.Lifecycle, app *fiber.App, config config.Configuration) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go func() {
// Prepare an endpoint for 'Not Found'.
app.All("*", func(c *fiber.Ctx) error {
errorMessage := fmt.Sprintf("Route '%s' does not exist in this API!", c.OriginalURL())
return c.Status(fiber.StatusNotFound).JSON(&fiber.Map{
"status": "fail",
"message": errorMessage,
})
})
// Listen to port 8080.
urls := fmt.Sprintf("%s:%s", config.AppHost, config.AppPort)
log.Fatal(app.Listen(urls))
}()
return nil
},
OnStop: func(ctx context.Context) error {
return app.Shutdown()
},
})
}
func main() {
fx.New(
fx.Provide(config.ProvideConfig(".")),
fx.Provide(database.ProvideDatabaseConnection()),
fx.Provide(
createApp,
authInfra.ProvideAuthRepository,
userInfra.ProvideUserRepository,
authorInfra.ProvideAuthorRepository,
categoryInfra.ProvideCategoryRepository,
quoteInfra.ProvideQuoteRepository,
authInfra.ProvideAuthService,
authApi.ProvideAuthHandler,
userApi.ProvideUserHandler,
authorApi.ProvideAuthorHandler,
categoryApi.ProvideCategoryHandler,
quoteApi.ProvideQuoteHandler,
),
fx.Invoke(
router.RegisterRoutes,
startApp,
// ... other invokable functions
),
).Run()
}