Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to organize gorilla mux routes?

Tags:

go

gorilla

i am using Gorilla Mux for writing a REST API and i am having trouble organizing my routes, currently all of my routes are defined in the main.go file like this

//main.go
package main

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

func main() {

    router := mux.NewRouter().StrictSlash(true)

    router.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "Hello")
    })

    router.HandleFunc("/user", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "User")
    })

    router.HandleFunc("/route2", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "Route2")
    })

    router.HandleFunc("/route3", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "Route3")
    })

    // route declarations continue like this

    http.ListenAndServe(":1128", router)

}

so what i want to do is take out and split this route declaration into multiple files, how would i go about doing that? thanks in advance.

like image 408
zola Avatar asked Dec 31 '15 14:12

zola


People also ask

What does Gorilla mux do?

Package gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler. The name mux stands for "HTTP request multiplexer". Like the standard http.

What is a mux router?

The name mux stands for "HTTP request multiplexer". Like the standard http. ServeMux, mux. Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions.

Which go router should I use?

The gorilla/mux package is perhaps the most famous Go router, and with good reason. It's packed full of features, including support for method-based routing, dynamic URLs, regexp route patterns, and host-based routing.


1 Answers

You can modularize your routers independently into different packages, and mount them on the main router

Elaborating just a little on the following issue, you can come up with this approach, that makes it quite scalable (and easier to test, to some degree)

/api/router.go

package api

import (
    "net/http"

    "github.com/gorilla/mux"
)

func Router() *mux.Router {
    router := mux.NewRouter()
    router.HandleFunc("/", home)
    return router
}

func home(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("hello from API"))
}

/main.go

package main

import (
    "log"
    "net/http"
    "strings"

    "github.com/...yourPath.../api"
    "github.com/...yourPath.../user"
    "github.com/gorilla/mux"
)

func main() {
    router := mux.NewRouter()

    router.HandleFunc("/", home)
    mount(router, "/api", api.Router())
    mount(router, "/user", user.Router())

    log.Fatal(http.ListenAndServe(":8080", router))
}

func mount(r *mux.Router, path string, handler http.Handler) {
    r.PathPrefix(path).Handler(
        http.StripPrefix(
            strings.TrimSuffix(path, "/"),
            handler,
        ),
    )
}

func home(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("Home"))
}
like image 98
Loris Avatar answered Sep 19 '22 16:09

Loris