コード例 #1
0
ファイル: run_server.go プロジェクト: RichardKnop/example-api
// 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
}
コード例 #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
}
コード例 #3
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
}