1
votes

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, 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
I was able to reproduce this and now it's working with the same code posted by OP. Try using GetError() to see if registering a route gives you any issue. Something like: err := router.Methods(route.Method).Path(route.Pattern).Name(route.Name).Handler(route.HandlerFunc).GetError()abm
Adding .GetError() didn't return anything different then what I currently see.CGideon
Weird ... last thing I did that made it work was remove the .Name() function call when registering the routes.. try doing that and make sure you are not caching the responses.abm
Removed the .Name() call and the appropriate field in the Route and still no luck, I've added my updated Router.go file to the question.CGideon
Oh .. make sure to save the returned error: err := router.Methods()...GetError() and check if it returns an error if err != nil{ log.Println(err) }abm

1 Answers

1
votes

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.