embedfs

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: MIT Imports: 5 Imported by: 0

README

embedfs

GoDoc Go.Dev reference Build Status codecov Action Status Go Report Card License Tag

Installation

    go get github.com/things-go/embedfs

Example

package main

import (
	"io/fs"
	"net/http"

	"github.com/gin-gonic/gin"

	"github.com/things-go/embedfs"
	"github.com/things-go/embedfs/testdata"
)

func main() {
	r := gin.Default()

	// 静态html文件
	embedfs.HTML(r, embedfs.WWW{
		EmbedFs:          testdata.Staticfs,
		EmbedTplPatterns: []string{"static/index.html", "static/views/*"},
		TplPatterns:      []string{"testdata/static/ixx.html"},
		RelativePathToTpl: map[string]string{
			"/":      "index.html",
			"/index": "index.html",
			"/hello": "hello.html",
			"/ioo":   "ioo.html",
			"/ixx":   "ixx.html",
		},
	})

	// 系统文件
	r.StaticFS("/xss", http.Dir("testdata/static/css"))
	r.StaticFS("/xmg", http.Dir("testdata/static/img"))
	r.StaticFile("/5.png", "testdata/static/5.png")
	r.StaticFile("/6.jpg", "testdata/static/views/6.jpg")
	// 嵌入绑定的文件
	cssFs, _ := fs.Sub(testdata.Staticfs, "static/css")
	r.StaticFS("/css", http.FS(cssFs))
	imgFs, _ := fs.Sub(testdata.Staticfs, "static/css")
	r.StaticFS("/img", http.FS(imgFs))
	embedfs.StaticFileFS(r, "/1.png", "static/1.png", http.FS(testdata.Staticfs))
	embedfs.StaticFileFS(r, "/4.png", "static/views/4.png", http.FS(testdata.Staticfs))

	err := r.Run(":9000")
	if err != nil {
		panic(err)
	}
}

License

This project is under MIT License. See the LICENSE file for the full license text.

Donation

if package help you a lot,you can support us by:

Alipay

alipay

WeChat Pay

wxpay

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTML

func HTML(engine *gin.Engine, c WWW) *gin.Engine

HTML 静态页面处理 只可以设一次静态文件

func StaticFileFS

func StaticFileFS(r gin.IRouter, relativePath, filepath string, fs http.FileSystem) gin.IRouter

StaticFileFS works just like `StaticFile()` but a custom `http.FileSystem` can be used instead. Gin by default user: gin.Dir()

Types

type WWW

type WWW struct {
	EmbedFs           fs.FS             // 如果不为nil使用embed fs
	EmbedTplPatterns  []string          // embed fs模板patterns, see: template的ParseFS和ParseGlob
	TplPatterns       []string          // 文件模板patterns, see: template的ParseFS和ParseGlob
	RelativePathToTpl map[string]string // url相对路径对模板名映射
}

WWW HTML config

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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