google-app-engine - 第三方路由器和静态文件

标签 google-app-engine routing go

我在 Google App Engine 上使用第三方路由器 ( httprouter ),并希望从根目录提供静态文件。

由于 App Engine,我需要将第三方路由器附加到 / 上的 DefaultServeMux:

router := httprouter.New()

// Doesn't work, duplicated "/".
http.Handle("/", http.FileServer(http.Dir("public")))

// Needed because of App Engine.
http.Handle("/", router)

问题是这会重复 / 模式并出现“multiple registrations for/”的 panic

如何从 root 提供文件,尤其是 index.html 并使用第三方路由器?

最佳答案

如果您在 / 提供静态文件,那么您不能根据 https://github.com/julienschmidt/httprouter/issues/7#issuecomment-45725482 提供任何其他路径。

You can't register a "catch all" at the root dir for serving files while also registering other handlers at sub-paths. See also the note at https://github.com/julienschmidt/httprouter#named-parameters

您应该使用 Go 在应用程序根目录提供模板,在子路径提供静态文件(CSS、JS 等):

router := httprouter.New()

router.GET("/", IndexHandler)
// Ripped straight from the httprouter docs
router.ServeFiles("/static/*filepath", http.Dir("/srv/www/public/"))

http.Handle("/", router)

关于google-app-engine - 第三方路由器和静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24849844/

相关文章:

c# - 从 Razor View 生成区域中 Razor 页面的 URL

asp.net-mvc - asp.net mvc 路由 : how to use default action but non-default parameter?

function - 调用函数而不考虑其参数类型

python - GAE 中的 session 以逗号分隔

java - 无法在应用引擎中保留 jpa 实体

python - 将结构化属性添加到 NDB 中的模型

php - 域路由错误 Laravel 中缺少必需的参数

git - 无法安装私有(private) Go 模块

go - 所有 Go 项目都需要在 GOPATH 下运行吗?

python - 如何在 (Python) Google App Engine 数据源上正确执行一对多联接?