Skip to content

Commit db494d5

Browse files
committed
Split schemas and utils into seperate packages for use in client libraries.
1 parent ff216fd commit db494d5

9 files changed

Lines changed: 667 additions & 628 deletions

File tree

errand-routes.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
gin "github.com/gin-gonic/gin"
1010
badger "github.com/dgraph-io/badger"
11+
utils "github.com/polygon-io/errands-server/utils"
12+
schemas "github.com/polygon-io/errands-server/schemas"
1113
)
1214

1315

@@ -18,7 +20,7 @@ type UpdateRequest struct {
1820
Logs []string `json:"logs"`
1921
}
2022
func ( s *ErrandsServer ) updateErrand( c *gin.Context ){
21-
var updatedErrand *Errand
23+
var updatedErrand *schemas.Errand
2224
var updateReq UpdateRequest
2325
if err := c.ShouldBind(&updateReq); err != nil {
2426
c.JSON(http.StatusBadRequest, gin.H{
@@ -27,7 +29,7 @@ func ( s *ErrandsServer ) updateErrand( c *gin.Context ){
2729
})
2830
return
2931
}
30-
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
32+
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
3133
if errand.Status != "active" {
3234
return errors.New("Errand must be in active state to update progress")
3335
}
@@ -63,7 +65,7 @@ type FailedRequest struct {
6365
Reason string `json:"reason" binding:"required"`
6466
}
6567
func ( s *ErrandsServer ) failedErrand( c *gin.Context ){
66-
var updatedErrand *Errand
68+
var updatedErrand *schemas.Errand
6769
var failedReq FailedRequest
6870
if err := c.ShouldBind(&failedReq); err != nil {
6971
c.JSON(http.StatusBadRequest, gin.H{
@@ -72,15 +74,15 @@ func ( s *ErrandsServer ) failedErrand( c *gin.Context ){
7274
})
7375
return
7476
}
75-
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
77+
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
7678
// if errand.Status != "active" {
7779
// return errors.New("Errand must be in active state to fail")
7880
// }
7981
// Update this errand attributes:
80-
if err := errand.addToLogs("ERROR", failedReq.Reason); err != nil {
82+
if err := errand.AddToLogs("ERROR", failedReq.Reason); err != nil {
8183
return err
8284
}
83-
errand.Failed = getTimestamp()
85+
errand.Failed = utils.GetTimestamp()
8486
errand.Status = "failed"
8587
errand.Progress = 0
8688
if errand.Options.Retries > 0 {
@@ -112,7 +114,7 @@ type CompletedRequest struct {
112114
Results *gin.H `json:"results"`
113115
}
114116
func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
115-
var updatedErrand *Errand
117+
var updatedErrand *schemas.Errand
116118
var compReq CompletedRequest
117119
if err := c.ShouldBind(&compReq); err != nil {
118120
c.JSON(http.StatusBadRequest, gin.H{
@@ -122,15 +124,15 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
122124
return
123125
}
124126
shouldDelete := false
125-
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
127+
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
126128
// if errand.Status != "active" {
127129
// return errors.New("Errand must be in active state to complete")
128130
// }
129131
// Update this errand attributes:
130-
if err := errand.addToLogs("INFO", "Completed!"); err != nil {
132+
if err := errand.AddToLogs("INFO", "Completed!"); err != nil {
131133
return err
132134
}
133-
errand.Completed = getTimestamp()
135+
errand.Completed = utils.GetTimestamp()
134136
errand.Status = "completed"
135137
errand.Progress = 100
136138
errand.Results = compReq.Results
@@ -163,13 +165,13 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
163165

164166

165167
func ( s *ErrandsServer ) retryErrand( c *gin.Context ){
166-
var updatedErrand *Errand
167-
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
168+
var updatedErrand *schemas.Errand
169+
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
168170
if errand.Status == "inactive" {
169171
return errors.New("Cannot retry errand which is in inactive state")
170172
}
171173
// Update this errand attributes:
172-
if err := errand.addToLogs("INFO", "Retrying!"); err != nil {
174+
if err := errand.AddToLogs("INFO", "Retrying!"); err != nil {
173175
return err
174176
}
175177
errand.Status = "inactive"
@@ -194,20 +196,20 @@ func ( s *ErrandsServer ) retryErrand( c *gin.Context ){
194196

195197

196198
func ( s *ErrandsServer ) logToErrand( c *gin.Context ){
197-
var logReq Log
199+
var logReq schemas.Log
198200
if err := c.ShouldBind(&logReq); err != nil {
199201
c.JSON(http.StatusBadRequest, gin.H{
200202
"message": "Invalid Parameters",
201203
"error": err.Error(),
202204
})
203205
return
204206
}
205-
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
207+
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
206208
if errand.Status != "active" {
207209
return errors.New("Errand must be in active state to log to")
208210
}
209211
// Update this errand attributes:
210-
return errand.addToLogs(logReq.Severity, logReq.Message)
212+
return errand.AddToLogs(logReq.Severity, logReq.Message)
211213
})
212214
if err != nil {
213215
c.JSON(http.StatusInternalServerError, gin.H{
@@ -260,14 +262,14 @@ func ( s *ErrandsServer ) deleteErrandByID( id string ) error {
260262
If no error is returned, the errand will be saved in the DB with the new
261263
attributes.
262264
*/
263-
func ( s *ErrandsServer ) UpdateErrandByID( id string, fn func( *Errand ) error ) ( *Errand, error ) {
264-
var updatedErrand *Errand
265+
func ( s *ErrandsServer ) UpdateErrandByID( id string, fn func( *schemas.Errand ) error ) ( *schemas.Errand, error ) {
266+
var updatedErrand *schemas.Errand
265267
err := s.DB.Update(func(txn *badger.Txn) error {
266268
item, err := txn.Get([]byte( id )); if err != nil {
267269
return err
268270
}
269271
err = item.Value(func(v []byte) error {
270-
errand := &Errand{}
272+
errand := &schemas.Errand{}
271273
err := errand.UnmarshalJSON( v ); if err != nil {
272274
return err
273275
}

errands-routes.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"encoding/json"
1414
gin "github.com/gin-gonic/gin"
1515
badger "github.com/dgraph-io/badger"
16+
utils "github.com/polygon-io/errands-server/utils"
17+
schemas "github.com/polygon-io/errands-server/schemas"
1618
)
1719

1820

@@ -44,7 +46,7 @@ func ( s *ErrandsServer ) errandNotifications( c *gin.Context ){
4446
case t, ok := <-client.Notifications:
4547
if ok {
4648
// If we are subscribed to this event type:
47-
if contains(client.EventSubs, t.Event) || client.EventSubs[0] == "*" {
49+
if utils.Contains(client.EventSubs, t.Event) || client.EventSubs[0] == "*" {
4850
jsonData, _ := json.Marshal( t )
4951
client.Gin.SSEvent("message", string( jsonData ))
5052
w.Flush()
@@ -69,15 +71,15 @@ func ( s *ErrandsServer ) errandNotifications( c *gin.Context ){
6971

7072
func ( s *ErrandsServer ) createErrand( c *gin.Context ){
7173
log.Println("creating errand")
72-
var item Errand
74+
var item schemas.Errand
7375
if err := c.ShouldBindJSON(&item); err != nil {
7476
c.JSON(http.StatusBadRequest, gin.H{
7577
"message": "Errand validation failed!",
7678
"error": err.Error(),
7779
})
7880
return
7981
}
80-
item.setDefaults()
82+
item.SetDefaults()
8183
err := s.DB.Update(func( txn *badger.Txn ) error {
8284
return s.saveErrand( txn, &item )
8385
})
@@ -97,8 +99,8 @@ func ( s *ErrandsServer ) createErrand( c *gin.Context ){
9799

98100

99101

100-
func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *Errand ) error {
101-
if !contains(ErrandStatuses, errand.Status) {
102+
func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *schemas.Errand ) error {
103+
if !utils.Contains(schemas.ErrandStatuses, errand.Status) {
102104
return errors.New("Invalid errand status state")
103105
}
104106
bytes, err := errand.MarshalJSON(); if err != nil {
@@ -111,7 +113,7 @@ func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *Errand ) error {
111113

112114

113115
func ( s *ErrandsServer ) getAllErrands( c *gin.Context ){
114-
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
116+
errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool {
115117
return true
116118
})
117119
if err != nil {
@@ -132,7 +134,7 @@ func ( s *ErrandsServer ) getAllErrands( c *gin.Context ){
132134
func ( s *ErrandsServer ) getFilteredErrands( c *gin.Context ){
133135
key := c.Param("key")
134136
value := c.Param("val")
135-
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
137+
errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool {
136138
switch key {
137139
case "status":
138140
return ( errand.Status == value )
@@ -173,7 +175,7 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){
173175
})
174176
return
175177
}
176-
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
178+
errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool {
177179
switch key {
178180
case "status":
179181
return ( errand.Status == value )
@@ -191,7 +193,7 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){
191193
}
192194
}else {
193195
if updateReq.Status != "" {
194-
_, err = s.UpdateErrandByID( errand.ID, func( e *Errand ) error {
196+
_, err = s.UpdateErrandByID( errand.ID, func( e *schemas.Errand ) error {
195197
e.Status = updateReq.Status
196198
return nil
197199
})
@@ -221,8 +223,8 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){
221223

222224

223225
func ( s *ErrandsServer ) processErrand( c *gin.Context ){
224-
var procErrand *Errand
225-
errands := make([]*Errand, 0)
226+
var procErrand *schemas.Errand
227+
errands := make([]*schemas.Errand, 0)
226228
hasFound := false
227229
typeFilter := c.Param("type")
228230
err := s.DB.Update(func(txn *badger.Txn) error {
@@ -234,7 +236,7 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){
234236
item := it.Item()
235237
err := item.Value(func( v []byte ) error {
236238

237-
errand := &Errand{}
239+
errand := &schemas.Errand{}
238240
err := errand.UnmarshalJSON( v ); if err != nil {
239241
return err
240242
}
@@ -264,11 +266,11 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){
264266
})
265267
procErrand = errands[0]
266268
// We are processing this errand:
267-
procErrand.Started = getTimestamp()
269+
procErrand.Started = utils.GetTimestamp()
268270
procErrand.Attempts += 1
269271
procErrand.Status = "active"
270272
procErrand.Progress = 0.0
271-
if err := procErrand.addToLogs("INFO", "Started!"); err != nil {
273+
if err := procErrand.AddToLogs("INFO", "Started!"); err != nil {
272274
return err
273275
}
274276
err := s.saveErrand( txn, procErrand ); if err != nil {
@@ -306,8 +308,8 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){
306308

307309

308310

309-
func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand, error ) {
310-
errands := make([]*Errand, 0)
311+
func ( s *ErrandsServer ) GetErrandsBy( fn func ( *schemas.Errand ) bool ) ( []*schemas.Errand, error ) {
312+
errands := make([]*schemas.Errand, 0)
311313
err := s.DB.View(func( txn *badger.Txn ) error {
312314
opts := badger.DefaultIteratorOptions
313315
opts.PrefetchSize = 50
@@ -316,7 +318,7 @@ func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand,
316318
for it.Rewind(); it.Valid(); it.Next() {
317319
item := it.Item()
318320
err := item.Value(func( v []byte ) error {
319-
errand := &Errand{}
321+
errand := &schemas.Errand{}
320322
err := errand.UnmarshalJSON( v ); if err != nil {
321323
return err
322324
}

errands-server.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
badger "github.com/dgraph-io/badger"
1414
binding "github.com/gin-gonic/gin/binding"
1515
validator "gopkg.in/go-playground/validator.v8"
16+
schemas "github.com/polygon-io/errands-server/schemas"
1617
)
1718

1819

@@ -23,8 +24,8 @@ import (
2324

2425
//easyjson:json
2526
type Notification struct {
26-
Event string `json:"event"`
27-
Errand Errand `json:"errand,omitempty"`
27+
Event string `json:"event"`
28+
Errand schemas.Errand `json:"errand,omitempty"`
2829
}
2930

3031

@@ -62,7 +63,7 @@ func NewErrandsServer( cfg *Config ) *ErrandsServer {
6263
return obj
6364
}
6465

65-
func ( s *ErrandsServer ) AddNotification( event string, errand *Errand ){
66+
func ( s *ErrandsServer ) AddNotification( event string, errand *schemas.Errand ){
6667
obj := &Notification{
6768
Event: event,
6869
Errand: *errand,
@@ -119,7 +120,7 @@ func ( s *ErrandsServer ) killDB(){
119120

120121

121122
func UserStructLevelValidation(v *validator.Validate, structLevel *validator.StructLevel) {
122-
errand := structLevel.CurrentStruct.Interface().(Errand)
123+
errand := structLevel.CurrentStruct.Interface().(schemas.Errand)
123124
if errand.Options.TTL < 5 && errand.Options.TTL != 0 {
124125
structLevel.ReportError(
125126
reflect.ValueOf(errand.Options.TTL), "ttl", "ttl", "must be positive, and more than 5",
@@ -153,7 +154,7 @@ func ( s *ErrandsServer) createAPI(){
153154
// s.API.Use(gzip.Gzip(gzip.DefaultCompression))
154155

155156
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
156-
v.RegisterStructValidation(UserStructLevelValidation, Errand{})
157+
v.RegisterStructValidation(UserStructLevelValidation, schemas.Errand{})
157158
}
158159

159160
// Singular errand Routes:

0 commit comments

Comments
 (0)