FileServe和Routing的配合更流畅

More fluent cooperation of FileServe and Routing

我有一个问题,我想在 FileServe / 上提供我的主要 AngularJS(Yeoman 部署)应用程序文件夹,但它会破坏我所有的路由器绑定。有什么办法可以保留它们并保持我的路线完整吗?

在下面的代码中,我仍然需要转到 /app 并重新绑定其他文件夹,因为我还不想对 Grunt 文件进行太多调整,所以添加了一些额外的文件夹的备份路径绑定。

func initializeRoutes() {
    // Handle all requests by serving a file of the same name
    fileHandler := http.FileServer(http.Dir(*clFlagStaticDirectory))
    bowerFileHandler := http.FileServer(http.Dir("../bower_components"))
    imagesFileHandler := http.FileServer(http.Dir("../app/images"))
    scriptsFileHandler := http.FileServer(http.Dir("../app/scripts"))
    stylesFileHandler := http.FileServer(http.Dir("../app/styles"))
    viewsFileHandler := http.FileServer(http.Dir("../app/views"))

    // Setup routes
    mainRoute := mux.NewRouter()
    mainRoute.StrictSlash(true)
    // mainRoute.Handle("/", http.RedirectHandler("/static/", 302))
    mainRoute.PathPrefix("/app").Handler(http.StripPrefix("/app", fileHandler))
    mainRoute.PathPrefix("/app/bower_components").Handler(http.StripPrefix("/bower_components", bowerFileHandler))
    mainRoute.PathPrefix("/bower_components").Handler(http.StripPrefix("/bower_components", bowerFileHandler))
    mainRoute.PathPrefix("/images").Handler(http.StripPrefix("/images", imagesFileHandler))
    mainRoute.PathPrefix("/scripts").Handler(http.StripPrefix("/scripts", scriptsFileHandler))
    mainRoute.PathPrefix("/styles").Handler(http.StripPrefix("/styles", stylesFileHandler))
    mainRoute.PathPrefix("/views").Handler(http.StripPrefix("/views", viewsFileHandler))

    // Basic routes
    // User routes
    userRoute := mainRoute.PathPrefix("/users").Subrouter()
    userRoute.Handle("/login", handler(userDoLogin)).Methods("POST")
    userRoute.Handle("/logout", handler(userDoLogout)).Methods("GET")
    userRoute.Handle("/forgot_password", handler(forgotPassword)).Methods("POST")

    // Bind API Routes
    apiRoute := mainRoute.PathPrefix("/api").Subrouter()

    apiProductModelRoute := apiRoute.PathPrefix("/productmodels").Subrouter()
    apiProductModelRoute.Handle("/", handler(listProductModels)).Methods("GET")
    apiProductModelRoute.Handle("/{id}", handler(getProductModel)).Methods("GET")

    // Bind generic route
    http.Handle("/", mainRoute)
}

所以我的目标是服务 /app 作为我的 / 主要路径,但保留我所有的 Mux 路由以赢得 FileServe。因此,如果我有一个 /app/users/login 文件夹,它不会加载,而是让路由器获胜。

注意:我的服务完全超过 HTTPS,没有超过 HTTP

非常感谢!这让我伤透了脑筋,这是我完全开始编写前端代码之前需要弄清楚的最后一件事 :)。

我觉得您想更改路由的评估顺序,使 /users/login 和类似的方法在 / 之前匹配。 / 应该是 FileServer 的服务器。

据我所知,路由将按照它们定义的顺序(添加到路由器)进行匹配。所以你只需要在 /.

之前移动你的 API 和其他动态路由

以下代码的工作原理类似:

  • /test 匹配在文件服务器和 returns 字符串 "OK"
  • 之前进行评估
  • 然后文件服务器returns路径下的文件

代码:

package main

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

func main() {
    appFileHandler := http.FileServer(http.Dir("/Users/alex/Projects/tmp/so/app"))
    r := mux.NewRouter()
    r.PathPrefix("/test").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
        w.Write([]byte("OK"))
    })
    r.PathPrefix("/").Handler(appFileHandler)
    http.Handle("/", r)
    http.ListenAndServe(":8080", nil)
}