Add basic JSON API with filtering
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/wpetit/fake-smtp/internal/query"
|
||||
"forge.cadoles.com/wpetit/fake-smtp/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 browseAPIV1Emails(w http.ResponseWriter, r *http.Request) {
|
||||
ctn := container.Must(r.Context())
|
||||
bus := cqrs.Must(ctn)
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
getInbox, err := createInboxQueryFromRequest(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.InboxData)
|
||||
if !ok {
|
||||
panic(errors.New("unexpected data"))
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, inboxData)
|
||||
}
|
||||
|
||||
func serveAPIV1Email(w http.ResponseWriter, r *http.Request) {
|
||||
emailID, err := getEmailID(r)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
email, err := openEmail(ctx, emailID)
|
||||
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 email"))
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, email)
|
||||
}
|
@ -2,7 +2,10 @@ package route
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/wpetit/fake-smtp/internal/query"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
"gitlab.com/wpetit/goweb/service/template"
|
||||
@ -20,3 +23,84 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func createInboxQueryFromRequest(r *http.Request) (*query.GetInboxRequest, error) {
|
||||
orderBy := r.URL.Query().Get("orderBy")
|
||||
reverse := r.URL.Query().Get("reverse")
|
||||
to := r.URL.Query().Get("to")
|
||||
from := r.URL.Query().Get("from")
|
||||
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.InboxSearch{}
|
||||
if to != "" {
|
||||
search.To = to
|
||||
}
|
||||
|
||||
if from != "" {
|
||||
search.From = from
|
||||
}
|
||||
|
||||
if body != "" {
|
||||
search.Body = body
|
||||
}
|
||||
|
||||
if rawAfter != "" {
|
||||
search.After = after
|
||||
}
|
||||
|
||||
if rawBefore != "" {
|
||||
search.Before = before
|
||||
}
|
||||
|
||||
inboxRequest := &query.GetInboxRequest{
|
||||
OrderBy: orderBy,
|
||||
Reverse: reverse == "y",
|
||||
Skip: int(skip),
|
||||
Limit: int(limit),
|
||||
Search: search,
|
||||
}
|
||||
|
||||
return inboxRequest, nil
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"forge.cadoles.com/wpetit/fake-smtp/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"
|
||||
)
|
||||
@ -14,12 +15,18 @@ import (
|
||||
func serveInboxPage(w http.ResponseWriter, r *http.Request) {
|
||||
ctn := container.Must(r.Context())
|
||||
tmpl := template.Must(ctn)
|
||||
|
||||
bus := cqrs.Must(ctn)
|
||||
|
||||
getInbox := &query.GetInboxRequest{}
|
||||
ctx := r.Context()
|
||||
|
||||
getInbox, err := createInboxQueryFromRequest(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"))
|
||||
|
@ -17,6 +17,13 @@ func Mount(r *chi.Mux, config *config.Config) error {
|
||||
r.Delete("/emails/{id}", handleEmailDelete)
|
||||
})
|
||||
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Route("/v1", func(r chi.Router) {
|
||||
r.Get("/emails", browseAPIV1Emails)
|
||||
r.Get("/emails/{id}", serveAPIV1Email)
|
||||
})
|
||||
})
|
||||
|
||||
notFoundHandler := r.NotFoundHandler()
|
||||
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler))
|
||||
|
||||
|
Reference in New Issue
Block a user