func DisabledExample() { // create a target backend server. It will return the value of the 'X-Echo' request header // as the response body: targetServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(r.Header.Get("X-Echo"))) })) defer targetServer.Close() // create a filter registry, and register the custom filter: filterRegistry := builtin.MakeRegistry() filterRegistry.Register(&setEchoHeader{}) // create a data client with a predefined route, referencing the filter and a path condition // containing a wildcard called 'echo': routeDoc := fmt.Sprintf(`Path("/return/:echo") -> setEchoHeader() -> "%s"`, targetServer.URL) dataClient, err := testdataclient.NewDoc(routeDoc) if err != nil { log.Fatal(err) } // create routing object: rt := routing.New(routing.Options{ FilterRegistry: filterRegistry, DataClients: []routing.DataClient{dataClient}}) defer rt.Close() // create a proxy instance, and start an http server: proxy := proxy.New(rt, proxy.OptionsNone) defer proxy.Close() router := httptest.NewServer(proxy) defer router.Close() // make a request to the proxy: rsp, err := http.Get(fmt.Sprintf("%s/return/Hello,+world!", router.URL)) if err != nil { log.Fatal(err) } defer rsp.Body.Close() // print out the response: if _, err := io.Copy(os.Stdout, rsp.Body); err != nil { log.Fatal(err) } // Output: // Hello, world! }
// to run this test, set `-args listener` for the test command func TestHTTPSServer(t *testing.T) { // TODO: figure why sometimes cannot connect if !testListener() { t.Skip() } a, err := findAddress() if err != nil { t.Fatal(err) } o := Options{ Address: a, CertPathTLS: "fixtures/test.crt", KeyPathTLS: "fixtures/test.key", } rt := routing.New(routing.Options{ FilterRegistry: builtin.MakeRegistry(), DataClients: []routing.DataClient{}}) defer rt.Close() proxy := proxy.New(rt, proxy.OptionsNone) defer proxy.Close() go listenAndServe(proxy, &o) r, err := waitConnGet("https://" + o.Address) if r != nil { defer r.Body.Close() } if err != nil { t.Fatalf("Cannot connect to the local server for testing: %s ", err.Error()) } if r.StatusCode != 404 { t.Fatalf("Status code should be 404, instead got: %d\n", r.StatusCode) } _, err = ioutil.ReadAll(r.Body) if err != nil { t.Fatalf("Failed to stream response body: %v", err) } }
func TestWithWrongKeyPathFails(t *testing.T) { a, err := findAddress() if err != nil { t.Fatal(err) } o := Options{Address: a, CertPathTLS: "fixtures/test.crt", KeyPathTLS: "fixtures/notFound.key", } rt := routing.New(routing.Options{ FilterRegistry: builtin.MakeRegistry(), DataClients: []routing.DataClient{}}) defer rt.Close() proxy := proxy.New(rt, proxy.OptionsNone) defer proxy.Close() err = listenAndServe(proxy, &o) if err == nil { t.Fatal(err) } }
// Run skipper. func Run(o Options) error { // init log err := initLog(o) if err != nil { return err } // init metrics metrics.Init(metrics.Options{ Listener: o.MetricsListener, Prefix: o.MetricsPrefix, EnableDebugGcMetrics: o.EnableDebugGcMetrics, EnableRuntimeMetrics: o.EnableRuntimeMetrics, EnableServeRouteMetrics: o.EnableServeRouteMetrics, EnableServeHostMetrics: o.EnableServeHostMetrics, }) // create authentication for Innkeeper auth := innkeeper.CreateInnkeeperAuthentication(innkeeper.AuthOptions{ InnkeeperAuthToken: o.InnkeeperAuthToken, OAuthCredentialsDir: o.OAuthCredentialsDir, OAuthUrl: o.OAuthUrl, OAuthScope: o.OAuthScope}) // create data clients dataClients, err := createDataClients(o, auth) if err != nil { return err } // append custom data clients dataClients = append(dataClients, o.CustomDataClients...) if len(dataClients) == 0 { log.Warning("no route source specified") } // create a filter registry with the available filter specs registered, // and register the custom filters registry := builtin.MakeRegistry() for _, f := range o.CustomFilters { registry.Register(f) } // create routing // create the proxy instance var mo routing.MatchingOptions if o.IgnoreTrailingSlash { mo = routing.IgnoreTrailingSlash } // ensure a non-zero poll timeout if o.SourcePollTimeout <= 0 { o.SourcePollTimeout = defaultSourcePollTimeout } // check for dev mode, and set update buffer of the routes updateBuffer := defaultRoutingUpdateBuffer if o.DevMode { updateBuffer = 0 } // include bundeled custom predicates o.CustomPredicates = append(o.CustomPredicates, source.New(), interval.NewBetween(), interval.NewBefore(), interval.NewAfter(), cookie.New(), query.New()) // create a routing engine routing := routing.New(routing.Options{ FilterRegistry: registry, MatchingOptions: mo, PollTimeout: o.SourcePollTimeout, DataClients: dataClients, Predicates: o.CustomPredicates, UpdateBuffer: updateBuffer}) defer routing.Close() proxyFlags := proxy.Flags(o.ProxyOptions) | o.ProxyFlags proxyParams := proxy.Params{ Routing: routing, Flags: proxyFlags, PriorityRoutes: o.PriorityRoutes, IdleConnectionsPerHost: o.IdleConnectionsPerHost, CloseIdleConnsPeriod: o.CloseIdleConnsPeriod, FlushInterval: o.BackendFlushInterval, ExperimentalUpgrade: o.ExperimentalUpgrade} if o.DebugListener != "" { do := proxyParams do.Flags |= proxy.Debug dbg := proxy.WithParams(do) log.Infof("debug listener on %v", o.DebugListener) go func() { http.ListenAndServe(o.DebugListener, dbg) }() } // create the proxy proxy := proxy.WithParams(proxyParams) defer proxy.Close() return listenAndServe(proxy, &o) }