/
http.go
178 lines (164 loc) · 5.89 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package channelmanager
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/maksimru/event-scheduler/channel"
pubsublistenerconfig "github.com/maksimru/event-scheduler/listener/pubsub/config"
pubsubpublisherconfig "github.com/maksimru/event-scheduler/publisher/pubsub/config"
"github.com/maksimru/event-scheduler/storage"
"net/http"
)
type ChannelManagerServer interface {
ListChannels(ctx echo.Context) error
AddChannel(ctx echo.Context) error
DeleteChannel(ctx echo.Context) error
UpdateChannel(ctx echo.Context) error
}
type SchedulerChannelManagerServer struct {
manager ChannelManager
httpServer *echo.Echo
}
func (m *SchedulerChannelManagerServer) BootChannelManagerServer(manager ChannelManager, httpServer *echo.Echo) error {
m.manager = manager
m.httpServer = httpServer
m.initHttpRoutes()
return nil
}
func (m *SchedulerChannelManagerServer) initHttpRoutes() {
m.httpServer.GET("/channels", m.ListChannels)
m.httpServer.DELETE("/channels/:id", m.DeleteChannel)
m.httpServer.POST("/channels", m.AddChannel)
m.httpServer.PATCH("/channels/:id", m.UpdateChannel)
}
type ChannelInput struct {
Source SourceInput `json:"source" form:"source" query:"source" validate:"required,dive"`
Destination TargetInput `json:"destination" form:"destination" query:"destination" validate:"required,dive"`
}
type SourceInput struct {
Driver string `json:"driver" form:"driver" query:"driver" validate:"required,oneof=pubsub"`
PubsubSourceConfig pubsublistenerconfig.SourceConfig `json:"config" form:"config" query:"config" validate:"required_if=Driver pubsub"`
}
type TargetInput struct {
Driver string `json:"driver" form:"driver" query:"driver" validate:"required,oneof=pubsub"`
PubsubTargetConfig pubsubpublisherconfig.DestinationConfig `json:"config" form:"config" query:"config" validate:"required_if=Driver pubsub"`
}
func (m *SchedulerChannelManagerServer) AddChannel(ctx echo.Context) error {
c := new(ChannelInput)
if err := ctx.Bind(&c); err != nil {
return ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error binding: %s", err.Error()),
})
}
if err := ctx.Validate(c); err != nil {
return echo.NewHTTPError(http.StatusNotAcceptable, err.Error())
}
channelID, err := m.manager.AddChannel(channel.Channel{
Source: channel.Source{
Driver: c.Source.Driver,
Config: pubsublistenerconfig.SourceConfig{
ProjectID: c.Source.PubsubSourceConfig.ProjectID,
SubscriptionID: c.Source.PubsubSourceConfig.SubscriptionID,
KeyFile: c.Source.PubsubSourceConfig.KeyFile,
},
},
Destination: channel.Destination{
Driver: c.Destination.Driver,
Config: pubsubpublisherconfig.DestinationConfig{
ProjectID: c.Destination.PubsubTargetConfig.ProjectID,
TopicID: c.Destination.PubsubTargetConfig.TopicID,
KeyFile: c.Destination.PubsubTargetConfig.KeyFile,
},
},
})
if err != nil {
return ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error adding new channel %s: %s", ctx, err.Error()),
})
}
return ctx.JSON(http.StatusOK, map[string]interface{}{
"status": true,
"channelID": channelID,
})
}
func (m *SchedulerChannelManagerServer) UpdateChannel(ctx echo.Context) error {
channelID := ctx.Param("id")
c := new(ChannelInput)
if err := ctx.Bind(&c); err != nil {
return ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error binding: %s", err.Error()),
})
}
if err := ctx.Validate(c); err != nil {
return echo.NewHTTPError(http.StatusNotAcceptable, err.Error())
}
_, err := m.manager.UpdateChannel(channelID, channel.Channel{
Source: channel.Source{
Driver: c.Source.Driver,
Config: pubsublistenerconfig.SourceConfig{
ProjectID: c.Source.PubsubSourceConfig.ProjectID,
SubscriptionID: c.Source.PubsubSourceConfig.SubscriptionID,
KeyFile: c.Source.PubsubSourceConfig.KeyFile,
},
},
Destination: channel.Destination{
Driver: c.Destination.Driver,
Config: pubsubpublisherconfig.DestinationConfig{
ProjectID: c.Destination.PubsubTargetConfig.ProjectID,
TopicID: c.Destination.PubsubTargetConfig.TopicID,
KeyFile: c.Destination.PubsubTargetConfig.KeyFile,
},
},
})
if err == storage.ErrChannelNotFound {
return ctx.JSON(http.StatusNotFound, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error updating channel %s: %s", ctx, err.Error()),
})
}
if err != nil {
return ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error updating channel %s: %s", ctx, err.Error()),
})
}
return ctx.JSON(http.StatusOK, map[string]interface{}{
"status": true,
"channelID": channelID,
})
}
func (m *SchedulerChannelManagerServer) DeleteChannel(ctx echo.Context) error {
channelID := ctx.Param("id")
err := m.manager.DeleteChannel(channelID)
if err == storage.ErrChannelNotFound {
return ctx.JSON(http.StatusNotFound, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error removing channel %s: %s", ctx, err.Error()),
})
}
if err != nil {
return ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error removing channel %s: %s", ctx, err.Error()),
})
}
return ctx.JSON(http.StatusOK, map[string]interface{}{
"status": true,
"channelID": channelID,
})
}
func (m *SchedulerChannelManagerServer) ListChannels(ctx echo.Context) error {
channels, err := m.manager.GetChannels()
if err != nil {
return ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
"status": false,
"error": fmt.Sprintf("error removing channel %s: %s", ctx, err.Error()),
})
}
return ctx.JSON(http.StatusOK, map[string]interface{}{
"status": true,
"data": channels,
})
}