-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (91 loc) · 2.87 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/FianGumilar/email-otp-verification/app"
"github.com/FianGumilar/email-otp-verification/config"
"github.com/FianGumilar/email-otp-verification/helpers"
"github.com/FianGumilar/email-otp-verification/repositories"
"github.com/FianGumilar/email-otp-verification/routes"
"github.com/go-playground/locales/id"
ut "github.com/go-playground/universal-translator"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"gopkg.in/go-playground/validator.v9"
id_translations "gopkg.in/go-playground/validator.v9/translations/id"
)
// CustomValidator adalah
type CustomValidator struct {
validator *validator.Validate
translator ut.Translator
}
// Passing Variable
var (
uni *ut.UniversalTranslator
echoHandler echo.Echo
)
var ctx = context.Background()
// Custom Validator and translation
func (cv *CustomValidator) Validate(i interface{}) error {
err := cv.validator.Struct(i)
if err != nil {
errs := err.(validator.ValidationErrors)
for _, row := range errs {
return errors.New(row.Translate(cv.translator))
}
}
return cv.validator.Struct(i)
}
func main() {
if err := config.OpenConnection(); err != nil {
panic(fmt.Sprintf("Open Connection Faild: %s", err.Error()))
}
defer config.CloseConnectionDB()
// Mongo DB connection using database default
mongoDB := config.ConnectMongo(ctx)
// Connection database
DB := config.DBConnection()
// Configuration Repository
repo := repositories.NewRepository(DB, mongoDB, ctx)
// Configuration Repository and Services
services := app.SetupApp(DB, repo)
// Routing API
routes.RoutesApi(echoHandler, services)
port := fmt.Sprintf(":%s", config.GetEnv("APP_PORT", "8080"))
echoHandler.Logger.Fatal(echoHandler.Start(port))
}
func init() {
boardingService()
e := echo.New()
echoHandler = *e
validateCustom := validator.New()
id := id.New()
uni = ut.New(id, id)
trans, _ := uni.GetTranslator("id")
id_translations.RegisterDefaultTranslations(validateCustom, trans)
e.Validator = &CustomValidator{validator: validateCustom, translator: trans}
e.Static("/img/*", "assets/img")
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Secure())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowCredentials: true,
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
}))
e.HTTPErrorHandler = func(err error, c echo.Context) {
report, ok := err.(*echo.HTTPError)
if !ok {
report = echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
result := helpers.ResponseJSON(false, strconv.Itoa(report.Code), err.Error(), nil)
c.Logger().Error(report)
c.JSON(report.Code, result)
}
}
func boardingService() {
fmt.Println(`Running Services...........................................................`)
}