/
main.go
61 lines (50 loc) · 2.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
package main
import (
"time"
crud_city_usecase "github.com/herryg91/cdd/examples/location-api/app/usecase/crud_city"
crud_province_usecase "github.com/herryg91/cdd/examples/location-api/app/usecase/crud_province"
search_usecase "github.com/herryg91/cdd/examples/location-api/app/usecase/search"
"github.com/herryg91/cdd/examples/location-api/config"
"github.com/herryg91/cdd/examples/location-api/handler"
pbCity "github.com/herryg91/cdd/examples/location-api/handler/grst/city"
pbProvince "github.com/herryg91/cdd/examples/location-api/handler/grst/province"
"github.com/herryg91/cdd/examples/location-api/repository/city_mysql"
"github.com/herryg91/cdd/examples/location-api/repository/province_mysql"
"github.com/herryg91/cdd/grst"
loggerInterceptor "github.com/herryg91/cdd/grst/interceptor/logger"
recoveryInterceptor "github.com/herryg91/cdd/grst/interceptor/recovery"
sessionInterceptor "github.com/herryg91/cdd/grst/interceptor/session"
"gorm.io/gorm/logger"
"github.com/herryg91/cdd/examples/location-api/pkg/conn/mysql"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/sirupsen/logrus"
)
func main() {
cfg := config.New()
db, err := mysql.Connect(cfg.DBHost, cfg.DBPort, cfg.DBUserName, cfg.DBPassword, cfg.DBDatabaseName,
mysql.SetPrintLog(cfg.DBLogEnable, logger.LogLevel(cfg.DBLogLevel), time.Duration(cfg.DBLogThreshold)*time.Millisecond))
if err != nil {
logrus.Panicln("Failed to Initialized mysql DB:", err)
}
provinceRepo := province_mysql.New(db)
cityRepo := city_mysql.New(db)
provinceUsecase := crud_province_usecase.New(provinceRepo)
cityUsecase := crud_city_usecase.New(cityRepo)
citySearchUsecase := search_usecase.New(cityRepo, provinceRepo)
provinceHndl := handler.NewProvinceHandler(provinceUsecase)
cityHndl := handler.NewCityHandler(cityUsecase, citySearchUsecase)
grpcRestSrv, err := grst.NewServer(cfg.GrpcPort, cfg.RestPort, true,
grst.RegisterGRPCUnaryInterceptor("session", sessionInterceptor.UnaryServerInterceptor()),
grst.RegisterGRPCUnaryInterceptor("recovery", recoveryInterceptor.UnaryServerInterceptor()),
grst.RegisterGRPCUnaryInterceptor("log", loggerInterceptor.UnaryServerInterceptor()),
grst.AddForwardHeaderToContext([]string{"country"}),
)
if err != nil {
logrus.Panicln("Failed to Initialize GRPC-REST Server:", err)
}
pbProvince.RegisterProvinceGrstServer(grpcRestSrv, provinceHndl)
pbCity.RegisterCityGrstServer(grpcRestSrv, cityHndl)
if err := <-grpcRestSrv.ListenAndServeGrst(); err != nil {
logrus.Panicln("Failed to Run Grpcrest Server:", err)
}
}