File: gorillamux.go

package info (click to toggle)
golang-github-manyminds-api2go 1.0-RC4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 552 kB
  • sloc: sh: 23; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// +build gorillamux,!gingonic,!echo

package routing

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/gorilla/mux"
)

type gorillamuxRouter struct {
	router *mux.Router
}

func (gm gorillamuxRouter) Handler() http.Handler {
	return gm.router
}

func (gm gorillamuxRouter) Handle(protocol, route string, handler HandlerFunc) {
	wrappedHandler := func(w http.ResponseWriter, r *http.Request) {
		handler(w, r, mux.Vars(r))
	}

	// The request path will have parameterized segments indicated as :name.  Convert
	// that notation to the {name} notation used by Gorilla mux.
	orig := strings.Split(route, "/")
	var mod []string
	for _, s := range orig {
		if len(s) > 0 && s[0] == ':' {
			s = fmt.Sprintf("{%s}", s[1:])
		}
		mod = append(mod, s)
	}
	modroute := strings.Join(mod, "/")

	gm.router.HandleFunc(modroute, wrappedHandler).Methods(protocol)
}

//Gorilla creates a new api2go router to use with the Gorilla mux framework
func Gorilla(gm *mux.Router) Routeable {
	return &gorillamuxRouter{router: gm}
}