Initial commit
This commit is contained in:
64
internal/route/api.go
Normal file
64
internal/route/api.go
Normal file
@ -0,0 +1,64 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/query"
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/storm"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/cqrs"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
func browseAPIV1SMS(w http.ResponseWriter, r *http.Request) {
|
||||
ctn := container.Must(r.Context())
|
||||
bus := cqrs.Must(ctn)
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
getInbox, err := createOutboxQueryFromRequest(r)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "bad request", logger.E(err))
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
result, err := bus.Query(ctx, getInbox)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "could not retrieve inbox"))
|
||||
}
|
||||
|
||||
inboxData, ok := result.Data().(*query.OutboxData)
|
||||
if !ok {
|
||||
panic(errors.New("unexpected data"))
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, inboxData)
|
||||
}
|
||||
|
||||
func serveAPIV1SMS(w http.ResponseWriter, r *http.Request) {
|
||||
smsID, err := getSMSID(r)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
email, err := openSMS(ctx, smsID)
|
||||
if err != nil {
|
||||
if errors.Is(err, storm.ErrNotFound) {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
panic(errors.Wrap(err, "could not open sms"))
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, email)
|
||||
}
|
101
internal/route/helper.go
Normal file
101
internal/route/helper.go
Normal file
@ -0,0 +1,101 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/query"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
"gitlab.com/wpetit/goweb/service/template"
|
||||
)
|
||||
|
||||
func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Data) template.Data {
|
||||
ctn := container.Must(r.Context())
|
||||
data, err := template.Extend(data,
|
||||
template.WithBuildInfo(w, r, ctn),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "could not extend template data"))
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func createOutboxQueryFromRequest(r *http.Request) (*query.GetOutboxRequest, error) {
|
||||
orderBy := r.URL.Query().Get("orderBy")
|
||||
reverse := r.URL.Query().Get("reverse")
|
||||
recipient := r.URL.Query().Get("recipient")
|
||||
body := r.URL.Query().Get("body")
|
||||
|
||||
var err error
|
||||
|
||||
var limit int64 = 0
|
||||
|
||||
rawLimit := r.URL.Query().Get("limit")
|
||||
if rawLimit != "" {
|
||||
limit, err = strconv.ParseInt(rawLimit, 10, 32)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
|
||||
var skip int64 = 0
|
||||
|
||||
rawSkip := r.URL.Query().Get("skip")
|
||||
if rawSkip != "" {
|
||||
skip, err = strconv.ParseInt(rawSkip, 10, 32)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
|
||||
var after time.Time
|
||||
|
||||
rawAfter := r.URL.Query().Get("after")
|
||||
if rawAfter != "" {
|
||||
after, err = time.Parse(time.RFC3339, rawAfter)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
|
||||
var before time.Time
|
||||
|
||||
rawBefore := r.URL.Query().Get("before")
|
||||
if rawBefore != "" {
|
||||
before, err = time.Parse(time.RFC3339, rawBefore)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
|
||||
search := &query.OutboxSearch{}
|
||||
if recipient != "" {
|
||||
search.Recipient = recipient
|
||||
}
|
||||
|
||||
if body != "" {
|
||||
search.Body = body
|
||||
}
|
||||
|
||||
if rawAfter != "" {
|
||||
search.After = after
|
||||
}
|
||||
|
||||
if rawBefore != "" {
|
||||
search.Before = before
|
||||
}
|
||||
|
||||
inboxRequest := &query.GetOutboxRequest{
|
||||
OrderBy: orderBy,
|
||||
Reverse: reverse == "y",
|
||||
Skip: int(skip),
|
||||
Limit: int(limit),
|
||||
Search: search,
|
||||
}
|
||||
|
||||
return inboxRequest, nil
|
||||
}
|
29
internal/route/mount.go
Normal file
29
internal/route/mount.go
Normal file
@ -0,0 +1,29 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/config"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"gitlab.com/wpetit/goweb/static"
|
||||
)
|
||||
|
||||
func Mount(r *chi.Mux, config *config.Config) error {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Get("/", serveOutboxPage)
|
||||
r.Delete("/sms", handleClearOutbox)
|
||||
r.Get("/sms/{id}", serveSMSPage)
|
||||
r.Delete("/sms/{id}", handleSMSDelete)
|
||||
})
|
||||
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Route("/v1", func(r chi.Router) {
|
||||
r.Get("/sms", browseAPIV1SMS)
|
||||
r.Get("/sms/{id}", serveAPIV1SMS)
|
||||
})
|
||||
})
|
||||
|
||||
notFoundHandler := r.NotFoundHandler()
|
||||
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler))
|
||||
|
||||
return nil
|
||||
}
|
62
internal/route/outbox.go
Normal file
62
internal/route/outbox.go
Normal file
@ -0,0 +1,62 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/command"
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/query"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/cqrs"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
"gitlab.com/wpetit/goweb/service/template"
|
||||
)
|
||||
|
||||
func serveOutboxPage(w http.ResponseWriter, r *http.Request) {
|
||||
ctn := container.Must(r.Context())
|
||||
tmpl := template.Must(ctn)
|
||||
bus := cqrs.Must(ctn)
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
getOutbox, err := createOutboxQueryFromRequest(r)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "bad request", logger.E(err))
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
result, err := bus.Query(ctx, getOutbox)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "could not retrieve outbox"))
|
||||
}
|
||||
|
||||
inboxData, ok := result.Data().(*query.OutboxData)
|
||||
if !ok {
|
||||
panic(errors.New("unexpected data"))
|
||||
}
|
||||
|
||||
data := extendTemplateData(w, r, template.Data{
|
||||
"Messages": inboxData.Messages,
|
||||
})
|
||||
|
||||
if err := tmpl.RenderPage(w, "outbox.html.tmpl", data); err != nil {
|
||||
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
|
||||
}
|
||||
}
|
||||
|
||||
func handleClearOutbox(w http.ResponseWriter, r *http.Request) {
|
||||
ctn := container.Must(r.Context())
|
||||
|
||||
bus := cqrs.Must(ctn)
|
||||
|
||||
clearInbox := &command.ClearOutboxRequest{}
|
||||
ctx := r.Context()
|
||||
|
||||
if _, err := bus.Exec(ctx, clearInbox); err != nil {
|
||||
panic(errors.Wrap(err, "could not clear outbox"))
|
||||
}
|
||||
|
||||
http.Error(w, http.StatusText(http.StatusNoContent), http.StatusNoContent)
|
||||
}
|
115
internal/route/sms.go
Normal file
115
internal/route/sms.go
Normal file
@ -0,0 +1,115 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/command"
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/model"
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/query"
|
||||
"forge.cadoles.com/Cadoles/fake-sms/internal/storm"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/cqrs"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
"gitlab.com/wpetit/goweb/service/template"
|
||||
)
|
||||
|
||||
func serveSMSPage(w http.ResponseWriter, r *http.Request) {
|
||||
smsID, err := getSMSID(r)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
sms, err := openSMS(ctx, smsID)
|
||||
if err != nil {
|
||||
if errors.Is(err, storm.ErrNotFound) {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
panic(errors.Wrap(err, "could not open sms"))
|
||||
}
|
||||
|
||||
ctn := container.Must(ctx)
|
||||
tmpl := template.Must(ctn)
|
||||
|
||||
data := extendTemplateData(w, r, template.Data{
|
||||
"SMS": sms,
|
||||
})
|
||||
|
||||
if err := tmpl.RenderPage(w, "sms.html.tmpl", data); err != nil {
|
||||
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
|
||||
}
|
||||
}
|
||||
|
||||
func handleSMSDelete(w http.ResponseWriter, r *http.Request) {
|
||||
smsID, err := getSMSID(r)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
bus := cqrs.Must(ctn)
|
||||
|
||||
deleteSMS := &command.DeleteSMSRequest{
|
||||
SMSID: smsID,
|
||||
}
|
||||
|
||||
if _, err := bus.Exec(ctx, deleteSMS); err != nil {
|
||||
panic(errors.Wrap(err, "could not delete email"))
|
||||
}
|
||||
|
||||
http.Error(w, http.StatusText(http.StatusNoContent), http.StatusNoContent)
|
||||
}
|
||||
|
||||
func getSMSID(r *http.Request) (int, error) {
|
||||
rawSMSID := chi.URLParam(r, "id")
|
||||
|
||||
smsID, err := strconv.ParseInt(rawSMSID, 10, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int(smsID), nil
|
||||
}
|
||||
|
||||
func openSMS(ctx context.Context, emailID int) (*model.SMS, error) {
|
||||
ctn := container.Must(ctx)
|
||||
bus := cqrs.Must(ctn)
|
||||
req := &query.OpenSMSRequest{
|
||||
SMSID: emailID,
|
||||
}
|
||||
|
||||
result, err := bus.Query(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
openEmailData, ok := result.Data().(*query.OpenSMSData)
|
||||
if !ok {
|
||||
return nil, errors.New("unexpected result data")
|
||||
}
|
||||
|
||||
return openEmailData.SMS, nil
|
||||
}
|
||||
|
||||
func getAttachmentIndex(r *http.Request) (int, error) {
|
||||
rawAttachmendIndex := chi.URLParam(r, "attachmendIndex")
|
||||
|
||||
attachmendIndex, err := strconv.ParseInt(rawAttachmendIndex, 10, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int(attachmendIndex), nil
|
||||
}
|
Reference in New Issue
Block a user