secrets: use httpin

This commit is contained in:
Chris Sexton 2024-03-13 12:21:07 -04:00
parent b73e64ad72
commit 28e9fc2480
1 changed files with 12 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package secrets
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/ggicci/httpin"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -31,9 +32,10 @@ func New(b bot.Bot) bot.Plugin {
func (p *SecretsPlugin) registerWeb() { func (p *SecretsPlugin) registerWeb() {
r := chi.NewRouter() r := chi.NewRouter()
r.HandleFunc("/add", p.handleRegister) r.With(httpin.NewInput(RegisterReq{})).
r.HandleFunc("/remove", p.handleRemove) Post("/add", p.handleRegister)
r.HandleFunc("/", p.handleIndex) r.Delete("/remove", p.handleRemove)
r.Get("/", p.handleIndex)
p.b.GetWeb().RegisterWebName(r, "/secrets", "Secrets") p.b.GetWeb().RegisterWebName(r, "/secrets", "Secrets")
} }
@ -71,14 +73,15 @@ func (p *SecretsPlugin) handleAll(w http.ResponseWriter, r *http.Request) {
p.sendKeys(w, r) p.sendKeys(w, r)
} }
func (p *SecretsPlugin) handleRegister(w http.ResponseWriter, r *http.Request) { type RegisterReq struct {
if checkMethod(http.MethodPost, w, r) { Key string `in:"form=key"`
log.Debug().Msgf("failed post %s", r.Method) Value string `in:"form=value"`
return
} }
func (p *SecretsPlugin) handleRegister(w http.ResponseWriter, r *http.Request) {
input := r.Context().Value(httpin.Input).(*RegisterReq)
checkError := mkCheckError(w) checkError := mkCheckError(w)
key, value := r.FormValue("key"), r.FormValue("value") err := p.c.RegisterSecret(input.Key, input.Value)
err := p.c.RegisterSecret(key, value)
if checkError(err) { if checkError(err) {
return return
} }
@ -86,9 +89,6 @@ func (p *SecretsPlugin) handleRegister(w http.ResponseWriter, r *http.Request) {
} }
func (p *SecretsPlugin) handleRemove(w http.ResponseWriter, r *http.Request) { func (p *SecretsPlugin) handleRemove(w http.ResponseWriter, r *http.Request) {
if checkMethod(http.MethodDelete, w, r) {
return
}
checkError := mkCheckError(w) checkError := mkCheckError(w)
b, err := io.ReadAll(r.Body) b, err := io.ReadAll(r.Body)
if checkError(err) { if checkError(err) {