douxun2023 2015-07-26 12:23
浏览 32
已采纳

FileServe和路由之间更流畅的合作

I have an issue where I want to serve my main AngularJS (Yeoman deployment) app folder on FileServe / but it will destroy all my router bindings. Is there any way I can reserve them and keep my routes in-tact?

In the code below I still have to go to /app and re-bind the other folders, because I don't want to temper with the Grunt file too much (yet), so added some extra backup path-bindings for the folders.

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)
}

So my goal is the serve /app as my / main path, but reserve all my Mux Routes to win over the FileServe. So if I'll have a /app/users/login folder, it won't load, but instead it will let the Router win.

Note: I'm serving purely over HTTPS and nothing over HTTP.

Thanks so much in advance! This is breaking my brain and it's the last thing I need to figure out before I can fully start on my front-end code :).

  • 写回答

1条回答 默认 最新

  • dsieyx2015 2015-07-26 17:41
    关注

    It looks for me like you want to change the order of how the routes are evaluated in a way that /users, /login, and similar are matched before the /. And the / should be server by the FileServer.

    As far as I know, the routes will be matched in the order they are defined (added to the router). So you just have to move your API and other dynamic routes before /.

    The following code works similar:

    • the /test match is evaluated before the file server and returns the string "OK"
    • then the file server returns the file under the path

    Code:

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法