-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
87 lines (74 loc) · 1.99 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
package main
import (
"io/ioutil"
"os"
"time"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/rs/zerolog"
"net/http"
)
var log *zerolog.Logger
func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
logger := zerolog.New(output).With().Timestamp().Caller().Logger()
log = &logger
}
func main() {
start := time.Now()
e := echo.New()
e.Logger.SetOutput(ioutil.Discard)
// Middleware
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
start := time.Now()
log.Debug().
Interface("headers", req.Header).
Msg(">>> " + req.Method + " " + req.RequestURI)
if err = next(c); err != nil {
c.Error(err)
}
log.Debug().
Str("latency", time.Now().Sub(start).String()).
Int("status", res.Status).
Interface("headers", res.Header()).
Msg("<<< " + req.Method + " " + req.RequestURI)
return
}
})
e.Use(middleware.Recover())
//CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}))
e.Static("/static", "assets/api-docs")
// Server
e.GET("/api/championships/:id", GetChampionship)
e.GET("/health", Health)
elapsed := time.Now().Sub(start)
log.Debug().Msg("Championships app initialized in " + elapsed.String())
e.Logger.Fatal(e.Start(":9999"))
}
func Health(c echo.Context) error {
return c.JSON(200, &HealthData{Status: "UP"})
}
type HealthData struct {
Status string `json:"status,omitempty"`
}
func GetChampionship(c echo.Context) error {
champ := &Championship{
Name: "Uefa",
Title: "Champions League",
Country: "Europe",
}
return c.JSON(http.StatusOK, champ)
}
type Championship struct {
Name string `json:"name,omitempty"`
Title string `json:"title,omitempty"`
Country string `json:"country,omitempty"`
}