Skip to content

Commit

Permalink
Split schemas and utils into seperate packages for use in client libr…
Browse files Browse the repository at this point in the history
…aries.
  • Loading branch information
qrpike committed Mar 25, 2019
1 parent ff216fd commit db494d5
Show file tree
Hide file tree
Showing 9 changed files with 667 additions and 628 deletions.
40 changes: 21 additions & 19 deletions errand-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
gin "github.com/gin-gonic/gin"
badger "github.com/dgraph-io/badger"
utils "github.com/polygon-io/errands-server/utils"
schemas "github.com/polygon-io/errands-server/schemas"
)


Expand All @@ -18,7 +20,7 @@ type UpdateRequest struct {
Logs []string `json:"logs"`
}
func ( s *ErrandsServer ) updateErrand( c *gin.Context ){
var updatedErrand *Errand
var updatedErrand *schemas.Errand
var updateReq UpdateRequest
if err := c.ShouldBind(&updateReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
Expand All @@ -27,7 +29,7 @@ func ( s *ErrandsServer ) updateErrand( c *gin.Context ){
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
if errand.Status != "active" {
return errors.New("Errand must be in active state to update progress")
}
Expand Down Expand Up @@ -63,7 +65,7 @@ type FailedRequest struct {
Reason string `json:"reason" binding:"required"`
}
func ( s *ErrandsServer ) failedErrand( c *gin.Context ){
var updatedErrand *Errand
var updatedErrand *schemas.Errand
var failedReq FailedRequest
if err := c.ShouldBind(&failedReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
Expand All @@ -72,15 +74,15 @@ func ( s *ErrandsServer ) failedErrand( c *gin.Context ){
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
// if errand.Status != "active" {
// return errors.New("Errand must be in active state to fail")
// }
// Update this errand attributes:
if err := errand.addToLogs("ERROR", failedReq.Reason); err != nil {
if err := errand.AddToLogs("ERROR", failedReq.Reason); err != nil {
return err
}
errand.Failed = getTimestamp()
errand.Failed = utils.GetTimestamp()
errand.Status = "failed"
errand.Progress = 0
if errand.Options.Retries > 0 {
Expand Down Expand Up @@ -112,7 +114,7 @@ type CompletedRequest struct {
Results *gin.H `json:"results"`
}
func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
var updatedErrand *Errand
var updatedErrand *schemas.Errand
var compReq CompletedRequest
if err := c.ShouldBind(&compReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
Expand All @@ -122,15 +124,15 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
return
}
shouldDelete := false
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
// if errand.Status != "active" {
// return errors.New("Errand must be in active state to complete")
// }
// Update this errand attributes:
if err := errand.addToLogs("INFO", "Completed!"); err != nil {
if err := errand.AddToLogs("INFO", "Completed!"); err != nil {
return err
}
errand.Completed = getTimestamp()
errand.Completed = utils.GetTimestamp()
errand.Status = "completed"
errand.Progress = 100
errand.Results = compReq.Results
Expand Down Expand Up @@ -163,13 +165,13 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){


func ( s *ErrandsServer ) retryErrand( c *gin.Context ){
var updatedErrand *Errand
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
var updatedErrand *schemas.Errand
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
if errand.Status == "inactive" {
return errors.New("Cannot retry errand which is in inactive state")
}
// Update this errand attributes:
if err := errand.addToLogs("INFO", "Retrying!"); err != nil {
if err := errand.AddToLogs("INFO", "Retrying!"); err != nil {
return err
}
errand.Status = "inactive"
Expand All @@ -194,20 +196,20 @@ func ( s *ErrandsServer ) retryErrand( c *gin.Context ){


func ( s *ErrandsServer ) logToErrand( c *gin.Context ){
var logReq Log
var logReq schemas.Log
if err := c.ShouldBind(&logReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error {
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
if errand.Status != "active" {
return errors.New("Errand must be in active state to log to")
}
// Update this errand attributes:
return errand.addToLogs(logReq.Severity, logReq.Message)
return errand.AddToLogs(logReq.Severity, logReq.Message)
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down Expand Up @@ -260,14 +262,14 @@ func ( s *ErrandsServer ) deleteErrandByID( id string ) error {
If no error is returned, the errand will be saved in the DB with the new
attributes.
*/
func ( s *ErrandsServer ) UpdateErrandByID( id string, fn func( *Errand ) error ) ( *Errand, error ) {
var updatedErrand *Errand
func ( s *ErrandsServer ) UpdateErrandByID( id string, fn func( *schemas.Errand ) error ) ( *schemas.Errand, error ) {
var updatedErrand *schemas.Errand
err := s.DB.Update(func(txn *badger.Txn) error {
item, err := txn.Get([]byte( id )); if err != nil {
return err
}
err = item.Value(func(v []byte) error {
errand := &Errand{}
errand := &schemas.Errand{}
err := errand.UnmarshalJSON( v ); if err != nil {
return err
}
Expand Down
36 changes: 19 additions & 17 deletions errands-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"encoding/json"
gin "github.com/gin-gonic/gin"
badger "github.com/dgraph-io/badger"
utils "github.com/polygon-io/errands-server/utils"
schemas "github.com/polygon-io/errands-server/schemas"
)


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

func ( s *ErrandsServer ) createErrand( c *gin.Context ){
log.Println("creating errand")
var item Errand
var item schemas.Errand
if err := c.ShouldBindJSON(&item); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Errand validation failed!",
"error": err.Error(),
})
return
}
item.setDefaults()
item.SetDefaults()
err := s.DB.Update(func( txn *badger.Txn ) error {
return s.saveErrand( txn, &item )
})
Expand All @@ -97,8 +99,8 @@ func ( s *ErrandsServer ) createErrand( c *gin.Context ){



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


func ( s *ErrandsServer ) getAllErrands( c *gin.Context ){
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool {
return true
})
if err != nil {
Expand All @@ -132,7 +134,7 @@ func ( s *ErrandsServer ) getAllErrands( c *gin.Context ){
func ( s *ErrandsServer ) getFilteredErrands( c *gin.Context ){
key := c.Param("key")
value := c.Param("val")
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool {
switch key {
case "status":
return ( errand.Status == value )
Expand Down Expand Up @@ -173,7 +175,7 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){
})
return
}
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool {
switch key {
case "status":
return ( errand.Status == value )
Expand All @@ -191,7 +193,7 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){
}
}else {
if updateReq.Status != "" {
_, err = s.UpdateErrandByID( errand.ID, func( e *Errand ) error {
_, err = s.UpdateErrandByID( errand.ID, func( e *schemas.Errand ) error {
e.Status = updateReq.Status
return nil
})
Expand Down Expand Up @@ -221,8 +223,8 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){


func ( s *ErrandsServer ) processErrand( c *gin.Context ){
var procErrand *Errand
errands := make([]*Errand, 0)
var procErrand *schemas.Errand
errands := make([]*schemas.Errand, 0)
hasFound := false
typeFilter := c.Param("type")
err := s.DB.Update(func(txn *badger.Txn) error {
Expand All @@ -234,7 +236,7 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){
item := it.Item()
err := item.Value(func( v []byte ) error {

errand := &Errand{}
errand := &schemas.Errand{}
err := errand.UnmarshalJSON( v ); if err != nil {
return err
}
Expand Down Expand Up @@ -264,11 +266,11 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){
})
procErrand = errands[0]
// We are processing this errand:
procErrand.Started = getTimestamp()
procErrand.Started = utils.GetTimestamp()
procErrand.Attempts += 1
procErrand.Status = "active"
procErrand.Progress = 0.0
if err := procErrand.addToLogs("INFO", "Started!"); err != nil {
if err := procErrand.AddToLogs("INFO", "Started!"); err != nil {
return err
}
err := s.saveErrand( txn, procErrand ); if err != nil {
Expand Down Expand Up @@ -306,8 +308,8 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){



func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand, error ) {
errands := make([]*Errand, 0)
func ( s *ErrandsServer ) GetErrandsBy( fn func ( *schemas.Errand ) bool ) ( []*schemas.Errand, error ) {
errands := make([]*schemas.Errand, 0)
err := s.DB.View(func( txn *badger.Txn ) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 50
Expand All @@ -316,7 +318,7 @@ func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand,
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
err := item.Value(func( v []byte ) error {
errand := &Errand{}
errand := &schemas.Errand{}
err := errand.UnmarshalJSON( v ); if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions errands-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
badger "github.com/dgraph-io/badger"
binding "github.com/gin-gonic/gin/binding"
validator "gopkg.in/go-playground/validator.v8"
schemas "github.com/polygon-io/errands-server/schemas"
)


Expand All @@ -23,8 +24,8 @@ import (

//easyjson:json
type Notification struct {
Event string `json:"event"`
Errand Errand `json:"errand,omitempty"`
Event string `json:"event"`
Errand schemas.Errand `json:"errand,omitempty"`
}


Expand Down Expand Up @@ -62,7 +63,7 @@ func NewErrandsServer( cfg *Config ) *ErrandsServer {
return obj
}

func ( s *ErrandsServer ) AddNotification( event string, errand *Errand ){
func ( s *ErrandsServer ) AddNotification( event string, errand *schemas.Errand ){
obj := &Notification{
Event: event,
Errand: *errand,
Expand Down Expand Up @@ -119,7 +120,7 @@ func ( s *ErrandsServer ) killDB(){


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

if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterStructValidation(UserStructLevelValidation, Errand{})
v.RegisterStructValidation(UserStructLevelValidation, schemas.Errand{})
}

// Singular errand Routes:
Expand Down
Loading

0 comments on commit db494d5

Please sign in to comment.