Go:在我的 API 端点旁边提供静态页面(它们的端点)
Go: Serve static pages (their endpoints) along side my API endpoints
所以我遇到了一个问题,我只能让我的 API 端点工作或我的静态文件工作。我正在使用 gorilla/mux 作为我的路由器,它可能与配置有关。
我认为在这种情况下我有四个文件:
main.go
func main() {
envVars()
router := NewRouter()
log.Fatal(http.ListenAndServe(":8080", router))
}
router.go
func NewRouter() *mux.Router {
// create new router
router := mux.NewRouter()
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
return router
}
routes.go
var routes = Routes{
Route{
// TODO: Find way to render static file
"Index",
"GET",
"/",
Index,
},
Route{
"PostIndex",
"GET",
"/api/posts",
PostIndex,
},
Route{
"PostCreate",
"POST",
"/api/posts",
PostCreate,
},
}
handlers.go
func Index(w http.ResponseWriter, r *http.Request) {
// TODO: Something to render html file
}
通过研究,我认为我可以使用子路由器,但似乎应该有更好的方法来做到这一点。对于我所有的其他路线,我已经实现了方法,但为了呈现 html 文件,我不确定该方法中会放什么。
要查看整个项目,请访问:https://github.com/nicholasrucci/blog
更新
我找到了 this source。这和我正在经历的很相似。
已更新
修复错误的代码。仅将文件服务器绑定到根路径,让其他路由自由供其他处理程序使用。
移除routes.go中的Index路由并更新router.go 像这样:
func NewRouter() *mux.Router {
// create new router
router := mux.NewRouter()
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
router.Path("/").Handler(http.FileServer(http.Dir("public/")))
return router
}
这会公开您域根目录下 public 目录中的 index.html 文件。
我最后做的是制作一个模板函数并从那里渲染我的 HTML。
func Index(w http.ResponseWriter, r *http.Request) {
t := template.New("Test")
t, err = t.Parse("<html><body>Hello World</body></html>)
if err != nil {
log.Fatal(err)
}
err = t.Execute(w, t)
if err != nil {
log.Fatal(err)
}
}
所以我遇到了一个问题,我只能让我的 API 端点工作或我的静态文件工作。我正在使用 gorilla/mux 作为我的路由器,它可能与配置有关。
我认为在这种情况下我有四个文件:
main.go
func main() {
envVars()
router := NewRouter()
log.Fatal(http.ListenAndServe(":8080", router))
}
router.go
func NewRouter() *mux.Router {
// create new router
router := mux.NewRouter()
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
return router
}
routes.go
var routes = Routes{
Route{
// TODO: Find way to render static file
"Index",
"GET",
"/",
Index,
},
Route{
"PostIndex",
"GET",
"/api/posts",
PostIndex,
},
Route{
"PostCreate",
"POST",
"/api/posts",
PostCreate,
},
}
handlers.go
func Index(w http.ResponseWriter, r *http.Request) {
// TODO: Something to render html file
}
通过研究,我认为我可以使用子路由器,但似乎应该有更好的方法来做到这一点。对于我所有的其他路线,我已经实现了方法,但为了呈现 html 文件,我不确定该方法中会放什么。
要查看整个项目,请访问:https://github.com/nicholasrucci/blog
更新
我找到了 this source。这和我正在经历的很相似。
已更新 修复错误的代码。仅将文件服务器绑定到根路径,让其他路由自由供其他处理程序使用。
移除routes.go中的Index路由并更新router.go 像这样:
func NewRouter() *mux.Router {
// create new router
router := mux.NewRouter()
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
router.Path("/").Handler(http.FileServer(http.Dir("public/")))
return router
}
这会公开您域根目录下 public 目录中的 index.html 文件。
我最后做的是制作一个模板函数并从那里渲染我的 HTML。
func Index(w http.ResponseWriter, r *http.Request) {
t := template.New("Test")
t, err = t.Parse("<html><body>Hello World</body></html>)
if err != nil {
log.Fatal(err)
}
err = t.Execute(w, t)
if err != nil {
log.Fatal(err)
}
}