...

Source file src/github.com/gin-gonic/contrib/static/example/bindata/example.go

Documentation: github.com/gin-gonic/contrib/static/example/bindata

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	assetfs "github.com/elazarl/go-bindata-assetfs"
     8  	"github.com/gin-gonic/contrib/static"
     9  	"github.com/gin-gonic/gin"
    10  )
    11  
    12  type binaryFileSystem struct {
    13  	fs http.FileSystem
    14  }
    15  
    16  func (b *binaryFileSystem) Open(name string) (http.File, error) {
    17  	return b.fs.Open(name)
    18  }
    19  
    20  func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
    21  
    22  	if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
    23  		if _, err := b.fs.Open(p); err != nil {
    24  			return false
    25  		}
    26  		return true
    27  	}
    28  	return false
    29  }
    30  
    31  func BinaryFileSystem(root string) *binaryFileSystem {
    32  	fs := &assetfs.AssetFS{Asset, AssetDir, AssetInfo, root}
    33  	return &binaryFileSystem{
    34  		fs,
    35  	}
    36  }
    37  
    38  // Usage
    39  // $ go-bindata data/
    40  // $ go build && ./bindata
    41  //
    42  func main() {
    43  	r := gin.Default()
    44  
    45  	r.Use(static.Serve("/static", BinaryFileSystem("data")))
    46  	r.GET("/ping", func(c *gin.Context) {
    47  		c.String(200, "test")
    48  	})
    49  	// Listen and Server in 0.0.0.0:8080
    50  	r.Run(":8080")
    51  }
    52  

View as plain text