-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (53 loc) · 1.51 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
package main
import (
"errors"
"os"
"github.com/rs/zerolog/log"
"github.com/andrei-dascalu/shortener/src/api"
"github.com/andrei-dascalu/shortener/src/repository/mongodb"
"github.com/andrei-dascalu/shortener/src/repository/redis"
"github.com/andrei-dascalu/shortener/src/shortener"
"github.com/ansrivas/fiberprometheus/v2"
"github.com/gofiber/fiber/v2"
)
func main() {
repo, err := chooseRepo()
if err != nil {
log.Panic().Err(err).Msg("error")
}
service := shortener.NewRedirectService(repo)
if service == nil {
log.Panic().Err(err).Msg("error")
}
app := fiber.New()
prometheus := fiberprometheus.New("my-service-name")
prometheus.RegisterAt(app, "/metrics")
app.Use(prometheus.Middleware)
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"message": "Hello World!",
})
})
app.Post("/url", api.GetSaveHandler(api.NewHandler(service)))
app.Get("/:code", api.GetRedirectHandler(api.NewHandler(service)))
app.Listen(":8080")
}
func chooseRepo() (shortener.LinkRepository, error) {
repoType := os.Getenv("DB_TYPE")
log.Warn().Str("repoType", repoType).Msg("Selected repo type")
switch repoType {
case "redis":
redisURL := os.Getenv("REDIS_URL")
repo, _ := redis.NewRedisRepository(redisURL)
return repo, nil
case "mongo":
mongoURL := os.Getenv("MONGO_URL")
mongoDb := os.Getenv("MONGO_DB")
repo, err := mongodb.NewMongoRepository(mongoURL, mongoDb)
if err != nil {
return nil, err
}
return repo, nil
}
return nil, errors.New("missing configuration")
}