コード例 #1
0
ファイル: consul_pipe_router.go プロジェクト: dilgerma/scope
func (pr *consulPipeRouter) privateAPI() {
	router := mux.NewRouter()
	router.Methods("GET").
		MatcherFunc(app.URLMatcher("/private/api/pipe/{key}")).
		HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			key := mux.Vars(r)["key"]
			log.Infof("%s: Server bridge connection started", key)
			defer log.Infof("%s: Server bridge connection stopped", key)

			pipe := pr.getPipe(key)
			if pipe == nil {
				log.Errorf("%s: Server bridge connection; Unknown pipe!", key)
				w.WriteHeader(http.StatusNotFound)
				return
			}

			conn, err := xfer.Upgrade(w, r, nil)
			if err != nil {
				log.Errorf("%s: Server bridge connection; Error upgrading to websocket: %v", key, err)
				return
			}
			defer conn.Close()

			end, _ := pipe.Ends()
			if err := pipe.CopyToWebsocket(end, conn); err != nil && !xfer.IsExpectedWSCloseError(err) {
				log.Errorf("%s: Server bridge connection; Error copying pipe to websocket: %v", key, err)
			}
		})

	log.Infof("Serving private API on endpoint %s.", pr.advertise)
	log.Infof("Private API terminated: %v", http.ListenAndServe(pr.advertise, router))
}
コード例 #2
0
ファイル: router_test.go プロジェクト: dilgerma/scope
func TestURLMatcher(t *testing.T) {
	test := func(pattern, path string, match bool, vars v) {
		routeMatch := &mux.RouteMatch{}
		if app.URLMatcher(pattern)(&http.Request{RequestURI: path}, routeMatch) != match {
			t.Fatalf("'%s' '%s'", pattern, path)
		}
		if match && !reflect.DeepEqual(v(routeMatch.Vars), vars) {
			t.Fatalf("%v != %v", v(routeMatch.Vars), vars)
		}
	}

	test("/a/b/c", "/a/b/c", true, v{})
	test("/a/b/c", "/c/b/a", false, v{})
	test("/{a}/b/c", "/b/b/c", true, v{"a": "b"})
	test("/{a}/b/c", "/b/b/b", false, v{})
	test("/a/b/{c}", "/a/b/b", true, v{"c": "b"})
	test("/a/b/{c}", "/a/b/b%2Fb", true, v{"c": "b/b"})
}