zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    【JavaScript】浅析IIFE(立即执行函数表达式)的作用
    【HTML】HTML之marquee详解
    【jsp】jsp中的动作元素
    【Spring】SpringMVC之基于注解的实现SpringMVC+MySQL
    【java】Java泛型
    【JavaScript】javascript中伪协议(javascript:)使用探讨
    【JavaScript】innerHTML、innerText和outerHTML的用法区别
    【HTML】Html页面跳转的5种方式
    【Spring】SpringMVC中浅析数据的传递方式
    【Spring】SpringMVC非注解配置的两种方式
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/13371973.html
Copyright © 2011-2022 走看看