-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
53 lines (43 loc) · 1.23 KB
/
server.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
package main
import (
"net/http"
"github.com/br4in3x/time-since-google-action/actions"
"github.com/br4in3x/time-since-google-action/internal/request"
"github.com/br4in3x/time-since-google-action/internal/response"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
)
type ActionFunc = func(r request.Request) (*response.Simple, error)
const (
ActionDateFrom = "DATE_FROM_TO"
ActionDateBetween = "DATE_BETWEEN"
)
type Server struct {
DateFromAction *actions.DateFromToAction
DateBetweenAction *actions.DateBetweenAction
}
func (s Server) DetermineAction(action string) (ActionFunc, error) {
switch action {
case ActionDateFrom:
return s.DateFromAction.Invoke, nil
case ActionDateBetween:
return s.DateBetweenAction.Invoke, nil
default:
return nil, errors.New("can not determine action")
}
}
func (s Server) Webhook(c echo.Context) error {
req := request.Request{}
if err := c.Bind(&req); err != nil {
return errors.New("can not unmarshall request payload")
}
act, err := s.DetermineAction(req.Handler.Name)
if err != nil {
return errors.Wrap(err, "main srv.DetermineAction error")
}
res, err := act(req)
if err != nil {
return errors.Wrap(err, "main act(req) error")
}
return c.JSON(http.StatusOK, res)
}