go-bindata-assetfs和gorilla mux

时间:2017-05-30 10:05:03

标签: go gorilla

我正在使用我的第一个去网络应用并尝试使用https://github.com/elazarl/go-bindata-assetfs

嵌入文件

这是我的项目结构:

project_dir
├───common
├───routers
└───templates
    ├───images
    └───css

生成了一个源文件:

go-bindata-assetfs -pkg common -prefix "templates/" templates/css/ templates/ templates/images/

现在我有到静态文件的旧路由:

package routers
import (
    "net/http"
    "github.com/gorilla/mux"
)

func SetTemplatesRoutes(router *mux.Router) *mux.Router {
    fs := http.FileServer(http.Dir("templates"))

    router.Handle("/templates/", fs)
    router.PathPrefix("/css/").Handler(
        http.StripPrefix("/css/",
            http.FileServer(http.Dir("templates/css")),
        ))

    router.PathPrefix("/dist/").Handler(
        http.StripPrefix("/dist/",
            http.FileServer(http.Dir("templates/dist")),
        ))

    router.PathPrefix("/images/").Handler(
        http.StripPrefix("/images/",
            http.FileServer(http.Dir("templates/images")),
        ))

    return router
}

我想用嵌入式文件替换,只是为了让html模板绑定css和图像文件。这是我的尝试:

    fs := http.FileServer(
        &assetfs.AssetFS{Asset: common.Asset, AssetDir: common.AssetDir, AssetInfo: common.AssetInfo})
    //
    router.Handle("/", fs)
    router.PathPrefix("/css/").Handler(
        http.StripPrefix("/css/",
            fs,
        ))

    router.PathPrefix("/dist/").Handler(
        http.StripPrefix("/dist/",
            fs,
        ))

    router.PathPrefix("/images/").Handler(
        http.StripPrefix("/images/",
            fs,
        )) 

不起作用。

有什么建议可以解决吗?

0 个答案:

没有答案