-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
59 lines (48 loc) · 1.35 KB
/
http.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
package http
import (
"context"
"fmt"
_ "github.com/axengine/ethevent/docs"
"github.com/axengine/ethevent/pkg/http/validator"
"github.com/axengine/ethevent/pkg/svc"
echopprof "github.com/hiko1129/echo-pprof"
"github.com/labstack/echo/v4"
echoMiddleware "github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
"sync"
)
type HttpServer struct {
svc *svc.Service
ec *echo.Echo
}
func New(svc *svc.Service) *HttpServer {
return &HttpServer{svc: svc, ec: echo.New()}
}
func (hs *HttpServer) Start(ctx context.Context, wg *sync.WaitGroup, dev bool, port int) {
defer wg.Done()
// set CORS
e := hs.ec
e.Validator = &validator.CustomValidator{}
e.Use(echoMiddleware.CORSWithConfig(echoMiddleware.DefaultCORSConfig))
if dev {
// set DOCS
e.GET("/docs/*any", echoSwagger.WrapHandler)
// pprof
echopprof.Wrap(e)
}
v1 := e.Group("/v1")
v1.GET("/task/list", hs.taskList)
v1.POST("/task/add", hs.taskAdd)
v1.POST("/task/pause", hs.taskPause)
v1.POST("/task/delete", hs.taskDelete)
v1.POST("/task/update", hs.taskUpdate)
v1.POST("/event/list", hs.eventList)
// Start the echo v4 server on port 8080
fmt.Printf("Starting server on port %d\n", port)
if err := e.Start(fmt.Sprintf(":%d", port)); err != nil {
e.Logger.Error(err)
}
}
func (hs *HttpServer) Stop(ctx context.Context) error {
return hs.ec.Shutdown(ctx)
}