viper-app/internal/server/web.go

43 lines
857 B
Go
Raw Normal View History

2023-02-03 16:18:31 +08:00
package server
import (
"crypto/md5"
"embed"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"packet-ui/pkg"
"strings"
)
/*
embed static web learn from opensource license MIT
see "github.com/v2rayA/v2rayA/server/router/index.go"
*/
// webRootFS 嵌入静态文件
//
//go:embed web
var webRootFS embed.FS
// html 缓存 HTML 页面
func html(html []byte) func(ctx *gin.Context) {
etag := fmt.Sprintf("W/%x", md5.Sum(html))
h := pkg.B2S(html)
return func(ctx *gin.Context) {
if ctx.IsAborted() {
return
}
ctx.Header("Content-Type", "text/html; charset=utf-8")
ctx.Header("Cache-Control", "public, must-revalidate")
ctx.Header("ETag", etag)
if match := ctx.GetHeader("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
ctx.Status(http.StatusNotModified)
return
}
}
ctx.String(http.StatusOK, h)
}
}