-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add events pubsub. * add events handler. * move http handlers to handlers package. * add scripts directory. * update Taskfile.
- Loading branch information
1 parent
830ec3a
commit 2f6263f
Showing
20 changed files
with
586 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/rugwirobaker/helmes" | ||
) | ||
|
||
// DeliveryHandler handles delivery callback reception | ||
func DeliveryHandler(events helmes.Pubsub) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
in := new(event) | ||
|
||
if err := json.NewDecoder(r.Body).Decode(in); err != nil { | ||
log.Printf("failed to serialize request") | ||
JSON(w, NewError(err.Error()), 500) | ||
return | ||
} | ||
events.Publish(r.Context(), convertEvent(in)) | ||
|
||
JSON(w, map[string]string{"status": "ok"}, http.StatusOK) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package handlers | ||
|
||
//Error ... | ||
type Error struct { | ||
Message string `json:"message"` | ||
} | ||
|
||
func (e Error) Error() string { | ||
return e.Message | ||
} | ||
|
||
// NewError creates a new error instance | ||
func NewError(text string) error { | ||
return &Error{Message: text} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/rugwirobaker/helmes" | ||
) | ||
|
||
// SubscribeHandler handles user subscriptions to delivery notifications | ||
func SubscribeHandler(events helmes.Pubsub) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
id := chi.URLParam(r, "id") | ||
|
||
h := w.Header() | ||
h.Set("Content-Type", "text/event-stream") | ||
h.Set("Cache-Control", "no-cache") | ||
h.Set("Connection", "keep-alive") | ||
h.Set("X-Accel-Buffering", "no") | ||
|
||
f, ok := w.(http.Flusher) | ||
if !ok { | ||
log.Println("could not start stream") | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithCancel(r.Context()) | ||
defer cancel() | ||
|
||
event, err := events.Subscribe(ctx, id) | ||
if err != nil { | ||
log.Println(err) | ||
Flush(w, f, NewError(err.Error())) | ||
return | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
log.Println("event: stream canceled") | ||
Flush(w, f, NewError("context canceled")) | ||
return | ||
|
||
case <-time.After(time.Second * 10): | ||
log.Println("event: stream timeout") | ||
Flush(w, f, NewError("connection timeout")) | ||
return | ||
|
||
case res := <-event: | ||
Flush(w, f, res) | ||
events.Done(ctx, res.ID) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
type event struct { | ||
MsgRef string `json:"msgRef"` | ||
Recipient string `json:"recipient"` | ||
GatewayRef string `json:"gatewayRef"` | ||
Status int `json:"status"` | ||
} | ||
|
||
func convertEvent(event *event) helmes.Event { | ||
return helmes.Event{ | ||
ID: event.MsgRef, | ||
Recipient: event.Recipient, | ||
Status: helmes.St(event.Status), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
"runtime" | ||
"time" | ||
|
||
helmes "github.com/rugwirobaker/helmes" | ||
) | ||
|
||
// HealthHandler reports the health of the application | ||
func HealthHandler() http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
res := &helmes.Health{ | ||
GitRev: helmes.Data().Version, | ||
Uptime: time.Since(startTime).Seconds(), | ||
Goroutines: runtime.NumGoroutine(), | ||
} | ||
JSON(w, res, http.StatusOK) | ||
} | ||
} |
Oops, something went wrong.