You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
|
|
|
"Index",
|
|
|
|
"GET",
|
|
|
|
"/",
|
|
|
|
index,
|
|
|
|
},
|
|
|
|
Route{
|
|
|
|
"ShortenAPI",
|
|
|
|
"GET",
|
|
|
|
"/api/v1/action/shorten",
|
|
|
|
shortenJSON,
|
|
|
|
},
|
|
|
|
Route{
|
|
|
|
"LookupAPI",
|
|
|
|
"GET",
|
|
|
|
"/api/v1/action/lookup",
|
|
|
|
lookupJSON,
|
|
|
|
},
|
|
|
|
Route{
|
|
|
|
"GetAll",
|
|
|
|
"GET",
|
|
|
|
"/all",
|
|
|
|
all,
|
|
|
|
},
|
|
|
|
Route{
|
|
|
|
"Shorten",
|
|
|
|
"POST",
|
|
|
|
"/shorten",
|
|
|
|
shorten,
|
|
|
|
},
|
|
|
|
Route{
|
|
|
|
"Redirect",
|
|
|
|
"GET",
|
|
|
|
"/{token}",
|
|
|
|
redirect,
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|