Exemplo n.º 1
0
// RunServer runs the app
func RunServer() error {
	cnf, db, err := initConfigDB(true, true)
	if err != nil {
		return err
	}
	defer db.Close()

	// Initialise the health service
	healthService := health.NewService(db)

	// Initialise the oauth service
	oauthService := oauth.NewService(cnf, db)

	// Initialise the email service
	emailService := email.NewService(cnf)

	// Initialise the accounts service
	accountsService := accounts.NewService(
		cnf,
		db,
		oauthService,
		emailService,
		nil, // accounts.EmailFactory
	)

	// Initialise the facebook service
	facebookService := facebook.NewService(
		cnf,
		db,
		accountsService,
		nil, // facebook.Adapter
	)

	// Start a negroni app
	app := negroni.New()
	app.Use(negroni.NewRecovery())
	app.Use(negroni.NewLogger())
	app.Use(gzip.Gzip(gzip.DefaultCompression))

	// Create a router instance
	router := mux.NewRouter()

	// Register routes
	healthService.RegisterRoutes(router, "/v1")
	oauthService.RegisterRoutes(router, "/v1/oauth")
	accountsService.RegisterRoutes(router, "/v1")
	facebookService.RegisterRoutes(router, "/v1/facebook")

	// Set the router
	app.UseHandler(router)

	// Run the server on port 8080, gracefully stop on SIGTERM signal
	graceful.Run(":8080", 5*time.Second, app)

	return nil
}
Exemplo n.º 2
0
// CreateSuperuser creates a new superuser
func CreateSuperuser() error {
	cnf, db, err := initConfigDB(true, false)
	if err != nil {
		return err
	}
	defer db.Close()

	// Initialise the oauth service
	oauthService := oauth.NewService(cnf, db)

	// Initialise the email service
	emailService := email.NewService(cnf)

	// Initialise the accounts service
	accountsService := accounts.NewService(
		cnf,
		db,
		oauthService,
		emailService,
		nil, // accounts.EmailFactory
	)

	// Fetch the account (assume all superusers belong to the first account)
	account, err := accountsService.FindAccountByID(uint(1))
	if err != nil {
		return err
	}

	reader := bufio.NewReader(os.Stdin)

	// Email from a user input
	fmt.Print("Email: ")
	email, err := reader.ReadString('\n')
	if err != nil {
		return err
	}

	// Password from a user input
	fmt.Print("Password: "******"\n"),
		strings.TrimRight(password, "\n"),
	)

	return err
}
Exemplo n.º 3
0
// The SetupSuite method will be run by testify once, at the very
// start of the testing suite, before any tests are run.
func (suite *FacebookTestSuite) SetupSuite() {
	// Initialise the config
	suite.cnf = config.NewConfig(false, false)

	// Create the test database
	db, err := database.CreateTestDatabasePostgres(
		testDbUser,
		testDbName,
		testMigrations,
		testFixtures,
	)
	if err != nil {
		log.Fatal(err)
	}
	suite.db = db

	// Fetch test accounts
	suite.accounts = make([]*accounts.Account, 0)
	err = accounts.AccountPreload(suite.db).Order("id").Find(&suite.accounts).Error
	if err != nil {
		log.Fatal(err)
	}

	// Fetch test users
	suite.users = make([]*accounts.User, 0)
	err = accounts.UserPreload(suite.db).Order("id").Find(&suite.users).Error
	if err != nil {
		log.Fatal(err)
	}

	// Initialise mocks
	suite.adapterMock = new(facebook.AdapterMock)

	// Initialise the service
	suite.service = facebook.NewService(
		suite.cnf,
		suite.db,
		accounts.NewService(
			suite.cnf,
			suite.db,
			oauth.NewService(suite.cnf, suite.db),
			new(emailMocks.ServiceInterface),
			new(accountsMocks.EmailFactoryInterface),
		),
		suite.adapterMock,
	)

	// Register routes
	suite.router = mux.NewRouter()
	suite.service.RegisterRoutes(suite.router, "/v1/facebook")
}
Exemplo n.º 4
0
// CreateAccount creates a new account
func CreateAccount() error {
	cnf, db, err := initConfigDB(true, false)
	if err != nil {
		return err
	}
	defer db.Close()

	// Initialise the oauth service
	oauthService := oauth.NewService(cnf, db)

	// Initialise the email service
	emailService := email.NewService(cnf)

	// Initialise the accounts service
	accountsService := accounts.NewService(
		cnf,
		db,
		oauthService,
		emailService,
		nil, // accounts.EmailFactory
	)

	reader := bufio.NewReader(os.Stdin)

	// Account name from a user input
	fmt.Print("Name: ")
	name, err := reader.ReadString('\n')
	if err != nil {
		return err
	}

	// Account description from a user input
	fmt.Print("Description: ")
	description, err := reader.ReadString('\n')
	if err != nil {
		return err
	}

	// API key (oauth client ID) from a user input
	fmt.Print("API Key (oauth client ID): ")
	apiKey, err := reader.ReadString('\n')
	if err != nil {
		return err
	}

	// OAuth 2.0 client secret from a user input
	fmt.Print("API Secret (oauth client secret): ")
	apiSecret, err := reader.ReadString('\n')
	if err != nil {
		return err
	}

	// OAuth 2.0 client redirect URI from a user input
	fmt.Print("Redirect URI (oauth): ")
	redirectURI, err := reader.ReadString('\n')
	if err != nil {
		return err
	}

	// Create a new account
	_, err = accountsService.CreateAccount(
		strings.TrimRight(name, "\n"),
		strings.TrimRight(description, "\n"),
		strings.TrimRight(apiKey, "\n"),
		strings.TrimRight(apiSecret, "\n"),
		strings.TrimRight(redirectURI, "\n"),
	)

	return err
}