-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (50 loc) · 1.13 KB
/
main.go
File metadata and controls
61 lines (50 loc) · 1.13 KB
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
package main
import (
"net/http"
"strings"
"github.com/andrewmeissner/react-gin-binary/ui"
assetfs "github.com/elazarl/go-bindata-assetfs"
"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, 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
}
// BinaryFileSystem ...
func BinaryFileSystem(root string) *binaryFileSystem {
return &binaryFileSystem{
fs: &assetfs.AssetFS{
Asset: ui.Asset,
AssetDir: ui.AssetDir,
AssetInfo: ui.AssetInfo,
Prefix: root,
},
}
}
func main() {
router := gin.Default()
router.StaticFS("/ui", BinaryFileSystem("ui/build"))
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusPermanentRedirect, "/ui")
})
api := router.Group("/api")
{
api.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
}
router.Run(":8080")
}