SimpleCloudNotifier/server/api/handler/website.go

120 lines
3.1 KiB
Go
Raw Normal View History

2022-11-18 21:25:40 +01:00
package handler
import (
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/website"
2022-11-21 22:52:44 +01:00
"errors"
2022-11-18 21:25:40 +01:00
"github.com/gin-gonic/gin"
"net/http"
2022-11-21 22:52:44 +01:00
"regexp"
2022-11-18 21:25:40 +01:00
"strings"
)
type WebsiteHandler struct {
2022-11-21 22:52:44 +01:00
app *logic.Application
rexTemplate *regexp.Regexp
2022-11-18 21:25:40 +01:00
}
func NewWebsiteHandler(app *logic.Application) WebsiteHandler {
return WebsiteHandler{
2022-11-21 22:52:44 +01:00
app: app,
rexTemplate: regexp.MustCompile("{{template\\|[A-Za-z0-9_\\-.]+}}"),
2022-11-18 21:25:40 +01:00
}
}
func (h WebsiteHandler) Index(g *gin.Context) ginresp.HTTPResponse {
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "index.html", true)
2022-11-18 21:25:40 +01:00
}
func (h WebsiteHandler) APIDocs(g *gin.Context) ginresp.HTTPResponse {
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "api.html", true)
2022-11-18 21:25:40 +01:00
}
func (h WebsiteHandler) APIDocsMore(g *gin.Context) ginresp.HTTPResponse {
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "api_more.html", true)
2022-11-18 21:25:40 +01:00
}
func (h WebsiteHandler) MessageSent(g *gin.Context) ginresp.HTTPResponse {
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "message_sent.html", true)
2022-11-18 21:25:40 +01:00
}
func (h WebsiteHandler) FaviconIco(g *gin.Context) ginresp.HTTPResponse {
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "favicon.ico", false)
2022-11-18 21:25:40 +01:00
}
func (h WebsiteHandler) FaviconPNG(g *gin.Context) ginresp.HTTPResponse {
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "favicon.png", false)
2022-11-18 21:25:40 +01:00
}
func (h WebsiteHandler) Javascript(g *gin.Context) ginresp.HTTPResponse {
type uri struct {
Filename string `uri:"fn"`
}
var u uri
if err := g.ShouldBindUri(&u); err != nil {
return ginresp.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "js/"+u.Filename, false)
2022-11-18 21:25:40 +01:00
}
func (h WebsiteHandler) CSS(g *gin.Context) ginresp.HTTPResponse {
type uri struct {
Filename string `uri:"fn"`
}
var u uri
if err := g.ShouldBindUri(&u); err != nil {
return ginresp.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
2022-11-21 22:52:44 +01:00
return h.serveAsset(g, "css/"+u.Filename, false)
2022-11-18 21:25:40 +01:00
}
2022-11-21 22:52:44 +01:00
func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginresp.HTTPResponse {
2022-11-18 21:25:40 +01:00
data, err := website.Assets.ReadFile(fn)
if err != nil {
return ginresp.Status(http.StatusNotFound)
}
2022-11-21 22:52:44 +01:00
if repl {
failed := false
data = h.rexTemplate.ReplaceAllFunc(data, func(match []byte) []byte {
prefix := len("{{template|")
suffix := len("}}")
fnSub := match[prefix : len(match)-suffix]
subdata, err := website.Assets.ReadFile(string(fnSub))
if err != nil {
failed = true
}
return subdata
})
if failed {
return ginresp.InternalError(errors.New("template replacement failed"))
}
}
2022-11-18 21:25:40 +01:00
mime := "text/plain"
lowerFN := strings.ToLower(fn)
if strings.HasSuffix(lowerFN, ".html") || strings.HasSuffix(lowerFN, ".htm") {
mime = "text/html"
} else if strings.HasSuffix(lowerFN, ".css") {
mime = "text/css"
} else if strings.HasSuffix(lowerFN, ".js") {
mime = "text/javascript"
} else if strings.HasSuffix(lowerFN, ".json") {
mime = "application/json"
} else if strings.HasSuffix(lowerFN, ".jpeg") || strings.HasSuffix(lowerFN, ".jpg") {
mime = "image/jpeg"
} else if strings.HasSuffix(lowerFN, ".png") {
mime = "image/png"
} else if strings.HasSuffix(lowerFN, ".svg") {
mime = "image/svg+xml"
}
return ginresp.Data(http.StatusOK, mime, data)
}