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
}
)

var _ = Describe("Server", func() {
	// try to load signing keys for token authority
	// NOTE: DO NOT USE THESE KEYS FOR PRODUCTION! FOR TEST 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())))
	}

	Describe("Basic sanity test", func() {
		ctx := context.New()

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

		// init renderer
		renderer := renderer.New(&renderer.Options{
			IndentJSON: true,
		}, renderer.JSON)

		// set up users resource
		usersResource := users.NewResource(ctx, &users.Options{
			Database: db,
Exemple #3
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,
	})
}