使用vfsgen 嵌入靜態資源到golang


vfsgen 是一個很不錯的golang 靜態資源嵌入工具包,使用簡單,功能強大,以下是一個簡單的使用

環境准備

  • docker-compose 文件

    主要是方便構建以及運行

version: "3"
services:
   app:
     build: ./
     image: dalongrong/vfsgen-app
     ports:
     - "9090:9090"
  • vfsgen 試用
    主要是基於vfsgen提供的包,調用方便,方便生成代碼可以直接調用的包(嵌入的資源)
    項目結構
    dist 為生成的嵌入靜態資源的包,cmd 為生成的嵌入資源的代碼調用,項目根的main.go為一個httpserver 主要是調用
    生成的嵌入資源的包,resource 為一些環境
 
├── Dockerfile
├── README.md
├── cmd
│   └── main.go
├── dist
│   └── mydir-dist.go
├── docker-compose.yaml
├── go.mod
├── go.sum
├── main.go
└── resource
    ├── dev
    │   ├── css
    │   │   └── index.css
    │   ├── fs
    │   ├── index.html
    │   └── js
    │       └── index.js
    └── pre
        ├── css
        │   └── index.css
        ├── fs
        ├── index.html
        └── js
            └── index.js

go.mod

module github.com/rongfengliang/demoapp
go 1.14
require (
    github.com/mitchellh/gox v1.0.1 // indirect
    github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
    github.com/shurcooL/vfsgen v0.0.0-20200627165143-92b8a710ab6c // indirect
    golang.org/x/tools v0.0.0-20200724001824-cbb3c69a3747 // indirect
)
 

cmd/main.go

package main
import (
    "log"
    "net/http"
    "github.com/shurcooL/httpfs/union"
    "github.com/shurcooL/vfsgen"
)
func main() {
    // 使用了union可以方便的進行資源合並
    // use union for combin resource
    var Assets = union.New(map[string]http.FileSystem{
        "/login/dev": http.Dir("./resource/dev"),
        "/login/pre": http.Dir("./resource/pre"),
    })
    var fs http.FileSystem = Assets
    err := vfsgen.Generate(fs, vfsgen.Options{
        PackageName:  "dist",
        Filename:     "./dist/mydir-dist.go",
        VariableName: "MyDir",
    })
    if err != nil {
        log.Fatalln(err)
    }
}

main.go

package main
import (
    "net/http"
    "github.com/rongfengliang/demoapp/dist"
)
func main() {
    http.Handle("/login/pre/", http.FileServer(dist.MyDir))
    http.Handle("/login/dev/", http.FileServer(dist.MyDir))
    http.ListenAndServe(":9090", nil)
}
  • dockerfile
    為了方便打包,代碼使用了gox 構建很方便,一個命令就可搞定基本主流平台的打包
 
FROM golang:1.14-alpine AS build-env
WORKDIR /go/src/app
RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
ENV  GO111MODULE=on
ENV  GOPROXY=https://goproxy.cn
RUN go get github.com/mitchellh/gox
COPY go.mod .
COPY go.sum .
COPY main.go .
COPY resource ./resource
COPY dist ./dist
COPY cmd/main.go ./cmd/main.go
RUN apk update && apk add git \
    && go run cmd/main.go && gox
FROM alpine:latest
WORKDIR /app
RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
COPY --from=build-env /go/src/app/demoapp_* /app/
CMD ["/app/demoapp_linux_amd64"]

構建&&運行效果

  • 構建
docker-compose build
  • 啟動
docker-compose up -d

訪問 /login/pre/ /login/dev 地址

 

 

說明

從工具的使用上vfsgen 是比較高效以及簡單的,是一個不錯的靜態資源嵌入工具,而且還支持template操作

參考資料

https://github.com/rongfengliang/vfsgen-learning
https://github.com/shurcooL/vfsgen
https://github.com/mitchellh/gox
https://github.com/laher/goxc


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM