diff --git a/README.md b/README.md index 0e7126b..bb63f32 100644 --- a/README.md +++ b/README.md @@ -2617,6 +2617,10 @@ the messages received by the SMTP server can be reproduced in JSON format. When both the SMTP server and the HTTP server are enabled, the following additional endpoints are made available on the HTTP server: +#### /smtp/gui +A very basic HTML/JavaScript GUI that displays and auto-refreshes the received +messages is made available on the `/smtp/gui` endpoint. + #### /smtp On the `/smtp` endpoint, an index of all received messages will be made available as JSON in the following schema: diff --git a/internal/smtp/gui.html b/internal/smtp/gui.html new file mode 100644 index 0000000..8107a38 --- /dev/null +++ b/internal/smtp/gui.html @@ -0,0 +1,129 @@ + + + + +apitest mock SMTP server GUI + + + + + + + +
+ + +
+

+ Message Metadata: +


+        

+ +

+ Body:
+ Show +

+ +

+ Multipart Metadata: +


+        

+ +

+ Multiparts:
+

+

+
+
+ + + + + diff --git a/internal/smtp/http.go b/internal/smtp/http.go index 4008a2f..3b8c8ca 100644 --- a/internal/smtp/http.go +++ b/internal/smtp/http.go @@ -1,7 +1,9 @@ package smtp import ( + _ "embed" "fmt" + "html/template" "net/http" "path" "regexp" @@ -9,8 +11,13 @@ import ( "strings" "github.com/programmfabrik/apitest/internal/handlerutil" + "github.com/sirupsen/logrus" ) +//go:embed gui.html +var guiTemplateSrc string +var guiTemplate = template.Must(template.New("gui").Parse(guiTemplateSrc)) + type smtpHTTPHandler struct { server *Server prefix string @@ -41,8 +48,14 @@ func (h *smtpHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { pathParts := strings.Split(path, "/") fmt.Println(pathParts) - // We know that pathParts must have at least length 1, since empty path + // We now know that pathParts must have at least length 1, since empty path // was already handled above. + + if pathParts[0] == "gui" { + h.handleGUI(w, r) + return + } + idx, err := strconv.Atoi(pathParts[0]) if err != nil { handlerutil.RespondWithErr( @@ -90,6 +103,15 @@ func (h *smtpHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) } +func (h *smtpHTTPHandler) handleGUI(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + + err := guiTemplate.Execute(w, map[string]any{"prefix": h.prefix}) + if err != nil { + logrus.Error("error rendering GUI:", err) + } +} + func (h *smtpHTTPHandler) handleMessageIndex(w http.ResponseWriter, r *http.Request) { var receivedMessages []*ReceivedMessage