yt-gen-app/lib/templ/tmpl/backend/route/route.tmpl

49 lines
1.1 KiB
Cheetah

package route
import (
"embed"
"mime"
"net/http"
"path/filepath"
"strings"
"zmap/route/api"
"github.com/labstack/echo/v4"
_ "zmap/docs"
echoSwagger "github.com/swaggo/echo-swagger"
)
//go:embed static
var Content embed.FS
func Init(e *echo.Echo) {
e.GET("/swagger/*", echoSwagger.WrapHandler)
api.Init(e.Group("/api"))
//e.GET("/", main)
e.GET("/*", defaultHandler)
e.GET("*", defaultHandler)
}
func defaultHandler(c echo.Context) error {
buff, err := Content.ReadFile(strings.ReplaceAll("static/"+c.Request().URL.Path, "//", "/"))
if err != nil {
buff, err = Content.ReadFile("static/index.html")
if err != nil {
return c.String(http.StatusNotFound, "Page not found")
}
return c.Blob(http.StatusOK, http.DetectContentType(buff), buff)
}
contentType := http.DetectContentType(buff)
tmpArr := strings.Split(contentType, ";")
if len(tmpArr) > 0 && strings.Trim(tmpArr[0], " ") == "text/plain" {
ext1 := filepath.Ext(c.Request().URL.Path)
contentType = mime.TypeByExtension(ext1)
}
return c.Blob(http.StatusOK, contentType, buff)
}