0

我有一个问题,我想在 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

提前非常感谢!这让我大吃一惊,这是我在完全开始我的前端代码之前需要弄清楚的最后一件事:)。

4

1 回答 1

1

在我看来,您想更改路由的评估顺序,使/users,/login和类似的在/. 并且/应该是文件服务器的服务器。

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

以下代码的工作方式类似:

  • 匹配在/test文件服务器之前评估并返回字符串“OK”
  • 然后文件服务器返回路径下的文件

代码:

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)
}
于 2015-07-26T17:41:42.487 回答