viper-app/internal/server/router.go
2023-02-03 16:18:31 +08:00

67 lines
1.3 KiB
Go

package server
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/vearutop/statigz"
"go.uber.org/zap"
"io"
"io/fs"
"net/http"
)
func GUI(s *Server) {
r := s.Engine
webFS, err := fs.Sub(webRootFS, "web")
if err != nil {
zap.S().Panic("init gui err, cause: ", err)
}
guiStaticResource(r, webFS)
guiIndexHTML(r, webFS)
}
func guiStaticResource(r *gin.Engine, sfs fs.FS) {
const (
staticPrefix = "/assets"
stripPrefix = ""
)
staticResource := http.StripPrefix(stripPrefix, statigz.FileServer(sfs.(fs.ReadDirFS)))
r.GET(staticPrefix+"/*w", func(c *gin.Context) {
staticResource.ServeHTTP(c.Writer, c.Request)
})
r.GET("/favicon.ico", func(c *gin.Context) {
staticResource.ServeHTTP(c.Writer, c.Request)
})
}
func guiIndexHTML(r *gin.Engine, sfs fs.FS) {
var indexHTML []byte
{
const indexFileName = "index.html"
index, err := sfs.Open(indexFileName)
if err != nil {
zap.S().Panicf("open [%s] err! cause: %e", indexFileName, err)
}
indexHTML, err = io.ReadAll(index)
if err != nil {
zap.S().Panicf("read [%s] err! cause: %e", indexFileName, err)
}
}
r.GET("/", html(indexHTML))
}
func API(s *Server) {
r := s.Engine
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
apiGroup := r.Group("/api")
{
v1 := apiGroup.Group("/v1")
_ = v1
}
}