使用Angular.js使用Gorilla Mux路由

使用Angular.js使用Gorilla Mux路由,go,routing,mux,gorilla,Go,Routing,Mux,Gorilla,我似乎无法正确选择路线。我正在使用Gorilla Mux,我正在尝试为我的angular应用程序提供服务,所以基本上是我的index.html,来自任何url,除了以/foo开头的url 这一个有效: func StaticFileServer(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, config.dir) } func main() { fs := http.Dir(config.dir) f

我似乎无法正确选择路线。我正在使用Gorilla Mux,我正在尝试为我的angular应用程序提供服务,所以基本上是我的index.html,来自任何url,除了以/foo开头的url

这一个有效:

func StaticFileServer(w http.ResponseWriter, r *http.Request) {
  http.ServeFile(w, r, config.dir)
}

func main() {

  fs := http.Dir(config.dir)
  fileHandler := http.FileServer(fs)

  router = mux.NewRouter()

  router.Handle("/foo/page", PublicHandler(handler(getAll)).Methods("GET")
  router.Handle("/foo/page/{id}", PublicHandler(handler(getOne)).Methods("GET")

  router.PathPrefix("/{blaah}/{blaah}/").Handler(fileHandler)
  router.PathPrefix("/").HandlerFunc(StaticFileServer)

  ...
}
但是肯定有一种比显式声明每个可能的路由更简单的方法,比如这个PathPrefix/{blaah}/{blaah}/thingy。。。 对于这个url,除/{blaah}/{blaah}/之外的任何其他url都会返回未找到的404页,而不是index.html


因此,我希望所有的东西都能被提供给静态文件等,只要它们能被找到,但其他所有东西都应该返回/public/index.html。

我有点晚了,但其他人可能会发现我的答案很有用

基本上,Gorilla Mux在这里执行所有的路由。我假设您希望AngularJS为任何不以/foo开头的URL进行路由

您可以使用regex将任何不以/foo开头的请求发送到index.html,方法是:

router.PathPrefix("/{_:.*}").HandlerFunc(StaticFileServer)

我也面临同样的问题,但如果我们使用Gorilla mux,我们有一个明确的解决方案

因为Angular是一个单页应用程序,所以我们必须这样处理。我有两个文件夹客户端和服务器。在客户端文件夹中,我保留所有角度代码,在服务器文件夹中,我保留所有服务器代码,因此呈现index.html的静态路径是client/dist。这里dist文件夹是角度构建文件夹

Go路由器代码如下所示-

spaHandler实现了http.Handler接口,因此我们可以使用它 响应HTTP请求。静态目录的路径,以及 该静态目录中索引文件的路径用于 在给定的静态目录中为SPA提供服务

type spaHandler struct {
    staticPath string
    indexPath  string
}
ServeHTTP检查URL路径以定位静态目录中的文件 在水疗中心。如果找到文件,将提供该文件。若否,详情为何? 将提供SPA处理程序上位于索引路径的文件。这 适用于服务SPA单页应用程序的行为

func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

    // get the absolute path to prevent directory traversal
    path, err := filepath.Abs(r.URL.Path)
    if err != nil {
        // if we failed to get the absolute path respond with a 400 bad request
        // and stop
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    // prepend the path with the path to the static directory
    path = filepath.Join(h.staticPath, path)

    // check whether a file exists at the given path
    _, err = os.Stat(path)
    if os.IsNotExist(err) {
        // file does not exist, serve index.html
        http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
        return
    } else if err != nil {
        // if we got an error (that wasn't that the file doesn't exist) stating the
        // file, return a 500 internal server error and stop
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    // otherwise, use http.FileServer to serve the static dir
    http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}

否。它使客户端请求的每个未在gorilla router中定义的URL作为index.html文件的响应-从而让您的angualr应用程序处理API调用以外的路由ui router,ngRoute:。
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

    // get the absolute path to prevent directory traversal
    path, err := filepath.Abs(r.URL.Path)
    if err != nil {
        // if we failed to get the absolute path respond with a 400 bad request
        // and stop
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    // prepend the path with the path to the static directory
    path = filepath.Join(h.staticPath, path)

    // check whether a file exists at the given path
    _, err = os.Stat(path)
    if os.IsNotExist(err) {
        // file does not exist, serve index.html
        http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
        return
    } else if err != nil {
        // if we got an error (that wasn't that the file doesn't exist) stating the
        // file, return a 500 internal server error and stop
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    // otherwise, use http.FileServer to serve the static dir
    http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}