1
0
mirror of https://github.com/axllent/mailpit.git synced 2024-12-28 23:06:43 +02:00

Feature: Add URL redirect (/view/latest) to view latest message in web UI (#166)

This commit is contained in:
Ralph Slooten 2023-11-02 16:15:45 +13:00
parent baa9f3be0b
commit ffe6167d96
2 changed files with 25 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
@ -11,6 +12,27 @@ import (
"github.com/gorilla/mux"
)
// RedirectToLatestMessage (method: GET) redirects the web UI to the latest message
func RedirectToLatestMessage(w http.ResponseWriter, r *http.Request) {
messages, err := storage.List(0, 1)
if err != nil {
httpError(w, err.Error())
return
}
uri := config.Webroot
if len(messages) == 1 {
uri, err = url.JoinPath(uri, "/view/"+messages[0].ID)
if err != nil {
httpError(w, err.Error())
return
}
}
http.Redirect(w, r, uri, 302)
}
// GetMessageHTML (method: GET) returns a rendered version of a message's HTML part
func GetMessageHTML(w http.ResponseWriter, r *http.Request) {
// swagger:route GET /view/{ID}.html testing GetMessageHTML

View File

@ -68,6 +68,9 @@ func Listen() {
r.HandleFunc(redirect, middleWareFunc(addSlashToWebroot)).Methods("GET")
}
// UI shortcut
r.HandleFunc(config.Webroot+"view/latest", handlers.RedirectToLatestMessage).Methods("GET")
// frontend testing
r.HandleFunc(config.Webroot+"view/{id}.html", handlers.GetMessageHTML).Methods("GET")
r.HandleFunc(config.Webroot+"view/{id}.txt", handlers.GetMessageText).Methods("GET")