-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
60 lines (52 loc) · 1.27 KB
/
server.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
package main
import (
"context"
"time"
"github.com/jatindotdev/tinybits/api/db"
"github.com/jatindotdev/tinybits/api/handlers"
"github.com/jatindotdev/tinybits/api/lib"
"github.com/jatindotdev/tinybits/api/middlewares"
"github.com/jatindotdev/tinybits/api/routes"
"github.com/jatindotdev/tinybits/api/storage"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
"github.com/redis/go-redis/v9"
"go.uber.org/fx"
)
func createServer(linkHandler *handlers.LinkHandler, db *sqlx.DB, redis *redis.Client) *echo.Echo {
app := echo.New()
app.Validator = lib.NewValidator()
middlewares.SetupMiddlewares(app)
routes.SetupHelperRoutes(app, db, redis)
api := app.Group("/api")
routes.SetupRoutes(api, linkHandler)
return app
}
func initServer(lifecycle fx.Lifecycle, app *echo.Echo) {
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
routes.StartTime = time.Now()
address := lib.GetServerAddress()
go app.Start(address)
return nil
},
OnStop: func(ctx context.Context) error {
return app.Shutdown(ctx)
},
})
}
func main() {
fx.New(
fx.Provide(
handlers.NewLinkHandler,
storage.NewLinkStorage,
db.CreatePostgresConnection,
db.CreateRedisClient,
createServer,
),
fx.Invoke(
lib.LoadEnv,
initServer,
),
).Run()
}