mirror of
https://github.com/ION606/MailPocket.git
synced 2026-05-14 22:06:55 +00:00
added admin routes
This commit is contained in:
+41
-11
@@ -5,28 +5,63 @@ import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"shared"
|
||||
"strings"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func main() {
|
||||
PORT, dbdir := shared.GetArgs()
|
||||
_, PORT, dbdir := shared.GetArgs()
|
||||
db, _ := sql.Open("sqlite", filepath.Join(dbdir, "emails.db"))
|
||||
db.Exec(`CREATE TABLE IF NOT EXISTS emails (
|
||||
email TEXT PRIMARY KEY,
|
||||
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
email TEXT,
|
||||
formname TEXT,
|
||||
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (email, formname)
|
||||
)`)
|
||||
|
||||
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
|
||||
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
|
||||
}
|
||||
|
||||
email := strings.TrimSpace(r.FormValue("email"))
|
||||
if email == "" {
|
||||
http.Error(w, "Email required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := db.Exec(`INSERT OR IGNORE INTO emails(email) VALUES (?)`, email)
|
||||
_, err := db.Exec(`INSERT OR IGNORE INTO emails(email, formname) VALUES (?, ?)`, email, formname)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err != nil {
|
||||
@@ -36,11 +71,6 @@ func main() {
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "data received"})
|
||||
})
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("SQLite Write Server is running"))
|
||||
})
|
||||
|
||||
log.Println("Starting server on port", PORT)
|
||||
log.Fatal(http.ListenAndServe(":"+PORT, nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user