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-24 12:32:02 -05:00
|
|
|
"os"
|
2025-02-08 13:05:53 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2025-02-24 12:32:02 -05:00
|
|
|
var PORT string
|
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
|
PORT = os.Args[1]
|
|
|
|
|
} else {
|
|
|
|
|
PORT = "15521"
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-08 13:05:53 -05:00
|
|
|
db, _ := sql.Open("sqlite", "emails.db")
|
|
|
|
|
db.Exec(`CREATE TABLE IF NOT EXISTS emails (
|
|
|
|
|
email TEXT PRIMARY KEY,
|
|
|
|
|
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
|
|
|
)`)
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
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"})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
w.Write([]byte("SQLite Write Server is running"))
|
|
|
|
|
})
|
|
|
|
|
|
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
|
|
|
}
|