mirror of
https://github.com/Dvorinka/SendMail.git
synced 2026-07-29 10:03:48 +00:00
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:
@@ -6,6 +6,7 @@ import (
|
|||||||
"html"
|
"html"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -208,6 +209,7 @@ var domainMap = map[string]EmailConfig{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendHandler(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "POST only", http.StatusMethodNotAllowed)
|
http.Error(w, "POST only", http.StatusMethodNotAllowed)
|
||||||
return
|
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
|
// 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) {
|
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 {
|
if r.Method != http.MethodGet {
|
||||||
http.Error(w, "GET only", http.StatusMethodNotAllowed)
|
http.Error(w, "GET only", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
@@ -363,6 +366,16 @@ func main() {
|
|||||||
http.HandleFunc("/", enableCors(rootHandler))
|
http.HandleFunc("/", enableCors(rootHandler))
|
||||||
http.HandleFunc("/send", enableCors(sendHandler))
|
http.HandleFunc("/send", enableCors(sendHandler))
|
||||||
http.HandleFunc("/send-params", enableCors(sendParamsHandler))
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user