Generated plugins (acceptance.json at the root) fail on: HTTP routes not registered through pluginsdk route or a public route reading personal columns; HTML built from unescaped non-constant strings; public POSTs outside route.Form; personal data names in log calls; outbound requests carrying personal data or values the spec does not send to that host. First-party plugins get the same findings as warnings. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.dev.alexdunmow.com/block/pluginsdk/plugin"
|
|
"git.dev.alexdunmow.com/private/quote-requests-a1a5c0/db"
|
|
)
|
|
|
|
func newHTTPHandler(deps plugin.CoreServices) http.Handler {
|
|
setServices(deps)
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/plugins/"+pluginName+"/submit", handleSubmit)
|
|
mux.HandleFunc("/api/plugins/"+pluginName+"/requests", handleList)
|
|
return mux
|
|
}
|
|
|
|
func handleSubmit(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
dbtx, ok := current.Pool.(db.DBTX)
|
|
if !ok {
|
|
http.Error(w, "service unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
email := strings.TrimSpace(r.PostForm.Get("email"))
|
|
params := db.InsertQuoteRequestParams{
|
|
Name: strings.TrimSpace(r.PostForm.Get("name")),
|
|
Email: email,
|
|
Phone: strings.TrimSpace(r.PostForm.Get("phone")),
|
|
PropertyAddress: strings.TrimSpace(r.PostForm.Get("property_address")),
|
|
RoofIssue: strings.TrimSpace(r.PostForm.Get("roof_issue")),
|
|
}
|
|
if err := db.New(dbtx).InsertQuoteRequest(r.Context(), params); err != nil {
|
|
http.Error(w, "could not save your request", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
slog.Info("quote", "email", email)
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write([]byte("<!doctype html><p>Thank you. Your quote request has been received.</p>"))
|
|
}
|
|
|
|
// handleList returns every stored quote request to any caller.
|
|
func handleList(w http.ResponseWriter, r *http.Request) {
|
|
dbtx, ok := current.Pool.(db.DBTX)
|
|
if !ok {
|
|
http.Error(w, "service unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
rows, err := db.New(dbtx).ListQuoteRequests(r.Context())
|
|
if err != nil {
|
|
http.Error(w, "could not load requests", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(rows)
|
|
}
|