Created Turnpike, event attendee and volunteer management
Built after prototype, Traverse, an attendee and volunteer list maintainer.
This commit is contained in:
commit
d05b8dc7e0
59 changed files with 8663 additions and 0 deletions
78
handle_settings.go
Normal file
78
handle_settings.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (app *App) handleGetSettings(w http.ResponseWriter, r *http.Request) {
|
||||
cfg := app.loadSMTPConfig()
|
||||
|
||||
baseURL := app.baseURL
|
||||
if baseURL == "" {
|
||||
app.db.QueryRow(`SELECT value FROM config WHERE key = 'base_url'`).Scan(&baseURL)
|
||||
}
|
||||
|
||||
pass := ""
|
||||
if cfg.Password != "" {
|
||||
pass = "***"
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]any{
|
||||
"smtp_host": cfg.Host,
|
||||
"smtp_port": cfg.Port,
|
||||
"smtp_user": cfg.User,
|
||||
"smtp_password": pass,
|
||||
"smtp_from": cfg.From,
|
||||
"smtp_from_name": cfg.FromName,
|
||||
"base_url": baseURL,
|
||||
})
|
||||
}
|
||||
|
||||
func (app *App) handleUpdateSettings(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, "invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
keys := []string{"smtp_host", "smtp_port", "smtp_user", "smtp_password", "smtp_from", "smtp_from_name", "base_url"}
|
||||
for _, k := range keys {
|
||||
v, ok := body[k]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
var val string
|
||||
switch vv := v.(type) {
|
||||
case string:
|
||||
if k == "smtp_password" && vv == "" {
|
||||
continue // don't erase the stored password with an empty value
|
||||
}
|
||||
val = vv
|
||||
case float64:
|
||||
val = strconv.Itoa(int(vv))
|
||||
default:
|
||||
continue
|
||||
}
|
||||
app.db.Exec(`INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)`, k, val)
|
||||
}
|
||||
|
||||
app.handleGetSettings(w, r)
|
||||
}
|
||||
|
||||
func (app *App) handleTestEmail(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
To string `json:"to"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.To == "" {
|
||||
writeError(w, "to email address required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
cfg := app.loadSMTPConfig()
|
||||
if err := sendEmail(cfg, body.To, "Turnpike test email", "This is a test email from your Turnpike instance. SMTP is configured correctly."); err != nil {
|
||||
writeError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"ok": true})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue