Gorilla Mux 路由无法解析。

huangapple go评论71阅读模式
英文:

Gorilla Mux routes not resolving

问题

所以,我正在使用Go和Gorilla Mux构建一个简单的RESTful API。我在第二个路由上遇到了问题,它返回了一个404错误。由于我对Go和Gorilla还不熟悉,所以我不确定问题出在哪里。我相信这可能是一个非常简单的问题,但我找不到解决办法。我认为问题可能出在我使用了不同的自定义包上。

这个问题类似于https://stackoverflow.com/questions/28831190/routes-returning-404-for-mux-gorilla,但是接受的解决方案没有解决我的问题。

以下是我的代码:

Router.go:

package router

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

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {
	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(route.HandlerFunc)
	}

	return router
}

var routes = Routes{
	Route{
		"CandidateList",
		"GET",
		"/candidate",
		CandidateList,
	},
	Route{
		"Index",
		"GET",
		"/",
		Index,
	},
}

Handlers.go:

package router

import (
	"fmt"
	"net/http"
)

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Welcome!")
}

func CandidateList(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "CandidateList!")
}

Main.go:

package main

import (
	"./router"
	"log"
	"net/http"
)

func main() {
	rout := router.NewRouter()
	log.Fatal(http.ListenAndServe(":8080", rout))
}

访问localhost:8080会返回"Welcome!",但访问localhost:8080/candidate会返回404 Page Not Found错误。感谢任何意见和帮助!谢谢!

这是我更新后的Router.go文件,仍然存在相同的问题。

Router.go:

package router

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

type Route struct {
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {
	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		router.
			Methods(route.Method).
			Path(route.Pattern).
			Handler(route.HandlerFunc).GetError()
	}

	return router
}

var routes = Routes{
	Route{
		"GET",
		"/candidate",
		CandidateList,
	},
	Route{
		"GET",
		"/",
		Index,
	},
}
英文:

So, I'm working on a simple RESTful API using Go and Gorilla Mux. I'm having issues with with my second route not working, it's returning a 404 error. I'm not sure what the problem is as I'm new to Go and Gorilla. I'm sure it's something really simple, but I can't seem to find it. I think it might be a problem with the fact that I'm using different custom packages.

This question is similar, https://stackoverflow.com/questions/28831190/routes-returning-404-for-mux-gorilla, but the accepted solution didn't fix my problem

Here's what my code looks like:

Router.go:

package router

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

type Route struct {
    Name        string
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {

router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
	    router.
		Methods(route.Method).
		Path(route.Pattern).
		Name(route.Name).
		Handler(route.HandlerFunc)
    }

    return router
}

var routes = Routes{
    Route{
	    "CandidateList",
	    "GET",
	    "/candidate",
	    CandidateList,
    },
	Route{
	    "Index",
	    "GET",
	    "/",
	    Index,
    },
}

Handlers.go

package router

import (
    "fmt"
    "net/http"
)

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Welcome!")
}

func CandidateList(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "CandidateList!")
}

Main.go

package main

import (
    "./router"
    "log"
    "net/http"
)

func main() {
    rout := router.NewRouter()
    log.Fatal(http.ListenAndServe(":8080", rout))
}

Going to just localhost:8080 returns Welcome! but going to localhost:8080/candidate returns a 404 Page Not Found error. I appreciate any input and help! Thanks!

This is an updated version of my Router.go file, there is still the same issue happening.

Router.go

package router

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

type Route struct {
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {

    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
	    router.
		    Methods(route.Method).
		    Path(route.Pattern).
		    Handler(route.HandlerFunc).GetError()
    }

    return router
}

var routes = Routes{
   Route{
	  "GET",
	  "/candidate",
	  CandidateList,
   },
   Route{
	   "GET",
	   "/",
	   Index,
    },
}

答案1

得分: 1

看起来我的项目在主要的src目录中保留了旧版本的Router.go和Handlers.go文件。通过删除这些重复的文件,并使用"go run Main.go"重新运行Main.go,我成功地让路由被识别了。

英文:

It appears that my project was holding onto old versions of the Router.go and Handlers.go files in the main src directory. By removing these duplicate files and re-running Main.go with go run Main.go I was able to get the route to be recognized.

huangapple
  • 本文由 发表于 2016年1月21日 05:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/34910997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定