Add Go/Postgres admin APIs, Angular admin UI, manual build flow, asset uploads, markdown import/export, configurable slug generation, and the Yar reading theme. Exclude local docs and generated development artifacts from version control.
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package admin
|
|
|
|
import (
|
|
"embed"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
//go:embed web/*
|
|
var adminWeb embed.FS
|
|
|
|
func adminIndex(adminDir string) ([]byte, error) {
|
|
if strings.TrimSpace(adminDir) != "" {
|
|
path := filepath.Join(adminDir, "index.html")
|
|
if page, err := os.ReadFile(path); err == nil {
|
|
return rewriteAdminBase(page), nil
|
|
}
|
|
}
|
|
return adminWeb.ReadFile("web/index.html")
|
|
}
|
|
|
|
func serveAdminFile(c *gin.Context, adminDir string) bool {
|
|
if strings.TrimSpace(adminDir) == "" {
|
|
return false
|
|
}
|
|
|
|
requested := strings.TrimPrefix(c.Param("filepath"), "/")
|
|
if requested == "" {
|
|
return false
|
|
}
|
|
cleaned := filepath.Clean(requested)
|
|
if cleaned == "." || strings.HasPrefix(cleaned, "..") {
|
|
return false
|
|
}
|
|
|
|
path := filepath.Join(adminDir, cleaned)
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.IsDir() {
|
|
return false
|
|
}
|
|
http.ServeFile(c.Writer, c.Request, path)
|
|
return true
|
|
}
|
|
|
|
func rewriteAdminBase(page []byte) []byte {
|
|
return []byte(strings.Replace(string(page), `<base href="/">`, `<base href="/admin/">`, 1))
|
|
}
|