Beispiel #1
0
		sessionsResource := sessions.NewResource(ctx, &sessions.Options{
			Database:              db,
			Renderer:              renderer,
			PrivateSigningKey:     privateSigningKey,
			PublicSigningKey:      publicSigningKey,
			UserRepositoryFactory: usersResource.UserRepositoryFactory,
		})

		// init server
		s := server.NewServer(&server.Config{
			Context: ctx,
		})

		// set up router
		ac := server.NewAccessController(ctx, renderer)
		router := server.NewRouter(s.Context, ac)

		// add REST resources to router
		router.AddResources(sessionsResource, usersResource)

		// add middlewares
		s.UseContextMiddleware(renderer)
		s.UseMiddleware(sessionsResource.NewAuthenticator())

		s.UseContextMiddleware(renderer)

		s.UseRouter(router)

		It("should serve request", func() {
			// run server and it shouldn't panic
			go s.Run(":8001", server.Options{
Beispiel #2
0
func main() {

	// try to load signing keys for token authority
	// NOTE: DO NOT USE THESE KEYS FOR PRODUCTION! FOR DEMO ONLY
	privateSigningKey, err := ioutil.ReadFile("keys/demo.rsa")
	if err != nil {
		panic(errors.New(fmt.Sprintf("Error loading private signing key: %v", err.Error())))
	}
	publicSigningKey, err := ioutil.ReadFile("keys/demo.rsa.pub")
	if err != nil {
		panic(errors.New(fmt.Sprintf("Error loading public signing key: %v", err.Error())))
	}

	// create current project context
	ctx := context.New()

	// set up DB session
	db := mongodb.New(&mongodb.Options{
		ServerName:   "localhost",
		DatabaseName: "test-go-app",
	})
	_ = db.NewSession()

	// set up Renderer (unrolled_render)
	renderer := renderer.New(&renderer.Options{
		IndentJSON: true,
	}, renderer.JSON)

	// set up users resource
	usersResource := users.NewResource(ctx, &users.Options{
		Database: db,
		Renderer: renderer,
	})

	// set up sessions resource
	sessionsResource := sessions.NewResource(ctx, &sessions.Options{
		PrivateSigningKey:     privateSigningKey,
		PublicSigningKey:      publicSigningKey,
		Database:              db,
		Renderer:              renderer,
		UserRepositoryFactory: usersResource.UserRepositoryFactory,
	})

	// init server
	s := server.NewServer(&server.Config{
		Context: ctx,
	})

	// set up router
	ac := server.NewAccessController(ctx, renderer)
	router := server.NewRouter(s.Context, ac)

	// add REST resources to router
	router.AddResources(sessionsResource, usersResource)

	// add middlewares
	s.UseMiddleware(sessionsResource.NewAuthenticator())

	// setup router
	s.UseRouter(router)

	// bam!
	s.Run(":3001", server.Options{
		Timeout: 10 * time.Second,
	})
}
Beispiel #3
0
func NewTestServer(options *TestServerOptions) *TestServer {

	// set up basic needs for a test server

	ta := options.TokenAuthority
	if ta == nil {
		if options.PrivateSigningKey == nil {
			panic("TestServer.options.PrivateSigningKey is required")
		}
		if options.PublicSigningKey == nil {
			panic("TestServer.options.PublicSigningKey is required")
		}
		ta = sessions.NewTokenAuthority(&sessions.TokenAuthorityOptions{
			PrivateSigningKey: options.PrivateSigningKey,
			PublicSigningKey:  options.PublicSigningKey,
		})
	}

	ctx := context.New()

	// init server
	s := server.NewServer(&server.Config{
		Context: ctx,
	})

	// set up DB session if not specified
	db := options.Database
	if options.Database == nil {
		db = mongodb.New(&mongodb.Options{
			ServerName:   "localhost",
			DatabaseName: "test-go-app",
		})
		_ = db.(*mongodb.MongoDB).NewSession()
	}

	// set up Renderer (unrolled_render)
	r := options.Renderer
	if r == nil {
		r = renderer.New(&renderer.Options{
			IndentJSON: true,
		}, renderer.JSON)
	}

	// set up router
	ac := server.NewAccessController(ctx, r)
	router := server.NewRouter(s.Context, ac)

	// init test server
	ts := TestServer{options, s, router, ta, db, r}

	// add REST resources to router
	for _, resource := range options.Resources {
		ts.AddResources(resource)
	}

	// add middlewares
	for _, middleware := range options.Middlewares {
		ts.AddMiddlewares(middleware)
	}

	return &ts
}
Beispiel #4
0
				Name:           "Test",
				Method:         "GET",
				Pattern:        "/api/test",
				DefaultVersion: "0.2",
				RouteHandlers: domain.RouteHandlers{
					"0.1": handleStub(ctx, "0.1"),
					"0.2": handleStub(ctx, "0.2"),
					"0.3": handleStub(ctx, "0.3"),
				},
				ACLHandler: handleAcl,
			}
			routes := &domain.Routes{route}
			s = server.NewServer(&server.Config{
				Context: ctx,
			})
			router := server.NewRouter(ctx, nil)
			router.AddRoutes(routes)
			s.UseContextMiddleware(r)
			s.UseRouter(router)

			// record HTTP responses
			recorder = httptest.NewRecorder()
		})

		Context("when user does not specify API version", func() {

			It("should use default API version", func() {

				request, _ = http.NewRequest("GET", "/api/test", nil)
				s.ServeHTTP(recorder, request)
				bodyJSON = test_helpers.MapFromJSON(recorder.Body.Bytes())