add newsletter

This commit is contained in:
samantha42
2026-05-30 18:15:20 +02:00
parent 681f916787
commit 8d4e523b3e
6 changed files with 104 additions and 17 deletions
+32 -13
View File
@@ -12,7 +12,8 @@ import (
)
type Config struct {
Job string `json:"job"`
Job string `json:"job"`
Emails []string `json:"emails"`
}
func loadConfig() (*Config, error) {
@@ -24,6 +25,14 @@ func loadConfig() (*Config, error) {
return &c, json.Unmarshal(data, &c)
}
func saveConfig(c *Config) error {
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
return os.WriteFile("data.json", data, 0644)
}
// ---------- Main ----------
func main() {
@@ -49,19 +58,29 @@ func main() {
http.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/icon.png") })
http.HandleFunc("GET /login", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/login.html") })
http.HandleFunc("POST /login", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
password := r.FormValue("password")
totp := r.FormValue("totp")
if os.Getenv("password") == password && os.Getenv("auth") == totp {
fmt.Fprintf(w, "logged in")
http.Redirect(w, r, "/admin", http.StatusAccepted)
} else {
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, "password or auth is wrong")
http.HandleFunc("POST /newsletter/subscribe", func(w http.ResponseWriter, r *http.Request) {
var req struct {
Email string `json:"email"`
}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
double := false
conf, err := loadConfig()
for _, e := range conf.Emails {
if e == req.Email {
double = true
}
}
if !double {
conf.Emails = append(conf.Emails, req.Email)
}
saveConfig(conf)
})
http.HandleFunc("GET /status", func(w http.ResponseWriter, r *http.Request) {