feat(main): add request logging and configurable server address

Add logging to `sendHandler` and `sendParamsHandler` to track incoming
requests. Update the server startup logic to use `HOST` and `PORT`
environment variables, defaulting to `localhost:660` if not provided.
This commit is contained in:
Tomas Dvorak
2026-05-13 15:20:17 +02:00
parent 28070f8d74
commit bcc967f7d7
+15 -2
View File
@@ -6,6 +6,7 @@ import (
"html"
"log"
"net/http"
"os"
"strconv"
"strings"
@@ -208,6 +209,7 @@ var domainMap = map[string]EmailConfig{
}
func sendHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[send] INCOMING %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
if r.Method != http.MethodPost {
http.Error(w, "POST only", http.StatusMethodNotAllowed)
return
@@ -301,6 +303,7 @@ func enableCors(handler http.HandlerFunc) http.HandlerFunc {
//
// GET /[email protected]&pass=secret&server=smtp.example.com&port=465&[email protected]&[email protected]&subject=Hello&message=Hi
func sendParamsHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[send-params] INCOMING %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
if r.Method != http.MethodGet {
http.Error(w, "GET only", http.StatusMethodNotAllowed)
return
@@ -363,6 +366,16 @@ func main() {
http.HandleFunc("/", enableCors(rootHandler))
http.HandleFunc("/send", enableCors(sendHandler))
http.HandleFunc("/send-params", enableCors(sendParamsHandler))
log.Println("Server running on localhost:660")
log.Fatal(http.ListenAndServe("localhost:660", nil))
host := os.Getenv("HOST")
if host == "" {
host = "localhost"
}
port := os.Getenv("PORT")
if port == "" {
port = "660"
}
addr := host + ":" + port
log.Println("Server running on " + addr)
log.Fatal(http.ListenAndServe(addr, nil))
}