-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
62 lines (46 loc) · 1.67 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
package main
import (
"context"
"ehgm.com.br/url-shortener/adapters/api"
"ehgm.com.br/url-shortener/adapters/idgenerator"
"ehgm.com.br/url-shortener/adapters/pubsub"
"ehgm.com.br/url-shortener/adapters/repository"
"ehgm.com.br/url-shortener/config"
"ehgm.com.br/url-shortener/domain/ports"
"ehgm.com.br/url-shortener/domain/usecases"
"github.com/gin-gonic/gin"
)
var log ports.Logger
func init() {
log = config.NewLogger()
log.Info("URL Shortener current version: 1.0.0")
}
func main() {
log.Info("Loading dependencies ...")
ctx := context.Background()
env := config.NewEnvConfig(log)
rdb := config.NewRedisClient(env.RedisHost, env.RedisPass)
ps := config.NewPubSubClient(ctx, log, env.ProjectId)
fdb := config.NewFirestoreClient(ctx, log, env.ProjectId)
idGenerator := idgenerator.NewIdGenerator(env.IdLength)
urlCounter := pubsub.NewUrlCounter(log, ps, env.PubsubTopic)
urlRepository := repository.NewUrlRepository(log, fdb, rdb, env.RedisTTL)
urlService := usecases.NewUrlService(log, idGenerator, urlRepository, urlCounter)
controller := api.NewUrlController(log, urlService)
log.Info("Starting Gin server ...")
router := gin.Default()
router.Use(api.ErrorHandlerMiddleware(log))
docGroup := router.Group("/doc")
docGroup.Static("/", "./doc")
docStatic := router.Group("/static")
docStatic.Static("/", "./static")
redirectGroup := router.Group("/r")
redirectGroup.GET("/:id", controller.RedirectToUrl)
urlsGroup := router.Group("/urls")
urlsGroup.POST("/", controller.PostUrl)
urlsGroup.GET("/:id", controller.GetUrl)
urlsGroup.PATCH("/:id", controller.PatchUrl)
statsGroup := router.Group("/stats")
statsGroup.GET("/", controller.GetStats)
router.Run()
}