added admin routes

This commit is contained in:
2025-02-28 19:11:16 -05:00
parent 29ef485e29
commit da40f652a7
10 changed files with 152 additions and 82 deletions
+1 -1
View File
@@ -15,5 +15,5 @@ RUN go mod download
RUN go build -o batched-server . RUN go build -o batched-server .
EXPOSE 15521 EXPOSE 15521 15522
CMD ["./batched-server", "15521"] CMD ["./batched-server", "15521"]
+1 -1
View File
@@ -15,5 +15,5 @@ RUN go mod download
RUN go build -o sqlite-server . RUN go build -o sqlite-server .
EXPOSE 15521 EXPOSE 15521 15522
CMD ["./sqlite-server", "15521"] CMD ["./sqlite-server", "15521"]
+3 -2
View File
@@ -1,4 +1,5 @@
PORT=15521 PORT=15521
ADMINPORT=15522
VOLUME_NAME=mailpocket-data VOLUME_NAME=mailpocket-data
.PHONY: run-batched run-sqlite stop reset .PHONY: run-batched run-sqlite stop reset
@@ -11,11 +12,11 @@ test-vol:
run-batched: test-vol stop run-batched: test-vol stop
docker build -t batched-server -f Dockerfile.batched . docker build -t batched-server -f Dockerfile.batched .
docker run -p $(PORT):$(PORT) --name batched-server -v $(VOLUME_NAME):/app/data batched-server docker run -d -p $(PORT):$(PORT) -p $(ADMINPORT):$(ADMINPORT) --name batched-server -v $(VOLUME_NAME):/app/data --env-file=.env batched-server
run-sqlite: test-vol stop run-sqlite: test-vol stop
docker build -t sqlite-server -f Dockerfile.sqldb . docker build -t sqlite-server -f Dockerfile.sqldb .
docker run -d -p $(PORT):$(PORT) --name sqlite-server -v $(VOLUME_NAME):/app/data sqlite-server docker run -d -p $(PORT):$(PORT) -p $(ADMINPORT):$(ADMINPORT) --name sqlite-server -v $(VOLUME_NAME):/app/data --env-file=.env sqlite-server
stop: stop:
docker stop batched-server || true docker stop batched-server || true
+3
View File
@@ -42,6 +42,9 @@ Both servers are designed to be simple, efficient, and easy to deploy.
git clone https://github.com/your-username/forms-server.git git clone https://github.com/your-username/forms-server.git
cd forms-server cd forms-server
``` ```
1.1 ENV
- create an env file with `ADMIN_PASSWORD=your_password_here` in it
- put this in the base directory (i.e. on the same level as the Dockerfiles/Makefile)
2.0 Using the Makefile: 2.0 Using the Makefile:
- ```bash - ```bash
+14
View File
@@ -5,3 +5,17 @@ go 1.23.6
replace shared => ../shared replace shared => ../shared
require shared v0.0.0-00010101000000-000000000000 require shared v0.0.0-00010101000000-000000000000
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/sys v0.30.0 // indirect
modernc.org/libc v1.61.13 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.8.2 // indirect
modernc.org/sqlite v1.36.0 // indirect
)
+34 -17
View File
@@ -13,8 +13,13 @@ import (
"time" "time"
) )
type EmailEntry struct {
Email string
FormName string
}
var ( var (
emailQueue []string emailQueue []EmailEntry // Changed to struct slice
queueLock sync.Mutex queueLock sync.Mutex
dbdir string dbdir string
PORT string PORT string
@@ -46,26 +51,24 @@ func saveEmails() {
// Write header if file is new // Write header if file is new
if !fileExists { if !fileExists {
if err := writer.Write([]string{"email", "timestamp"}); err != nil { if err := writer.Write([]string{"email", "formname", "timestamp"}); err != nil {
log.Println("Failed to write header:", err) log.Println("Failed to write header:", err)
return return
} }
} }
// Write each email with timestamp // Write each entry
for _, email := range emailQueue { for _, entry := range emailQueue {
timestamp := time.Now().Format(time.RFC3339) timestamp := time.Now().Format(time.RFC3339)
if err := writer.Write([]string{email, timestamp}); err != nil { if err := writer.Write([]string{entry.Email, entry.FormName, timestamp}); err != nil {
log.Println("Failed to write email:", err) log.Println("Failed to write email:", err)
return return
} }
} }
// Clear queue emailQueue = emailQueue[:0] // Clear queue
emailQueue = emailQueue[:0]
} }
// background goroutine to batch writes
func init() { func init() {
go func() { go func() {
for { for {
@@ -81,12 +84,28 @@ func init() {
}() }()
} }
func submitHandler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.URL.Path == "/" {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Batched Write Server is running"))
return
}
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
formName := strings.Trim(r.URL.Path, "/")
if formName == "" {
http.Error(w, "Form name required", http.StatusBadRequest)
return
}
email := strings.TrimSpace(r.FormValue("email")) email := strings.TrimSpace(r.FormValue("email"))
if email == "" { if email == "" {
http.Error(w, "Email required", http.StatusBadRequest) http.Error(w, "Email required", http.StatusBadRequest)
@@ -94,7 +113,7 @@ func submitHandler(w http.ResponseWriter, r *http.Request) {
} }
queueLock.Lock() queueLock.Lock()
emailQueue = append(emailQueue, email) emailQueue = append(emailQueue, EmailEntry{Email: email, FormName: formName})
shouldFlush := len(emailQueue) >= 100 shouldFlush := len(emailQueue) >= 100
queueLock.Unlock() queueLock.Unlock()
@@ -107,14 +126,12 @@ func submitHandler(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
PORT, dbdir = shared.GetArgs() _, PORT, dbdir = shared.GetArgs()
fpath = filepath.Join(dbdir, "emails.csv") fpath = filepath.Join(dbdir, "emails.csv")
http.HandleFunc("/submit", submitHandler) shared.StartAdminServer(dbdir)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) http.HandleFunc("/", handler) // Single handler for all routes
w.Write([]byte("Batched Write Server is running"))
})
log.Println("Starting server on port", PORT) log.Println("Starting server on port", PORT)
log.Fatal(http.ListenAndServe(":"+PORT, nil)) log.Fatal(http.ListenAndServe(":"+PORT, nil))
+6 -3
View File
@@ -2,6 +2,11 @@ module sqlite-server
go 1.23.6 go 1.23.6
require (
modernc.org/sqlite v1.36.0
shared v0.0.0-00010101000000-000000000000
)
require ( require (
github.com/dustin/go-humanize v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
@@ -9,12 +14,10 @@ require (
github.com/ncruces/go-strftime v0.1.9 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/sys v0.28.0 // indirect golang.org/x/sys v0.30.0 // indirect
modernc.org/libc v1.61.13 // indirect modernc.org/libc v1.61.13 // indirect
modernc.org/mathutil v1.7.1 // indirect modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.8.2 // indirect modernc.org/memory v1.8.2 // indirect
modernc.org/sqlite v1.35.0 // indirect
shared v0.0.0-00010101000000-000000000000 // indirect
) )
replace shared => ../shared replace shared => ../shared
+41 -11
View File
@@ -5,28 +5,63 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"strings"
"shared" "shared"
"strings"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
) )
func main() { func main() {
PORT, dbdir := shared.GetArgs() _, PORT, dbdir := shared.GetArgs()
db, _ := sql.Open("sqlite", filepath.Join(dbdir, "emails.db")) db, _ := sql.Open("sqlite", filepath.Join(dbdir, "emails.db"))
db.Exec(`CREATE TABLE IF NOT EXISTS emails ( db.Exec(`CREATE TABLE IF NOT EXISTS emails (
email TEXT PRIMARY KEY, email TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP 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")) email := strings.TrimSpace(r.FormValue("email"))
if email == "" { if email == "" {
http.Error(w, "Email required", http.StatusBadRequest) http.Error(w, "Email required", http.StatusBadRequest)
return 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") w.Header().Set("Content-Type", "application/json")
if err != nil { if err != nil {
@@ -36,11 +71,6 @@ func main() {
json.NewEncoder(w).Encode(map[string]string{"message": "data received"}) 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.Println("Starting server on port", PORT)
log.Fatal(http.ListenAndServe(":"+PORT, nil)) log.Fatal(http.ListenAndServe(":"+PORT, nil))
} }
+20 -17
View File
@@ -1,6 +1,8 @@
#!/bin/bash #!/bin/bash
CONTAINER_NAME="batched-server"
SERVER_URL="http://localhost:15521" SERVER_URL="http://localhost:15521"
VOLUME_PATH="/app/data" # Path inside container
test_server_running() { test_server_running() {
echo "Testing if the server is running..." echo "Testing if the server is running..."
@@ -14,40 +16,41 @@ test_server_running() {
fi fi
} }
test_form_submission() {
test_submit_email() { echo "Testing form submission to dynamic endpoint..."
echo "Testing email submission..." FORM_NAME="testform"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST -d "email=test@example.com" "$SERVER_URL/submit") RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST -d "email=test@example.com" "$SERVER_URL/$FORM_NAME")
if [ "$RESPONSE" -eq 200 ]; then if [ "$RESPONSE" -eq 200 ]; then
echo "✅ Email submission successful (Status: 200 OK)" echo "✅ Form submission to /$FORM_NAME successful (Status: 200 OK)"
else else
echo "Email submission failed (Received status: $RESPONSE)" echo "Form submission failed (Received status: $RESPONSE)"
exit 1 exit 1
fi fi
} }
test_csv_written() { test_csv_written() {
CSV_FILE="batched-server/emails.csv" CSV_FILE="$VOLUME_PATH/emails.csv"
FORM_NAME="testform"
if [ -f "$CSV_FILE" ]; then # Use docker exec to check CSV inside container
if grep -q "test@example.com" "$CSV_FILE"; then if docker exec $CONTAINER_NAME /bin/sh -c "[ -f $CSV_FILE ]"; then
echo "✅ Email found in $CSV_FILE" if docker exec $CONTAINER_NAME grep -q "test@example.com,$FORM_NAME" "$CSV_FILE"; then
echo "✅ Entry found in CSV (form: $FORM_NAME)"
else else
echo "❌ Email NOT found in $CSV_FILE" echo "❌ Entry NOT found in CSV"
exit 1 exit 1
fi fi
else else
echo "❌ CSV file not found!" echo "❌ CSV file not found in container!"
exit 1 exit 1
fi fi
} }
# run the tests # Run tests
test_server_running test_server_running
test_submit_email test_form_submission
sleep 2 # wait for the batched write to complete sleep 2 # Allow time for batched write
test_csv_written test_csv_written
echo "✅ All tests passed successfully!" echo "✅ All batched CSV tests passed!"
+29 -30
View File
@@ -1,58 +1,57 @@
#!/bin/bash #!/bin/bash
CONTAINER_NAME="sqlite-server"
SERVER_URL="http://localhost:15521" SERVER_URL="http://localhost:15521"
VOLUME_PATH="/app/data"
test_server_running() { test_server_running() {
# testing if the server is running...
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$SERVER_URL/") RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$SERVER_URL/")
if [ "$RESPONSE" -eq 200 ]; then if [ "$RESPONSE" -eq 200 ]; then
echo "✅ server is running (status: 200 ok)"; echo "✅ Server is running (status: 200 OK)"
else else
echo "server is not running (received status: $RESPONSE)"; echo "Server is not running (received status: $RESPONSE)"
exit 1; exit 1
fi fi
} }
test_submit_email() { test_form_submission() {
# testing email submission... FORM_NAME="testform"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST -d "email=test@example.com" "$SERVER_URL/submit") RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST -d "email=test@example.com" "$SERVER_URL/$FORM_NAME")
if [ "$RESPONSE" -eq 200 ]; then if [ "$RESPONSE" -eq 200 ]; then
echo "✅ email submission successful (status: 200 ok)"; echo "✅ Form submission to /$FORM_NAME successful (status: 200 OK)"
else else
echo "email submission failed (received status: $RESPONSE)"; echo "Form submission failed (received status: $RESPONSE)"
exit 1; exit 1
fi fi
} }
test_db_entry() { test_db_entry() {
# testing if the email was written to the sqlite database... DB_FILE="$VOLUME_PATH/emails.db"
DB_FILE="sqlite-server/emails.db" FORM_NAME="testform"
if [ -f "$DB_FILE" ]; then # Use docker exec to check database inside container
if ! command -v sqlite3 >/dev/null 2>&1; then if docker exec $CONTAINER_NAME /bin/sh -c "[ -f $DB_FILE ]"; then
echo "❌ sqlite3 is not installed. please install sqlite3 to run this test"; result=$(docker exec $CONTAINER_NAME sqlite3 "$DB_FILE" \
exit 1; "SELECT email FROM emails WHERE email='test@example.com' AND formname='$FORM_NAME';")
fi;
result=$(sqlite3 "$DB_FILE" "select email from emails where email='test@example.com';")
if [ "$result" == "test@example.com" ]; then if [ "$result" == "test@example.com" ]; then
echo "email found in $DB_FILE"; echo "Entry found in database (form: $FORM_NAME)"
else else
echo "❌ email not found in $DB_FILE"; echo "❌ Entry not found in database"
exit 1; exit 1
fi; fi
else else
echo "database file $DB_FILE not found!"; echo "❌ Database file not found in container!"
exit 1; exit 1
fi fi
} }
# run the tests # Run tests
test_server_running; test_server_running
test_submit_email; test_form_submission
sleep 2; # wait for the write to complete sleep 2 # Allow time for async write
test_db_entry; test_db_entry
echo "✅ all tests passed successfully!"; echo "✅ All SQLite tests passed!"