137 lines
2.7 KiB
Go
137 lines
2.7 KiB
Go
package route
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"forge.cadoles.com/wpetit/fake-smtp/internal/config"
|
|
"forge.cadoles.com/wpetit/fake-smtp/internal/query"
|
|
"github.com/pkg/errors"
|
|
"gitlab.com/wpetit/goweb/middleware/container"
|
|
"gitlab.com/wpetit/goweb/service"
|
|
"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),
|
|
withBaseURL(ctn),
|
|
)
|
|
if err != nil {
|
|
panic(errors.Wrap(err, "could not extend template 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)
|
|
}
|
|
}
|
|
|
|
var headers map[string]string
|
|
|
|
rawHeaders := r.URL.Query().Get("headers")
|
|
if rawHeaders != "" {
|
|
headers = make(map[string]string)
|
|
if err := json.Unmarshal([]byte(rawHeaders), &headers); 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
|
|
}
|
|
|
|
if rawHeaders != "" {
|
|
search.Headers = headers
|
|
}
|
|
|
|
inboxRequest := &query.GetInboxRequest{
|
|
OrderBy: orderBy,
|
|
Reverse: reverse == "y",
|
|
Skip: int(skip),
|
|
Limit: int(limit),
|
|
Search: search,
|
|
}
|
|
|
|
return inboxRequest, nil
|
|
}
|
|
|
|
func withBaseURL(ctn *service.Container) template.DataExtFunc {
|
|
return func(data template.Data) (template.Data, error) {
|
|
conf, err := config.From(ctn)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
data["BaseURL"] = conf.HTTP.BaseURL
|
|
|
|
return data, nil
|
|
}
|
|
}
|