Esempio n. 1
0
File: clio.go Progetto: kelyar/clio
/**
 *  Finding correct handler to certain method:path
 */
func Router(w http.ResponseWriter, req *http.Request) {

	// splitting whole path into parts
	path, paramsString := helpers.SplitPath(req.URL.String())

	routeFound := false

	method := req.Method
	if IsWebsocket() {
		method = "WS"
	}

	// finding correct handler
	for rawPattern, _ := range routes[method] {
		pattern := helpers.PreparePattern(rawPattern)

		if pattern.MatchString(path) {
			routeFound = true

			// homage to Sinatra's splat
			splat = pattern.FindAllStringSubmatch(path, 100)[0][1:]

			// filling params
			params = helpers.ParseParams(paramsString)

			// calling matched handler
			fmt.Fprintln(w, routes[method][rawPattern]())

			// terminal debugging
			if AppSettings["verbose-output"] != nil && AppSettings["verbose-output"].(bool) == true {
				log.Printf("%s %s\n", method, req.URL.String())
			}
			break
		}
	}
	if !routeFound {
		NotFound(w, req)
	}
}
Esempio n. 2
0
func TestPreparePatternDots(t *testing.T) {
	rxp := helpers.PreparePattern("/a.mp3")
	if rxp.String() != "^/a\\.mp3$" {
		t.Error("")
	}
}
Esempio n. 3
0
func TestPreparePattern(t *testing.T) {
	rxp := helpers.PreparePattern("/a/*/c")
	if rxp.String() != "^/a/([\\p{L}\\d\\-_]{1,})/c$" {
		t.Error("")
	}
}