/
main.go
142 lines (118 loc) · 4.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"net/http"
"strings"
"time"
"github.com/radyatamaa/technical-test-aichat/internal"
"github.com/radyatamaa/technical-test-aichat/pkg/database"
beego "github.com/beego/beego/v2/server/web"
beegoContext "github.com/beego/beego/v2/server/web/context"
"github.com/beego/beego/v2/server/web/filter/cors"
"github.com/beego/i18n"
"github.com/radyatamaa/technical-test-aichat/internal/domain"
"github.com/radyatamaa/technical-test-aichat/internal/middlewares"
"github.com/radyatamaa/technical-test-aichat/pkg/response"
"github.com/radyatamaa/technical-test-aichat/pkg/zaplogger"
customerVoucherRepository "github.com/radyatamaa/technical-test-aichat/internal/customer_voucher/repository"
customerVoucherBookRepository "github.com/radyatamaa/technical-test-aichat/internal/customer_voucher_book/repository"
purchaseTransactionRepository "github.com/radyatamaa/technical-test-aichat/internal/purchase_transaction/repository"
customerHandler "github.com/radyatamaa/technical-test-aichat/internal/customer/delivery/http/v1"
customerRepository "github.com/radyatamaa/technical-test-aichat/internal/customer/repository"
customerUsecase "github.com/radyatamaa/technical-test-aichat/internal/customer/usecase"
)
// @title Api Gateway V1
// @version v1
// @contact.name radyatama
// @contact.email mohradyatama24@gmail.com
// @description api "API Gateway v1"
// @BasePath /api
// @query.collection.format multi
func main() {
err := beego.LoadAppConfig("ini", "conf/app.ini")
if err != nil {
panic(err)
}
// global execution timeout
serverTimeout := beego.AppConfig.DefaultInt64("serverTimeout", 60)
// global execution timeout
requestTimeout := beego.AppConfig.DefaultInt("executionTimeout", 5)
// web hook to slack error log
slackWebHookUrl := beego.AppConfig.DefaultString("slackWebhookUrlLog", "")
// app version
appVersion := beego.AppConfig.DefaultString("version", "1")
// log path
logPath := beego.AppConfig.DefaultString("logPath", "./logs/api.log")
// init data
initData := beego.AppConfig.DefaultString("initData", "true")
// database initialization
db := database.DB()
// language
lang := beego.AppConfig.DefaultString("lang", "en|id")
languages := strings.Split(lang, "|")
for _, value := range languages {
if err := i18n.SetMessage(value, "./conf/"+value+".ini"); err != nil {
panic("Failed to set message file for l10n")
}
}
// global execution timeout to second
timeoutContext := time.Duration(requestTimeout) * time.Second
// beego config
beego.BConfig.Log.AccessLogs = false
beego.BConfig.Log.EnableStaticLogs = false
beego.BConfig.Listen.ServerTimeOut = serverTimeout
// zap logger
zapLog := zaplogger.NewZapLogger(logPath, slackWebHookUrl)
if beego.BConfig.RunMode == "dev" {
// db auto migrate dev environment
if err := db.AutoMigrate(
&domain.Customer{},
&domain.CustomerVoucher{},
&domain.CustomerVoucherBook{},
&domain.PurchaseTransaction{},
); err != nil {
panic(err)
}
// static files swagger
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
if initData == "true" {
domain.SeederData(db)
}
if beego.BConfig.RunMode != "prod" {
// static files swagger
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
// middleware init
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowMethods: []string{http.MethodGet, http.MethodPost},
AllowAllOrigins: true,
}))
beego.InsertFilterChain("*", middlewares.RequestID())
beego.InsertFilterChain("/api/*", middlewares.BodyDumpWithConfig(middlewares.NewAccessLogMiddleware(zapLog, appVersion).Logger()))
// health check
beego.Get("/health", func(ctx *beegoContext.Context) {
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(beego.M{"status": "alive"}, beego.BConfig.RunMode != "prod", false)
})
// default error handler
beego.ErrorController(&response.ErrorController{})
// init repository
customerRepo := customerRepository.NewMysqlCustomerRepository(db, zapLog)
customerVoucherRepo := customerVoucherRepository.NewMysqlCustomerVoucherRepository(db, zapLog)
customerVoucherBookRepo := customerVoucherBookRepository.NewMysqlCCustomerVoucherBookRepository(db, zapLog)
purchaseTransactionRepo := purchaseTransactionRepository.NewPurchaseTransactionRepository(db, zapLog)
// init usecase
customerUcase := customerUsecase.NewCustomerUseCase(timeoutContext,
customerRepo,
customerVoucherRepo,
customerVoucherBookRepo,
purchaseTransactionRepo,
zapLog)
// init handler
customerHandler.NewCustomerHandler(customerUcase, zapLog)
// default error handler
beego.ErrorController(&internal.BaseController{})
beego.Run()
}