2022-09-26 03:04:07 +00:00
|
|
|
package webserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-09-26 06:54:32 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-09-26 03:04:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate cp -r ../../web/dist ./
|
|
|
|
//go:embed dist
|
|
|
|
var staticFiles embed.FS
|
|
|
|
|
2022-09-26 06:54:32 +00:00
|
|
|
func staticFileHandler() gin.HandlerFunc {
|
|
|
|
sf, err := fs.Sub(staticFiles, "dist")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal("compile error: ", err)
|
2022-09-26 03:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 06:54:32 +00:00
|
|
|
fs := http.FileServer(http.FS(sf))
|
|
|
|
|
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
defer ctx.Abort()
|
|
|
|
filename := strings.TrimLeft(ctx.Request.RequestURI, "/")
|
|
|
|
|
|
|
|
if filename == "" {
|
|
|
|
filename = "index.html"
|
2022-09-26 03:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 06:54:32 +00:00
|
|
|
fs.ServeHTTP(ctx.Writer, ctx.Request)
|
|
|
|
}
|
2022-09-26 03:04:07 +00:00
|
|
|
}
|