2022-11-13 19:17:07 +01:00
|
|
|
package swagger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
|
|
|
"embed"
|
|
|
|
_ "embed"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-11-30 16:46:55 +01:00
|
|
|
//go:embed *.html
|
|
|
|
//go:embed *.json
|
|
|
|
//go:embed *.yaml
|
|
|
|
//go:embed *.js
|
|
|
|
//go:embed *.css
|
|
|
|
//go:embed themes/*
|
2022-11-13 19:17:07 +01:00
|
|
|
var assets embed.FS
|
|
|
|
|
|
|
|
func getAsset(fn string) ([]byte, string, bool) {
|
|
|
|
data, err := assets.ReadFile(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
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 data, mime, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Handle(g *gin.Context) ginresp.HTTPResponse {
|
|
|
|
type uri struct {
|
2022-11-30 16:46:55 +01:00
|
|
|
Filename1 string `uri:"fn1"`
|
|
|
|
Filename2 *string `uri:"fn2"`
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
|
|
|
if err := g.ShouldBindUri(&u); err != nil {
|
|
|
|
return ginresp.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:46:55 +01:00
|
|
|
filename := u.Filename1
|
|
|
|
if u.Filename2 != nil {
|
|
|
|
filename = filename + "/" + *u.Filename2
|
|
|
|
}
|
|
|
|
|
|
|
|
if filename == "" {
|
2022-11-13 19:17:07 +01:00
|
|
|
index, _, _ := getAsset("index.html")
|
|
|
|
return ginresp.Data(http.StatusOK, "text/html", index)
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:46:55 +01:00
|
|
|
if data, mime, ok := getAsset(filename); ok {
|
2022-11-13 19:17:07 +01:00
|
|
|
return ginresp.Data(http.StatusOK, mime, data)
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:46:55 +01:00
|
|
|
return ginresp.JSON(http.StatusNotFound, gin.H{"error": "AssetNotFound", "filename": filename, "filename1": u.Filename1, "filename2": u.Filename2})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|