-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroservice_http.go
60 lines (51 loc) · 1.6 KB
/
microservice_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
60
// Create and maintain by Chaiyapong Lapliengtrakul (chaiyapong@3dsinteractive.com), All right reserved (2021 - Present)
package main
import (
"context"
"time"
"github.com/labstack/echo"
)
// GET register service endpoint for HTTP GET
func (ms *Microservice) GET(path string, h ServiceHandleFunc) {
ms.echo.GET(path, func(c echo.Context) error {
return h(NewHTTPContext(ms, c))
})
}
// POST register service endpoint for HTTP POST
func (ms *Microservice) POST(path string, h ServiceHandleFunc) {
ms.echo.POST(path, func(c echo.Context) error {
return h(NewHTTPContext(ms, c))
})
}
// PUT register service endpoint for HTTP PUT
func (ms *Microservice) PUT(path string, h ServiceHandleFunc) {
ms.echo.PUT(path, func(c echo.Context) error {
return h(NewHTTPContext(ms, c))
})
}
// PATCH register service endpoint for HTTP PATCH
func (ms *Microservice) PATCH(path string, h ServiceHandleFunc) {
ms.echo.PATCH(path, func(c echo.Context) error {
return h(NewHTTPContext(ms, c))
})
}
// DELETE register service endpoint for HTTP DELETE
func (ms *Microservice) DELETE(path string, h ServiceHandleFunc) {
ms.echo.DELETE(path, func(c echo.Context) error {
return h(NewHTTPContext(ms, c))
})
}
// startHTTP will start HTTP service, this function will block thread
func (ms *Microservice) startHTTP(exitChannel chan bool) error {
// Caller can exit by sending value to exitChannel
go func() {
<-exitChannel
ms.stopHTTP()
}()
return ms.echo.Start(":8080")
}
func (ms *Microservice) stopHTTP() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ms.echo.Shutdown(ctx)
}