Skip to content

Instantly share code, notes, and snippets.

@Aladex
Last active May 27, 2020 18:06
Show Gist options
  • Save Aladex/0a8db1ddf0aae999601d332019fb7a5e to your computer and use it in GitHub Desktop.
Save Aladex/0a8db1ddf0aae999601d332019fb7a5e to your computer and use it in GitHub Desktop.
GinGonic self-hosted SPA for bindata
go-bindata -o cmd/bindata.go -fs -prefix "app" dist/...
go build -i -o bin/zpix cmd/*
package main
import (
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strings"
)
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,
}
}
func main() {
route := gin.Default()
route.Use(static.Serve("/app", BinaryFileSystem("dist/")))
route.NoRoute(func(c *gin.Context) {
data, err := Asset("dist/index.html")
if err != nil {
log.Fatalln(err)
}
// Write asset
c.Data(200, "text/html", data)
})
err := route.Run("0.0.0.0:8080")
if err != nil {
log.Fatalln(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment