/
api.go
58 lines (48 loc) · 1.3 KB
/
api.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
package core
import (
"errors"
"log"
echo "github.com/labstack/echo/v4"
)
type lambgoAPI struct {
router *echo.Echo
}
// Router defines all the endpoints of the app
func NewAPI(h Handler, noun string) (*lambgoAPI, error) {
if (Handler{}) == h {
return nil, errors.New("you didn't implement any handler")
}
lambgoAPI := lambgoAPI{
router: echo.New(),
}
switch {
case h.Creator != nil:
lambgoAPI.router.POST("/api/"+noun, h.Create)
case h.Reader != nil:
lambgoAPI.router.GET("/api/"+noun, h.ReadAll)
lambgoAPI.router.GET("/api/"+noun+"/:id", h.Read)
case h.Updater != nil:
lambgoAPI.router.PUT("/api/"+noun, h.Update)
case h.Deleter != nil:
lambgoAPI.router.DELETE("/api/"+noun+"/:id", h.Delete)
case h.Executor != nil:
lambgoAPI.router.POST("/api/"+noun+"/"+h.Executor.Action(), h.Execute)
}
lambgoAPI.router.GET("/health", h.BasicCheck)
return &lambgoAPI, nil
}
func (a *lambgoAPI) Check(host, port string) error {
switch {
case host == "":
return errors.New("empty host configuration")
case port == "":
return errors.New("empty port configuration")
}
return nil
}
func (a *lambgoAPI) Start(host, port string) error {
log.Print("starting lambgo service \n")
a.Check(host, port)
log.Print("sucesfully started lambgo service \n")
return a.router.Start(host + ":" + port)
}