-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (64 loc) · 1.57 KB
/
main.go
File metadata and controls
76 lines (64 loc) · 1.57 KB
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
package main
import (
"fmt"
"go-dice/engine"
"go-dice/routes"
"net/http"
"os"
"github.com/go-playground/validator"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type CustomValidator struct {
validator *validator.Validate
}
func (cv *CustomValidator) Validate(i any) error {
if err := cv.validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
return nil
}
func main() {
args := os.Args
if len(args) > 1 {
for _, arg := range args[1:] {
fmt.Println(engine.RollDice(arg))
}
return
}
e := echo.New()
e.Static("/", "static")
if env := os.Getenv("ENV"); env != "production" {
e.Use(
func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().URL.Path == "/css/style.css" {
c.Response().Header().Set("Cache-Control", "no-store")
c.Response().Header().Set("Pragma", "no-cache")
c.Response().Header().Set("Expires", "0")
}
return next(c)
}
},
)
}
e.Validator = &CustomValidator{validator: validator.New()}
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `${time_rfc3339} ${method} ${uri} ${status}
`,
}))
e.Use(middleware.Recover())
routes.Routes(e)
e.HTTPErrorHandler = func(err error, c echo.Context) {
code := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
}
if code == http.StatusNotFound {
c.String(http.StatusNotFound, "404 Not Found")
return
}
e.DefaultHTTPErrorHandler(err, c)
}
e.Logger.Fatal(e.Start(":3000"))
}