forked from umputun/secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue umputun#31: Replace separate UI with the embedded HTMX based
- Loading branch information
Showing
19 changed files
with
855 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/go-chi/chi/v5" | ||
log "github.com/go-pkgz/lgr" | ||
"github.com/pkg/errors" | ||
"github.com/umputun/secrets/backend/app/messager" | ||
"github.com/umputun/secrets/backend/app/store" | ||
"github.com/umputun/secrets/backend/app/validator" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
baseTmpl = "base" | ||
mainTmpl = "main" | ||
errorTmpl = "error" | ||
|
||
msgKey = "message" | ||
pinKey = "pin" | ||
expKey = "exp" | ||
expUnitKey = "exp-unit" | ||
keyKey = "key" | ||
) | ||
|
||
type createMsgForm struct { | ||
Message string | ||
Pin string | ||
Exp int | ||
ExpUnit string | ||
validator.Validator | ||
} | ||
|
||
type showMsgForm struct { | ||
Key string | ||
Pin string | ||
Message string | ||
validator.Validator | ||
} | ||
|
||
type templateData struct { | ||
Form any | ||
PinSize int | ||
} | ||
|
||
func (s Server) render(w http.ResponseWriter, status int, page string, tmplName string, data any) { | ||
ts, ok := s.TemplateCache[page] | ||
if !ok { | ||
err := fmt.Errorf("the template %s does not exist", page) | ||
log.Printf("[ERROR] %v", err) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
if tmplName == "" { | ||
tmplName = baseTmpl | ||
} | ||
err := ts.ExecuteTemplate(buf, tmplName, data) | ||
if err != nil { | ||
log.Printf("[ERROR] %v", err) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(status) | ||
_, err = buf.WriteTo(w) | ||
if err != nil { | ||
log.Printf("[ERROR] %v", err) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
// renders the home page | ||
func (s Server) indexCtrl(w http.ResponseWriter, r *http.Request) { | ||
data := templateData{ | ||
Form: createMsgForm{}, | ||
PinSize: s.PinSize, | ||
} | ||
s.render(w, http.StatusOK, "home.tmpl.html", baseTmpl, data) | ||
} | ||
|
||
// renders the generate link page | ||
func (s Server) generateLink(w http.ResponseWriter, r *http.Request) { | ||
err := r.ParseForm() | ||
if err != nil { | ||
s.render(w, http.StatusOK, "error.tmpl.html", errorTmpl, err.Error()) | ||
return | ||
} | ||
|
||
exp := r.PostFormValue(expKey) | ||
|
||
form := createMsgForm{ | ||
Message: r.PostForm.Get(msgKey), | ||
Pin: r.PostForm.Get(pinKey), | ||
ExpUnit: r.PostForm.Get(expUnitKey), | ||
} | ||
|
||
form.CheckField(validator.NotBlank(form.Message), msgKey, "Message can't be empty") | ||
form.CheckField(validator.NotBlank(form.Pin), pinKey, fmt.Sprintf("Pin must be %d characters long", s.PinSize)) | ||
form.CheckField(validator.MinChars(form.Pin, s.PinSize), pinKey, fmt.Sprintf("Pin must be %d characters long", s.PinSize)) | ||
form.CheckField(validator.MaxChars(form.Pin, s.PinSize), pinKey, fmt.Sprintf("Pin must be %d characters long", s.PinSize)) | ||
form.CheckField(validator.NotBlank(exp), expKey, "Expire can't be empty") | ||
form.CheckField(validator.IsNumber(exp), expKey, "Expire must be a number") | ||
|
||
expInt, _ := strconv.Atoi(exp) | ||
form.Exp = expInt | ||
expDuration := duration(expInt, r.PostFormValue(expUnitKey)) | ||
|
||
form.CheckField(validator.MaxDuration(expDuration, s.MaxExpire), expKey, fmt.Sprintf("Expire must be less than %s", humanDuration(s.MaxExpire))) | ||
|
||
if !form.Valid() { | ||
data := templateData{ | ||
Form: form, | ||
PinSize: s.PinSize, | ||
} | ||
|
||
s.render(w, http.StatusOK, "home.tmpl.html", mainTmpl, data) | ||
return | ||
} | ||
|
||
msg, err := s.Messager.MakeMessage(time.Minute*time.Duration(expInt), form.Message, form.Pin) | ||
if err != nil { | ||
s.render(w, http.StatusOK, "secure-link.tmpl.html", errorTmpl, err.Error()) | ||
return | ||
} | ||
|
||
msgUrl := fmt.Sprintf("http://%s/message/%s", s.Domain, msg.Key) | ||
|
||
data := struct { | ||
Url string | ||
Expire string | ||
}{ | ||
Url: msgUrl, | ||
Expire: msg.Exp.Format("2006-01-02 15:04:05"), | ||
} | ||
|
||
s.render(w, http.StatusOK, "secure-link.tmpl.html", "secure-link", data) | ||
} | ||
|
||
// renders the show decoded message page | ||
func (s Server) showMessageView(w http.ResponseWriter, r *http.Request) { | ||
key := chi.URLParam(r, keyKey) | ||
|
||
data := templateData{ | ||
Form: showMsgForm{ | ||
Key: key, | ||
}, | ||
PinSize: s.PinSize, | ||
} | ||
|
||
s.render(w, http.StatusOK, "show-message.tmpl.html", baseTmpl, data) | ||
} | ||
|
||
func (s Server) loadMessage(w http.ResponseWriter, r *http.Request) { | ||
err := r.ParseForm() | ||
if err != nil { | ||
s.render(w, http.StatusOK, "error.tmpl.html", errorTmpl, err.Error()) | ||
return | ||
} | ||
|
||
form := showMsgForm{ | ||
Key: r.PostForm.Get("key"), | ||
Pin: r.PostForm.Get("pin"), | ||
} | ||
|
||
form.CheckField(validator.NotBlank(form.Pin), "pin", "Pin can't be empty") | ||
form.CheckField(validator.MinChars(form.Pin, s.PinSize), "pin", fmt.Sprintf("Pin must be %d characters long", s.PinSize)) | ||
form.CheckField(validator.MaxChars(form.Pin, s.PinSize), "pin", fmt.Sprintf("Pin must be %d characters long", s.PinSize)) | ||
|
||
if !form.Valid() { | ||
data := templateData{ | ||
Form: form, | ||
PinSize: s.PinSize, | ||
} | ||
|
||
s.render(w, http.StatusOK, "show-message.tmpl.html", mainTmpl, data) | ||
return | ||
} | ||
|
||
msg, err := s.Messager.LoadMessage(form.Key, form.Pin) | ||
if err != nil { | ||
if errors.Is(err, messager.ErrExpired) || errors.Is(err, store.ErrLoadRejected) { | ||
s.render(w, http.StatusOK, "error.tmpl.html", errorTmpl, err.Error()) | ||
return | ||
} | ||
log.Printf("[WARN] can't load message %v", err) | ||
form.AddFieldError("pin", err.Error()) | ||
|
||
data := templateData{ | ||
Form: form, | ||
PinSize: s.PinSize, | ||
} | ||
|
||
s.render(w, http.StatusOK, "show-message.tmpl.html", mainTmpl, data) | ||
|
||
return | ||
} | ||
|
||
s.render(w, http.StatusOK, "decoded-message.tmpl.html", "decoded-message", string(msg.Data)) | ||
} | ||
|
||
func duration(n int, unit string) time.Duration { | ||
switch unit { | ||
case "m": | ||
return time.Duration(n) * time.Minute | ||
case "h": | ||
return time.Duration(n) * time.Hour | ||
case "d": | ||
return time.Duration(n*24) * time.Hour | ||
default: | ||
return time.Duration(0) | ||
} | ||
} | ||
|
||
func humanDuration(d time.Duration) string { | ||
switch { | ||
case d < time.Minute: | ||
return fmt.Sprintf("%d seconds", d/time.Second) | ||
case d < time.Hour: | ||
return fmt.Sprintf("%d minutes", d/time.Minute) | ||
case d < time.Hour*24: | ||
return fmt.Sprintf("%d hours", d/time.Hour) | ||
default: | ||
return fmt.Sprintf("%d days", d/(time.Hour*24)) | ||
} | ||
} |
Oops, something went wrong.