英文:
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 {
// 创建新的路由器
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: 找到一种渲染静态文件的方法
"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: 渲染HTML文件的方法
}
通过研究,我认为我可以使用子路由器,但似乎应该有更好的方法来解决这个问题。对于其他所有路由,我已经实现了方法,但是对于渲染HTML文件,我不确定该在该方法中放入什么。
要查看整个项目,请访问:https://github.com/nicholasrucci/blog
更新
我找到了这个来源。它与我遇到的情况类似。
英文:
So I am having an issue where I can only have my API endpoints working or my static files working. I am using gorilla/mux as my router and I it might have something to do with configuring that.
I have four files that I believe matter in this situation:
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
}
From researching I was thinking that I could maybe use a subrouter but it seems like there should be a better way about doing this. For all of my other routes I have implemented methods but for rendering an html file, I am not sure what would be put in that method.
To view the whole project, visit: https://github.com/nicholasrucci/blog
Update
I have found this source. It is similar to what I am going through.
答案1
得分: 1
更新了代码以修复错误。将文件服务器绑定到根路径,使其他路由可以被其他处理程序使用。
在routes.go中删除Index路由,并像这样更新router.go:
func NewRouter() *mux.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
}
这将在公共目录下的根路径下公开index.html文件。
英文:
Updated
code to fix error. Binding file server only to root path, leaving other routes free to be used by other handlers.
Remove the Index route in routes.go and update router.go like this:
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
}
This exposes the index.html file in the public directory under the root of your domain.
答案2
得分: 0
我最终做的是创建一个模板函数,并从该函数中渲染我的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)
}
}
英文:
What I ended up doing was making a template function and rendering my HTML from there.
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论