Skip to content
Snippets Groups Projects
Forked from asapo / asapo
2069 commits behind the upstream repository.
routes.go 696 B
package utils

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

type Routes []Route

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

func NewRouter(listRoutes Routes) *mux.Router {
	router := mux.NewRouter()
	for _, route := range listRoutes {
		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(route.HandlerFunc)
		// allow routes without trailing slash
		if strings.HasSuffix(route.Pattern, "/") {
			router.
				Methods(route.Method).
				Path(strings.TrimSuffix(route.Pattern, "/")).
				Name(route.Name + "_noslash").
				Handler(route.HandlerFunc)
		}
	}
	return router
}