2025-02-08 13:05:53 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/json"
|
2025-02-24 12:32:02 -05:00
|
|
|
"log"
|
2025-02-08 13:05:53 -05:00
|
|
|
"net/http"
|
2025-02-28 19:11:16 -05:00
|
|
|
"os"
|
2025-02-24 18:53:25 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
"shared"
|
2025-02-28 19:11:16 -05:00
|
|
|
"strings"
|
|
|
|
|
|
2025-02-08 13:05:53 -05:00
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2025-02-28 19:11:16 -05:00
|
|
|
_, PORT, dbdir := shared.GetArgs()
|
2025-02-24 18:53:25 -05:00
|
|
|
db, _ := sql.Open("sqlite", filepath.Join(dbdir, "emails.db"))
|
2025-02-08 13:05:53 -05:00
|
|
|
db.Exec(`CREATE TABLE IF NOT EXISTS emails (
|
2025-02-28 19:11:16 -05:00
|
|
|
email TEXT,
|
|
|
|
|
formname TEXT,
|
|
|
|
|
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
PRIMARY KEY (email, formname)
|
2025-02-08 13:05:53 -05:00
|
|
|
)`)
|
|
|
|
|
|
2025-02-28 19:11:16 -05:00
|
|
|
adminPassword := os.Getenv("ADMIN_PASSWORD")
|
|
|
|
|
|
|
|
|
|
if adminPassword == "" {
|
|
|
|
|
log.Fatal("ADMIN_PASSWORD environment variable required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shared.StartAdminServer(dbdir)
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
|
// Handle root GET request
|
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
w.Write([]byte("SQLite Write Server is running"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle form submissions for other paths
|
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formname := strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
|
if formname == "" {
|
|
|
|
|
http.Error(w, "Form name required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-08 13:05:53 -05:00
|
|
|
email := strings.TrimSpace(r.FormValue("email"))
|
|
|
|
|
if email == "" {
|
|
|
|
|
http.Error(w, "Email required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 19:11:16 -05:00
|
|
|
_, err := db.Exec(`INSERT OR IGNORE INTO emails(email, formname) VALUES (?, ?)`, email, formname)
|
2025-02-08 13:05:53 -05:00
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
if err != nil {
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "storage failed"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "data received"})
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-24 12:32:02 -05:00
|
|
|
log.Println("Starting server on port", PORT)
|
|
|
|
|
log.Fatal(http.ListenAndServe(":"+PORT, nil))
|
2025-02-08 13:05:53 -05:00
|
|
|
}
|