示例#1
0
func (server *Server) buildDefaultHTTPRouter() *mux.Router {
	router := mux.NewRouter()
	router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
	router.StrictSlash(true)
	router.SkipClean(true)
	return router
}
示例#2
0
func TestParseTwoRules(t *testing.T) {
	router := mux.NewRouter()
	route := router.NewRoute()
	serverRoute := &serverRoute{route: route}
	rules := &Rules{route: serverRoute}

	expression := "Host: Foo.Bar ; Path:/FOObar"
	routeResult, err := rules.Parse(expression)

	if err != nil {
		t.Fatal("Error while building route for Host:foo.bar;Path:/FOObar")
	}

	request, err := http.NewRequest("GET", "http://foo.bar/foobar", nil)
	routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})

	if routeMatch == true {
		t.Log(err)
		t.Fatal("Rule Host:foo.bar;Path:/FOObar don't match")
	}

	request, err = http.NewRequest("GET", "http://foo.bar/FOObar", nil)
	routeMatch = routeResult.Match(request, &mux.RouteMatch{Route: routeResult})

	if routeMatch == false {
		t.Log(err)
		t.Fatal("Rule Host:foo.bar;Path:/FOObar don't match")
	}
}
示例#3
0
文件: web.go 项目: ldez/traefik
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *WebProvider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ []types.Constraint) error {
	systemRouter := mux.NewRouter()

	// health route
	systemRouter.Methods("GET").Path("/health").HandlerFunc(provider.getHealthHandler)

	// API routes
	systemRouter.Methods("GET").Path("/api").HandlerFunc(provider.getConfigHandler)
	systemRouter.Methods("GET").Path("/api/providers").HandlerFunc(provider.getConfigHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}").HandlerFunc(provider.getProviderHandler)
	systemRouter.Methods("PUT").Path("/api/providers/{provider}").HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
		if provider.ReadOnly {
			response.WriteHeader(http.StatusForbidden)
			fmt.Fprintf(response, "REST API is in read-only mode")
			return
		}
		vars := mux.Vars(request)
		if vars["provider"] != "web" {
			response.WriteHeader(http.StatusBadRequest)
			fmt.Fprintf(response, "Only 'web' provider can be updated through the REST API")
			return
		}

		configuration := new(types.Configuration)
		body, _ := ioutil.ReadAll(request.Body)
		err := json.Unmarshal(body, configuration)
		if err == nil {
			configurationChan <- types.ConfigMessage{"web", configuration}
			provider.getConfigHandler(response, request)
		} else {
			log.Errorf("Error parsing configuration %+v", err)
			http.Error(response, fmt.Sprintf("%+v", err), http.StatusBadRequest)
		}
	})
	systemRouter.Methods("GET").Path("/api/providers/{provider}/backends").HandlerFunc(provider.getBackendsHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}/backends/{backend}").HandlerFunc(provider.getBackendHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}/backends/{backend}/servers").HandlerFunc(provider.getServersHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}/backends/{backend}/servers/{server}").HandlerFunc(provider.getServerHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}/frontends").HandlerFunc(provider.getFrontendsHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}/frontends/{frontend}").HandlerFunc(provider.getFrontendHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}/frontends/{frontend}/routes").HandlerFunc(provider.getRoutesHandler)
	systemRouter.Methods("GET").Path("/api/providers/{provider}/frontends/{frontend}/routes/{route}").HandlerFunc(provider.getRouteHandler)

	// Expose dashboard
	systemRouter.Methods("GET").Path("/").HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
		http.Redirect(response, request, "/dashboard/", 302)
	})
	systemRouter.Methods("GET").PathPrefix("/dashboard/").Handler(http.StripPrefix("/dashboard/", http.FileServer(&assetfs.AssetFS{Asset: autogen.Asset, AssetInfo: autogen.AssetInfo, AssetDir: autogen.AssetDir, Prefix: "static"})))

	// expvars
	if provider.server.globalConfiguration.Debug {
		systemRouter.Methods("GET").Path("/debug/vars").HandlerFunc(expvarHandler)
	}

	go func() {
		if len(provider.CertFile) > 0 && len(provider.KeyFile) > 0 {
			err := http.ListenAndServeTLS(provider.Address, provider.CertFile, provider.KeyFile, systemRouter)
			if err != nil {
				log.Fatal("Error creating server: ", err)
			}
		} else {
			err := http.ListenAndServe(provider.Address, systemRouter)
			if err != nil {
				log.Fatal("Error creating server: ", err)
			}
		}
	}()
	return nil
}
示例#4
0
func TestPriorites(t *testing.T) {
	router := mux.NewRouter()
	router.StrictSlash(true)
	rules := &Rules{route: &serverRoute{route: router.NewRoute()}}
	routeFoo, err := rules.Parse("PathPrefix:/foo")
	if err != nil {
		t.Fatal("Error while building route for PathPrefix:/foo")
	}
	fooHandler := &fakeHandler{name: "fooHandler"}
	routeFoo.Handler(fooHandler)

	if !router.Match(&http.Request{URL: &url.URL{
		Path: "/foo",
	}}, &mux.RouteMatch{}) {
		t.Fatalf("Error matching route")
	}

	if router.Match(&http.Request{URL: &url.URL{
		Path: "/fo",
	}}, &mux.RouteMatch{}) {
		t.Fatalf("Error matching route")
	}

	multipleRules := &Rules{route: &serverRoute{route: router.NewRoute()}}
	routeFoobar, err := multipleRules.Parse("PathPrefix:/foobar")
	if err != nil {
		t.Fatal("Error while building route for PathPrefix:/foobar")
	}
	foobarHandler := &fakeHandler{name: "foobarHandler"}
	routeFoobar.Handler(foobarHandler)
	if !router.Match(&http.Request{URL: &url.URL{
		Path: "/foo",
	}}, &mux.RouteMatch{}) {
		t.Fatalf("Error matching route")
	}
	fooMatcher := &mux.RouteMatch{}
	if !router.Match(&http.Request{URL: &url.URL{
		Path: "/foobar",
	}}, fooMatcher) {
		t.Fatalf("Error matching route")
	}

	if fooMatcher.Handler == foobarHandler {
		t.Fatalf("Error matching priority")
	}

	if fooMatcher.Handler != fooHandler {
		t.Fatalf("Error matching priority")
	}

	routeFoo.Priority(1)
	routeFoobar.Priority(10)
	router.SortRoutes()

	foobarMatcher := &mux.RouteMatch{}
	if !router.Match(&http.Request{URL: &url.URL{
		Path: "/foobar",
	}}, foobarMatcher) {
		t.Fatalf("Error matching route")
	}

	if foobarMatcher.Handler != foobarHandler {
		t.Fatalf("Error matching priority")
	}

	if foobarMatcher.Handler == fooHandler {
		t.Fatalf("Error matching priority")
	}
}