-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-gin.go
69 lines (55 loc) · 1.63 KB
/
init-gin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package webmgmt
import (
"fmt"
"github.com/alexj212/gox/ginx"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"io/fs"
"log"
"net/http"
"strings"
)
//InitGin will initialize the Router with the admin web app. It registers the webapp and assets file handler
// to be under the WebPath config field.
func InitGin(app *MgmtApp, router *gin.Engine, webPath string, fileSystem fs.FS) error {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
// loge.Info("app.Config.WebPath: %v\n", app.webPath)
router.GET(webPath+"ws", func(c *gin.Context) {
// loge.Info("/ws invoked")
serveWs(app, c.Writer, c.Request)
})
router.GET(webPath+"/version", func(c *gin.Context) {
app.handleServerVersion(c.Writer, c.Request)
})
if fileSystem != nil {
//newFS := &NewPathFS{
// base: app.fileSystem,
// prefix: app.webPath,
//}
router.Use(ginx.Serve(webPath, ginx.StaticFS(fileSystem, webPath, true)))
} else {
log.Printf("unable to set file src or proxy\n")
return errors.Errorf("unable to set file src or proxy")
}
return nil
}
func ServeProxy(app *MgmtApp, webPath string, filesProxy http.Handler) gin.HandlerFunc {
handler := http.StripPrefix(webPath, filesProxy)
return func(c *gin.Context) {
handler.ServeHTTP(c.Writer, c.Request)
c.Abort()
}
}
type NewPathFS struct {
base fs.FS
prefix string
}
func (p *NewPathFS) Open(name string) (fs.File, error) {
if !strings.HasPrefix(name, p.prefix) {
fmt.Printf("ERROR Open: %v prefix: %v\n", name, p.prefix)
return nil, errors.Errorf("unable to open file: %v", name)
}
name = name[len(p.prefix):]
fmt.Printf("Open: %v\n", name)
return nil, nil
}