Routes returning 404 for mux gorilla

Atmocreations

In my Go application, I'm using gorilla/mux.

I would like to have

http://host:3000/ to be serving files statically from the subdirectory "frontend" and http://host:3000/api/ and its subpaths being served by the specified functions.

With the following code, neither of the calls work. /index.html is the only one that doesn (but not the resources being loaded by it). What am I doing wrong?

package main

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

func main() {
  routineQuit := make(chan int)

  router := mux.NewRouter().StrictSlash(true)
  router.PathPrefix("/").Handler(http.FileServer(http.Dir("./frontend/")))
  router.HandleFunc("/api", Index)
  router.HandleFunc("/api/abc", AbcIndex)
  router.HandleFunc("/api/abc/{id}", AbcShow)
  http.Handle("/", router)
  http.ListenAndServe(":" + strconv.Itoa(3000), router)

  <- routineQuit
}

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

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

func AbcShow(w http.ResponseWriter, r *http.Request) {
  vars := mux.Vars(r)
  todoId := vars["todoId"]
  fmt.Fprintln(w, "Todo show:", todoId)
}
Elwinar

Gorilla's mux routes are evaluated in the order in which they are added. Therefore, the first route to match the request is used.

In your case, the / handler will match every incoming request, then look for the file in the frontend/ directory, then display a 404 error. You just need to swap your routes order to get it running:

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/api/abc/{id}", AbcShow)
router.HandleFunc("/api/abc", AbcIndex)
router.HandleFunc("/api", Abc)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./frontend/")))
http.Handle("/", router)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Golang Gorilla mux with http.FileServer returning 404

From Dev

how to organize gorilla mux routes?

From Dev

Golang gorilla mux routes from class or file

From Dev

Gorilla mux routes in separate files in subfolder?

From Dev

Custom 404 with Gorilla Mux and std http.FileServer

From Dev

Gorilla mux custom middleware

From Dev

Gorilla mux subdomain

From Dev

gorilla mux router handlers

From Dev

Gorilla Mux Regex

From Dev

Reversing a Subrouter in Gorilla / Mux

From Dev

Nesting subrouters in Gorilla Mux

From Dev

Laravel Routes returning a 404 with route parameters

From Dev

Gorilla Mux router from inside handler only works once then gives 404 page not found

From Dev

How to match subdomain with gorilla mux

From Dev

Gorilla Mux for sub path Routing

From Dev

Go Gorilla Mux Session Name

From Dev

Laravel routes are returning "404 Not Found" error after moving to production

From Dev

How to use gorilla mux with http.TimeoutHandler

From Dev

Handle array of ids in a request using gorilla/mux

From Dev

Nested Gorilla Mux router does not work

From Dev

golang mux HandleFunc always 404

From Dev

Gorilla mux returns blank url params during tests

From Dev

Google Cloud Go Handler without Router Gorilla Mux?

From Dev

In go, how do I use a closure with a gorilla/mux subrouter?

From Dev

How does 'PathPrefix' work in 'gorilla.mux' library for Go?

From Dev

Unit testing for functions that use gorilla/mux URL parameters

From Dev

How do I split URLS in multiple Files using gorilla/mux?

From Dev

My (valid?) regex doesn't work in gorilla/mux

From Dev

How can I find CSS files using golang Gorilla mux

Related Related

HotTag

Archive