2024-03-28 20:52:52 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Masterminds/sprig/v3"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
fiberhtml "github.com/gofiber/template/html/v2"
|
2024-04-26 08:34:50 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/schema"
|
2024-03-28 20:52:52 +00:00
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed views/*
|
|
|
|
var viewsfs embed.FS
|
|
|
|
|
|
|
|
func notFoundHandler(c *fiber.Ctx) error {
|
|
|
|
// Check if the request accepts JSON
|
|
|
|
if string(c.Context().Request.Header.ContentType()) == "application/json" || len(c.Accepts("html")) == 0 {
|
|
|
|
// The client expects a JSON response
|
2024-06-24 06:34:36 +00:00
|
|
|
return c.Status(fiber.StatusNotFound).JSON(schema.ErrorResponse{
|
2024-03-28 20:52:52 +00:00
|
|
|
Error: &schema.APIError{Message: "Resource not found", Code: fiber.StatusNotFound},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// The client expects an HTML response
|
2024-06-24 06:34:36 +00:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("views/404", fiber.Map{})
|
2024-03-28 20:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderEngine() *fiberhtml.Engine {
|
|
|
|
engine := fiberhtml.NewFileSystem(http.FS(viewsfs), ".html")
|
|
|
|
engine.AddFuncMap(sprig.FuncMap())
|
|
|
|
engine.AddFunc("MDToHTML", markDowner)
|
|
|
|
return engine
|
|
|
|
}
|
|
|
|
|
|
|
|
func markDowner(args ...interface{}) template.HTML {
|
|
|
|
s := blackfriday.MarkdownCommon([]byte(fmt.Sprintf("%s", args...)))
|
2024-04-26 08:34:50 +00:00
|
|
|
return template.HTML(bluemonday.UGCPolicy().Sanitize(string(s)))
|
2024-03-28 20:52:52 +00:00
|
|
|
}
|