-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (56 loc) · 1.5 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
package main
import (
"os"
"time"
"github.com/Netflix/go-env"
"github.com/subosito/gotenv"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/doutorfinancas/pun-sho/api"
"github.com/doutorfinancas/pun-sho/database"
"github.com/doutorfinancas/pun-sho/entity"
"github.com/doutorfinancas/pun-sho/service"
)
const Timestamp = "timestamp"
// @title Pun Sho API
// @version 0.2
// @description Create your shortlinks with QRCodes and more!
// @BasePath /api/v1
func main() {
loggerConfig := zap.NewProductionConfig()
loggerConfig.EncoderConfig.TimeKey = Timestamp
loggerConfig.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)
log, _ := loggerConfig.Build()
cfg := &api.Config{}
handleEnv(log, cfg)
g, err := database.Connect(cfg.GetDatabaseConfig())
if err != nil {
log.Fatal("can't connect to database")
}
db := database.NewDatabase(g)
shortyRepo := entity.NewShortyRepository(db, log)
shortyAccessRepo := entity.NewShortyAccessRepository(db, log)
qrSvc := service.NewQRCodeService(cfg.QRLogo)
shortySvc := service.NewShortyService(
log,
shortyRepo,
shortyAccessRepo,
qrSvc,
cfg.HostName,
cfg.QRLogo,
cfg.PublicIDLength,
)
a := api.NewAPI(log, cfg, shortySvc, qrSvc)
a.Run()
}
func handleEnv(log *zap.Logger, cfg *api.Config) {
if _, err := os.Stat(".env"); err == nil {
err := gotenv.Load(".env")
if err != nil {
log.Fatal(err.Error())
}
}
if _, err := env.UnmarshalFromEnviron(cfg); err != nil {
log.Fatal(err.Error())
}
}