Add basic JSON API with filtering

This commit is contained in:
wpetit 2020-11-03 11:49:31 +01:00
parent 14770d7146
commit 92baa742c4
5 changed files with 261 additions and 4 deletions

View File

@ -2,19 +2,33 @@ package query
import ( import (
"context" "context"
"strings"
"time"
"forge.cadoles.com/wpetit/fake-smtp/internal/model" "forge.cadoles.com/wpetit/fake-smtp/internal/model"
"forge.cadoles.com/wpetit/fake-smtp/internal/storm" "forge.cadoles.com/wpetit/fake-smtp/internal/storm"
stormdb "github.com/asdine/storm/v3"
"github.com/asdine/storm/v3/q"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/cqrs" "gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/middleware/container"
) )
type InboxSearch struct {
To string
From string
Body string
Subject string
After time.Time
Before time.Time
}
type GetInboxRequest struct { type GetInboxRequest struct {
OrderBy string OrderBy string
Limit int Limit int
Skip int Skip int
Reverse bool Reverse bool
Search *InboxSearch
} }
type InboxData struct { type InboxData struct {
@ -39,7 +53,26 @@ func HandleGetInbox(ctx context.Context, qry cqrs.Query) (interface{}, error) {
emails := make([]*model.Email, 0) emails := make([]*model.Email, 0)
query := db.Select() var query stormdb.Query
if req.Search != nil {
matchers := make([]q.Matcher, 0)
if req.Search.Body != "" {
matchers = append(matchers, q.Or(
q.Re("HTML", req.Search.Body),
q.Re("Text", req.Search.Body),
))
}
if req.Search.Subject != "" {
matchers = append(matchers, q.Re("Subject", req.Search.Subject))
}
query = db.Select(matchers...)
} else {
query = db.Select()
}
if req.OrderBy != "" { if req.OrderBy != "" {
query = query.OrderBy(req.OrderBy) query = query.OrderBy(req.OrderBy)
@ -51,6 +84,14 @@ func HandleGetInbox(ctx context.Context, qry cqrs.Query) (interface{}, error) {
query = query.Reverse() query = query.Reverse()
} }
if req.Limit != 0 {
query = query.Limit(req.Limit)
}
if req.Skip != 0 {
query = query.Limit(req.Skip)
}
if err := query.Find(&emails); err != nil { if err := query.Find(&emails); err != nil {
if err == storm.ErrNotFound { if err == storm.ErrNotFound {
return &InboxData{emails}, nil return &InboxData{emails}, nil
@ -59,5 +100,59 @@ func HandleGetInbox(ctx context.Context, qry cqrs.Query) (interface{}, error) {
return nil, errors.Wrap(err, "could not retrieve emails") return nil, errors.Wrap(err, "could not retrieve emails")
} }
if req.Search == nil {
return &InboxData{emails}, nil return &InboxData{emails}, nil
} }
filtered := make([]*model.Email, 0, len(emails))
for _, eml := range emails {
match := true
if req.Search.To != "" {
found := false
for _, addr := range eml.To {
if strings.Contains(addr.Name, req.Search.To) || strings.Contains(addr.Address, req.Search.To) {
found = true
break
}
}
if !found {
match = false
}
}
if req.Search.From != "" {
found := false
for _, addr := range eml.From {
if strings.Contains(addr.Name, req.Search.From) || strings.Contains(addr.Address, req.Search.From) {
found = true
break
}
}
if !found {
match = false
}
}
if !req.Search.After.IsZero() && !eml.SentAt.After(req.Search.After) {
match = false
}
if !req.Search.Before.IsZero() && !eml.SentAt.Before(req.Search.Before) {
match = false
}
if match {
filtered = append(filtered, eml)
}
}
return &InboxData{filtered}, nil
}

64
internal/route/api.go Normal file
View 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)
}

View File

@ -2,7 +2,10 @@ package route
import ( import (
"net/http" "net/http"
"strconv"
"time"
"forge.cadoles.com/wpetit/fake-smtp/internal/query"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/template" "gitlab.com/wpetit/goweb/service/template"
@ -20,3 +23,84 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da
return data 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
}

View File

@ -7,6 +7,7 @@ import (
"forge.cadoles.com/wpetit/fake-smtp/internal/query" "forge.cadoles.com/wpetit/fake-smtp/internal/query"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/cqrs" "gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/logger"
"gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/template" "gitlab.com/wpetit/goweb/service/template"
) )
@ -14,12 +15,18 @@ import (
func serveInboxPage(w http.ResponseWriter, r *http.Request) { func serveInboxPage(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context()) ctn := container.Must(r.Context())
tmpl := template.Must(ctn) tmpl := template.Must(ctn)
bus := cqrs.Must(ctn) bus := cqrs.Must(ctn)
getInbox := &query.GetInboxRequest{}
ctx := r.Context() 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) result, err := bus.Query(ctx, getInbox)
if err != nil { if err != nil {
panic(errors.Wrap(err, "could not retrieve inbox")) panic(errors.Wrap(err, "could not retrieve inbox"))

View File

@ -17,6 +17,13 @@ func Mount(r *chi.Mux, config *config.Config) error {
r.Delete("/emails/{id}", handleEmailDelete) 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() notFoundHandler := r.NotFoundHandler()
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler)) r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler))