Skip to content

Instantly share code, notes, and snippets.

@rodesousa
Last active February 11, 2018 20:55
Show Gist options
  • Select an option

  • Save rodesousa/94818f68cd91b31229f8c7b6bb54785c to your computer and use it in GitHub Desktop.

Select an option

Save rodesousa/94818f68cd91b31229f8c7b6bb54785c to your computer and use it in GitHub Desktop.
bynary_asset_with_gin #golang
package main
import (
"net/http"
"strings"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/rdesousa/dashboard-gitlabv2/api"
)
func BinaryFileSystem(root string) *binaryFileSystem {
fs := &assetfs.AssetFS{Asset, AssetDir, AssetInfo, root}
return &binaryFileSystem{
fs,
}
}
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 main() {
r := gin.Default()
// r.LoadHTMLFiles("frontend/build/index.html")
r.Use(static.Serve("/", BinaryFileSystem("frontend/build")))
// r.GET("/", func(c *gin.Context) {
// c.HTML(http.StatusOK, "index.html", gin.H{})
// })
r.GET("/api/merge_request", api.Merge_Request)
r.GET("/api/test", func(c *gin.Context) {
c.JSON(200, gin.H{"aze": "aze"})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment