-
Notifications
You must be signed in to change notification settings - Fork 0
/
staticserve.go
50 lines (42 loc) · 1.08 KB
/
staticserve.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
package main
//go:generate go-bindata -ignore=ui/node_modules -ignore=ui/src -prefix "ui/" -pkg main -o bindata.go ui/...
//go:generate npm --prefix ui/ run build
import (
"net/http"
"strings"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
)
type binaryFileSystem struct {
fs http.FileSystem
}
func (b *binaryFileSystem) Open(name string) (http.File, error) {
return b.fs.Open(name)
}
func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
if _, err := b.fs.Open(p); err != nil {
return false
}
return true
}
return false
}
func BinaryFileSystem(root string) *binaryFileSystem {
fs := &assetfs.AssetFS{Asset, AssetDir, AssetInfo, root}
return &binaryFileSystem{
fs,
}
}
// Usage
// $ go-bindata data/
// $ go build && ./bindata
//
func StaticServe(r *gin.Engine) {
r.Use(static.Serve("/", BinaryFileSystem("")))
r.GET("/ping", func(c *gin.Context) {
c.String(200, "test")
})
// Listen and Server in 0.0.0.0:8080
}