pay/internal/payment/delivery/http/payment_http.go

53 lines
1.2 KiB
Go

package http
import (
"fmt"
"net/http"
"strconv"
"golang.org/x/text/language"
"golang.org/x/text/message"
"source.toby3d.me/toby3d/pay/internal/common"
"source.toby3d.me/toby3d/pay/internal/urlutil"
"source.toby3d.me/toby3d/pay/web/template"
)
type Handler struct {
matcher language.Matcher
}
func NewHandler() *Handler {
return &Handler{
matcher: language.NewMatcher(append([]language.Tag{language.English},
message.DefaultCatalog.Languages()...)),
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tags, _, _ := language.ParseAcceptLanguage(r.Header.Get(common.HeaderAcceptLanguage))
tag, _, _ := h.matcher.Match(tags...)
var amount uint64
if head, _ := urlutil.ShiftPath(r.URL.Path); head != "" {
parsed, err := strconv.ParseFloat(head, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// NOTE(toby3d): Take my money? No way!
if parsed < 0 {
http.Redirect(w, r, fmt.Sprint("/", parsed*-1), http.StatusMovedPermanently)
return
}
amount = uint64(parsed * 100)
}
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
template.WriteTemplate(w, template.NewContext(tag, amount))
}