FileServe和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")


}

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


注意:我的服务纯粹是结束HTTPS而不是结束HTTP。


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


皈依舞
浏览 193回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go