Files
opsxdb/server/internal/site/site.go
T

59 lines
1.3 KiB
Go

// Package site:产品官网落地页(嵌入二进制,深色密度跟工作台)。
package site
import (
"embed"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
//go:embed template.html assets
var files embed.FS
type Handler struct {
html string
}
func New() *Handler {
raw, err := files.ReadFile("template.html")
if err != nil {
panic("site: template.html 缺失: " + err.Error())
}
return &Handler{html: string(raw)}
}
func (h *Handler) Register(r *gin.Engine) {
r.GET("/", h.Page)
r.GET("/home", h.Page)
r.GET("/index.html", h.Page)
r.GET("/site/assets/*file", h.Asset)
}
func (h *Handler) Page(c *gin.Context) {
c.Header("Content-Type", "text/html; charset=utf-8")
c.Header("Cache-Control", "no-cache")
_, _ = c.Writer.WriteString(h.html)
}
func (h *Handler) Asset(c *gin.Context) {
name := strings.TrimPrefix(c.Param("file"), "/")
if strings.Contains(name, "..") {
c.Status(http.StatusBadRequest)
return
}
data, err := files.ReadFile("assets/" + name)
if err != nil {
c.Status(http.StatusNotFound)
return
}
ct := "application/octet-stream"
if strings.HasSuffix(name, ".css") {
ct = "text/css; charset=utf-8"
}
c.Header("Content-Type", ct)
c.Header("Cache-Control", "public, max-age=3600")
c.Data(http.StatusOK, ct, data)
}