-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (53 loc) · 1.36 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
package main
import (
"devlocator/database"
"devlocator/handlers"
"devlocator/openapi"
"devlocator/repositories"
"fmt"
"os"
"regexp"
"strings"
"github.com/go-playground/validator/v10"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
)
type CustomValidator struct {
validator *validator.Validate
}
func (cv *CustomValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
func validateDateTime(fl validator.FieldLevel) bool {
// 日付の形式を正規表現でチェック
re := regexp.MustCompile(`^\d{4}(\d{2})(\d{2})$`)
// カンマで分割
dates := strings.Split(fl.Field().String(), ",")
// 分割された各日付に対してチェック
for _, date := range dates {
if !re.MatchString(date) {
return false
}
}
return true
}
func main() {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file")
}
port := os.Getenv("PORT")
e := echo.New()
e.Validator = &CustomValidator{validator: validator.New()}
if err := e.Validator.(*CustomValidator).validator.RegisterValidation("datetime", validateDateTime); err != nil {
e.Logger.Fatal(err)
}
db, err := database.DBConnectGorm()
if err != nil {
e.Logger.Fatal(err)
}
eventRepo := repositories.NewEventRepository(db)
s := handlers.Server{EventRepository: eventRepo}
openapi.RegisterHandlers(e, s)
e.Logger.Fatal(e.Start(":" + port))
}