-
Notifications
You must be signed in to change notification settings - Fork 65
/
main.go
60 lines (49 loc) · 1.38 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
package main
import (
"log"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/starptech/go-web/config"
"github.com/starptech/go-web/internal/controller"
"github.com/starptech/go-web/internal/core"
"github.com/starptech/go-web/internal/models"
)
func main() {
config, err := config.NewConfig()
if err != nil {
log.Fatalf("%+v\n", err)
}
// create server
server := core.NewServer(config)
// serve files for dev
server.ServeStaticFiles()
userCtrl := &controller.User{}
userListCtrl := &controller.UserList{}
healthCtrl := &controller.Healthcheck{}
// api endpoints
g := server.Echo.Group("/api")
g.GET("/users/:id", userCtrl.GetUserJSON)
// pages
u := server.Echo.Group("/users")
u.GET("", userListCtrl.GetUsers)
u.GET("/:id", userCtrl.GetUser)
// metric / health endpoint according to RFC 5785
server.Echo.GET("/.well-known/health-check", healthCtrl.GetHealthcheck)
server.Echo.GET("/.well-known/metrics", echo.WrapHandler(promhttp.Handler()))
// migration for dev
user := models.User{Name: "Peter"}
mr := server.GetModelRegistry()
err = mr.Register(user)
if err != nil {
server.Echo.Logger.Fatal(err)
}
mr.AutoMigrateAll()
mr.Create(&user)
// Start server
go func() {
if err := server.Start(config.Address); err != nil {
server.Echo.Logger.Info("shutting down the server")
}
}()
server.GracefulShutdown()
}