-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (69 loc) · 2.07 KB
/
main.go
File metadata and controls
74 lines (69 loc) · 2.07 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/basjoofan/gust/config"
"github.com/basjoofan/gust/handler"
"github.com/basjoofan/gust/store"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
// Setup
e := echo.New()
e.Logger.SetHeader("${time_rfc3339_nano} ${level} ${short_file}[${line}]")
beforeStart(e.Logger, config.Init, store.Init)
e.Logger.SetLevel(config.LogLevel[config.Server.LogLevel])
// Middleware
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_rfc3339_nano} ${method} ${status} ${uri} ${latency_human} ${error}\n",
}))
e.Use(middleware.Recover())
e.HTTPErrorHandler = handler.ErrorHandler
e.Server.RegisterOnShutdown(store.Close)
// Route => handler
e.GET("/ping", func(c echo.Context) error {
return c.String(http.StatusOK, "pong")
})
// socketGroup := e.Group("/socket")
// socketGroup.Any("/jobs/:jobId", handler.Poll)
// socketGroup.Any("/jobs/:jobId/statistic", handler.PollStatistic)
// Authenticated group
authGroup := e.Group("")
suiteGroup := authGroup.Group("/suites")
suiteGroup.GET("", handler.FindListSuite)
suiteGroup.GET("/:suiteId", handler.FindOneSuite)
suiteGroup.POST("", handler.InsertOneSuite)
suiteGroup.PUT("/:suiteId", handler.UpdateOneSuite)
suiteGroup.DELETE("/:suiteId", handler.DeleteOneSuite)
// Start server and graceful shutdown
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
// Start server
go func() {
address := fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port)
if err := e.Start(address); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
<-s
ctx, cancel := context.WithTimeout(config.Context, 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}
func beforeStart(logger echo.Logger, i ...func() error) {
for _, f := range i {
err := f()
if err != nil {
logger.Error(err)
panic(err)
}
}
}