ginpackr

package
v0.0.0-...-896f4a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2019 License: MIT Imports: 4 Imported by: 0

README

ginpackr

bind packr2 to gin-gonic/gin, to embed static file inside golang webserv

usage

0. before

assume code struct in /User/tsingson/go/src/github.com/tsingson/ginpackr-example/

./main.go
./public
./public/css
./public/js
./public/index.html
....

1. get packr2
cd /User/tsingson/go/src/github.com/tsingson/ginpackr-example/
go mod init
go get -u github.com/gobuffalo/packr/v2/...
go get -u github.com/gobuffalo/packr/v2/packr2

2. code struct
./main.go
./public
./public/css
./public/js
./public/index.html
....

3. use ginpackr in gin

in main.go ( in /User/tsingson/go/src/github.com/tsingson/ginpackr-example/)

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gobuffalo/packr/v2"
	"github.com/tsingson/packr/ginpackr"
	...
)

func main() {

	box := packr.NewBox( "./public") // static HTML file here 
	r := gin.Default()
	r.Use(ginpackr.PackrServe("/", box)) // use packr2 box ...

	r.Static("/css", "public/css")
	r.Static("/js", "public/js")
	r.Run()
}

4. run it in develop moe
cd /User/tsingson/go/src/github.com/tsingson/ginpackr-example/
go run ./main.go

5. build it
cd /User/tsingson/go/src/github.com/tsingson/ginpackr-example/
pack2 install .

inspired by gin-static

MIT license

see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LocalFile

func LocalFile(box *packr.Box, indexes bool) *localFileSystem

func PackrServe

func PackrServe(urlPrefix string, box *packr.Box) gin.HandlerFunc

Static returns a middleware handler that serves static files in the given directory.

Types

type ServeFileSystem

type ServeFileSystem interface {
	http.FileSystem
	Exists(prefix string, path string) bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL