fake-smtp/internal/route/helper.go

137 lines
2.7 KiB
Go
Raw Permalink Normal View History

2020-04-17 17:53:01 +02:00
package route
import (
2022-02-18 10:14:16 +01:00
"encoding/json"
2020-04-17 17:53:01 +02:00
"net/http"
2020-11-03 11:49:31 +01:00
"strconv"
"time"
2020-04-17 17:53:01 +02:00
2021-07-26 14:47:58 +02:00
"forge.cadoles.com/wpetit/fake-smtp/internal/config"
2020-11-03 11:49:31 +01:00
"forge.cadoles.com/wpetit/fake-smtp/internal/query"
2020-04-17 17:53:01 +02:00
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
2021-07-26 14:47:58 +02:00
"gitlab.com/wpetit/goweb/service"
2020-04-17 17:53:01 +02:00
"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),
2021-07-26 14:47:58 +02:00
withBaseURL(ctn),
2020-04-17 17:53:01 +02:00
)
if err != nil {
panic(errors.Wrap(err, "could not extend template data"))
}
return data
}
2020-11-03 11:49:31 +01:00
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)
}
}
2022-02-18 10:14:16 +01:00
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)
}
}
2020-11-03 11:49:31 +01:00
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
}
2022-02-18 10:14:16 +01:00
if rawHeaders != "" {
search.Headers = headers
}
2020-11-03 11:49:31 +01:00
inboxRequest := &query.GetInboxRequest{
OrderBy: orderBy,
Reverse: reverse == "y",
Skip: int(skip),
Limit: int(limit),
Search: search,
}
return inboxRequest, nil
}
2021-07-26 14:47:58 +02:00
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
}
}