mirror of
https://github.com/ION606/MailPocket.git
synced 2026-05-14 22:06:55 +00:00
added Makefile variable port
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
|
PORT=15521
|
||||||
|
|
||||||
.PHONY: run-batched run-sqlite setup-sqlite
|
.PHONY: run-batched run-sqlite setup-sqlite
|
||||||
|
|
||||||
run-batched:
|
run-batched:
|
||||||
cd batched-server && go run main.go
|
cd batched-server && go run main.go $(PORT)
|
||||||
|
|
||||||
run-sqlite: setup-sqlite
|
run-sqlite: setup-sqlite
|
||||||
cd sqlite-server && go run main.go
|
cd sqlite-server && go run main.go $(PORT)
|
||||||
|
|
||||||
setup-sqlite:
|
setup-sqlite:
|
||||||
@if [ ! -f sqlite-server/go.mod ]; then \
|
@if [ ! -f sqlite-server/go.mod ]; then \
|
||||||
|
|||||||
+24
-14
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -10,14 +11,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PORT = ":3000"
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
emailQueue []string
|
emailQueue []string
|
||||||
queueLock sync.Mutex
|
queueLock sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func saveEmails() {
|
func saveEmails() {
|
||||||
queueLock.Lock()
|
queueLock.Lock()
|
||||||
defer queueLock.Unlock()
|
defer queueLock.Unlock()
|
||||||
@@ -33,6 +31,7 @@ func saveEmails() {
|
|||||||
|
|
||||||
f, err := os.OpenFile("emails.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
f, err := os.OpenFile("emails.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("Failed to open file:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
@@ -40,22 +39,24 @@ func saveEmails() {
|
|||||||
writer := csv.NewWriter(f)
|
writer := csv.NewWriter(f)
|
||||||
defer writer.Flush()
|
defer writer.Flush()
|
||||||
|
|
||||||
// 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", "timestamp"}); err != nil {
|
||||||
|
log.Println("Failed to write header:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write each email with timestamp
|
// Write each email with timestamp
|
||||||
for _, email := range emailQueue {
|
for _, email := 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{email, timestamp}); err != nil {
|
||||||
|
log.Println("Failed to write email:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear queue
|
// Clear queue
|
||||||
emailQueue = emailQueue[:0]
|
emailQueue = emailQueue[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,18 +64,18 @@ func saveEmails() {
|
|||||||
func init() {
|
func init() {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(5 * time.Second);
|
time.Sleep(5 * time.Second)
|
||||||
queueLock.Lock();
|
queueLock.Lock()
|
||||||
hasEmails := len(emailQueue) > 0;
|
hasEmails := len(emailQueue) > 0
|
||||||
queueLock.Unlock();
|
queueLock.Unlock()
|
||||||
if hasEmails {
|
if hasEmails {
|
||||||
saveEmails();
|
log.Println("Flushing email queue...")
|
||||||
|
saveEmails()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}();
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func submitHandler(w http.ResponseWriter, r *http.Request) {
|
func submitHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
@@ -102,10 +103,19 @@ func submitHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var PORT string
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
PORT = os.Args[1]
|
||||||
|
} else {
|
||||||
|
PORT = "15521"
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/submit", submitHandler)
|
http.HandleFunc("/submit", submitHandler)
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("Batched Write Server is running"))
|
w.Write([]byte("Batched Write Server is running"))
|
||||||
})
|
})
|
||||||
http.ListenAndServe(PORT, nil)
|
|
||||||
|
log.Println("Starting server on port", PORT)
|
||||||
|
log.Fatal(http.ListenAndServe(":"+PORT, nil))
|
||||||
}
|
}
|
||||||
@@ -8,9 +8,10 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
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/sys v0.22.0 // indirect
|
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
|
||||||
modernc.org/libc v1.55.3 // indirect
|
golang.org/x/sys v0.28.0 // indirect
|
||||||
modernc.org/mathutil v1.6.0 // indirect
|
modernc.org/libc v1.61.13 // indirect
|
||||||
modernc.org/memory v1.8.0 // indirect
|
modernc.org/mathutil v1.7.1 // indirect
|
||||||
modernc.org/sqlite v1.34.5 // indirect
|
modernc.org/memory v1.8.2 // indirect
|
||||||
|
modernc.org/sqlite v1.35.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
+11
-3
@@ -3,15 +3,22 @@ package main
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PORT = ":3000"
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var PORT string
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
PORT = os.Args[1]
|
||||||
|
} else {
|
||||||
|
PORT = "15521"
|
||||||
|
}
|
||||||
|
|
||||||
db, _ := sql.Open("sqlite", "emails.db")
|
db, _ := sql.Open("sqlite", "emails.db")
|
||||||
db.Exec(`CREATE TABLE IF NOT EXISTS emails (
|
db.Exec(`CREATE TABLE IF NOT EXISTS emails (
|
||||||
email TEXT PRIMARY KEY,
|
email TEXT PRIMARY KEY,
|
||||||
@@ -40,5 +47,6 @@ func main() {
|
|||||||
w.Write([]byte("SQLite Write Server is running"))
|
w.Write([]byte("SQLite Write Server is running"))
|
||||||
})
|
})
|
||||||
|
|
||||||
http.ListenAndServe(PORT, nil)
|
log.Println("Starting server on port", PORT)
|
||||||
|
log.Fatal(http.ListenAndServe(":"+PORT, nil))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user