Beispiel #1
0
func startServer(port int) <-chan error {

	reverseProxy := func(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
		hostPort := server.GetUrlParameter(req, "host_port")
		server.NewReverseProxy().SetForwardHostPort(hostPort).Strip("/"+hostPort).ServeHTTP(resp, req)
	}

	key := loadPublicKeyFromFile()
	_, stopped := server.NewService().
		WithAuth(
			server.Auth{
				VerifyKeyFunc: func() []byte { return key },
			}.Init()).
		ListenPort(port).
		Route(
			server.Endpoint{
				UrlRoute:    "/{host_port}/{url:.*}",
				HttpMethods: []server.HttpMethod{server.GET, server.POST, server.PUT, server.PATCH, server.DELETE},
				AuthScope:   server.AuthScopeNone,
			}).
		To(reverseProxy).
		Route(
			server.Endpoint{
				UrlRoute:    "/secure/{host_port}/{url:.*}",
				HttpMethods: []server.HttpMethod{server.GET, server.POST, server.PUT, server.PATCH, server.DELETE},
				AuthScope:   server.AuthScope("secure"),
			}).
		To(reverseProxy).
		Start()
	return stopped
}
Beispiel #2
0
// Spins up a test server that will serve the template text from an authenticated endpoint.
func (suite *TestSuiteTemplate) SetUpSuite(c *C) {
	suite.stop, suite.stopped = server.NewService().
		ListenPort(suite.port).
		WithAuth(server.Auth{VerifyKeyFunc: testutil.PublicKeyFunc}.Init()).
		Route(server.Endpoint{UrlRoute: "/secure", HttpMethod: server.GET, AuthScope: server.AuthScope("secure")}).
		To(func(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
			resp.Write([]byte(suite.template))
		}).Start()
}
Beispiel #3
0
func (suite *TestSuiteFuncs) SetUpSuite(c *C) {
	suite.stop, suite.stopped = server.NewService().
		ListenPort(suite.port).
		WithAuth(server.Auth{VerifyKeyFunc: testutil.PublicKeyFunc}.Init()).
		Route(server.Endpoint{UrlRoute: "/content", HttpMethod: server.GET, AuthScope: server.AuthScopeNone}).
		To(func(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
			resp.Write([]byte(suite.content))
		}).
		Route(server.Endpoint{UrlRoute: "/secure", HttpMethod: server.GET, AuthScope: server.AuthScope("secure")}).
		To(func(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
			resp.Write([]byte(suite.content))
		}).Start()

	suite.contentFile = os.TempDir() + "/test-content"
	err := ioutil.WriteFile(suite.contentFile, []byte(contentFileContent), 0644)
	c.Assert(err, IsNil)
}
Beispiel #4
0
func startServer(port int) <-chan error {
	key := loadPublicKeyFromFile()
	// For implementing shutdown
	proxy := make(chan bool)
	stop, stopped := server.NewService().
		WithAuth(
			server.Auth{
				VerifyKeyFunc: func() []byte { return key },
			}.Init()).
		ListenPort(port).
		Route(
			server.Endpoint{
				UrlRoute:   "/info",
				HttpMethod: server.GET,
				AuthScope:  server.AuthScopeNone,
			}).
		To(
			func(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
				glog.Infoln("Showing version info.")
				server.Marshal(resp, req, version.BuildInfo())
			}).
		Route(
			server.Endpoint{
				UrlRoute:   "/quitquitquit",
				HttpMethod: server.POST,
				AuthScope:  server.AuthScope("quitquitquit"),
			}).
		To(
			func(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
				glog.Infoln("Stopping the server....")
				proxy <- true
			}).
		OnShutdown(
			func() error {
				glog.Infoln("Executing user custom shutdown...")
				return nil
			}).
		Start()
	// For stopping the server
	go func() {
		<-proxy
		stop <- 1
	}()
	return stopped
}