The official API for the Stuyvesant Spectator.
- Clone the repo
$ git clone https://github.com/stuyspec/stuy-spec-api.git
-
Follow the directions below to set up Rails. Ignore the MySQL section; instead, only complete the PostgreSQL section: https://gorails.com/setup/
-
In the
stuy-spec-api
repository, create your dotenv file.
$ echo PG_HOST=localhost > .env
- If you intend to use this API while working with client-app or cli-uploader, follow the setup instructions in the AWS S3 section below. Then create, migrate, and seed the database with media.
$ rails db:create db:migrate db:seed media=true
- If you did not follow instruction 4, create, migrate, and seed the database.
$ rails db:create db:migrate db:seed
- To start the server, run:
rails server
If the above doesn't work, you can use Docker. Docker encapsulates your runtime environment into a "container", basically making your configuration deterministic and reproducible.
-
Follow steps 1-3 above
-
Run
docker-compose build
-
Run
docker-compose up
. If you get an error saying it can't connect to db, try stopping and rerunning. -
In a separate terminal instance, run
docker-compose run web rake db:create
. If there are a bunch of errors about being unable to connect to TCP/IP at 5432, just check the top of those errors to see if something likeCreated database stuy-spec-api_development
was created. If so, then ignore the errors. -
Run
docker-compose run web rails db:migrate db:seed
-
To start the server, run
docker-compose run web rails server
.
You will need to be an IAM user for the Spectator Web AWS account. Request an account by messaging one of the editors on Facebook.
While you wait for your beloved editors to get the account set up, watch this IAM introduction.
After you receive your login information, navigate to the stuyspec AWS console and log in. In the AWS console, navigate to the service "IAM". Go to Users, in the sidebar, and click on your username. Click the Security Credentials tab and create an Access Key. It will prompt you to download a file with your new access key and secret key. Download it.
If you don't have one already, navigate to your home directory (cd ~/
) and make a directory called .aws
. Then create a file called config
with this content:
[default]
region=us-east-2
output=json
Next, create a file called credentials
with this content:
[default]
aws_access_key_id=YOUR_ACCESS_KEY_ID
aws_secret_access=YOUR_SECRET_ACCESS
Once the ~/.aws
directory is set up above, S3 should work automagically.
Elastic Beanstalk is an orchestration service offered from Amazon Web Services for deploying infrastructure which orchestrates various AWS services, including EC2 (a cloud computing service) and S3. It is where we host our production database.
The Rails console lets you interact with the Rails API from the command line with Ruby. For instance:
> a = Article.find_by(title: 'The Original Title')
> a.title = 'New Title'
> a.save
This function is helpful if you know cli-uploader made a mistake or you want to make a small change in a record of the database. Here are the steps to open the Rails console for our production database:
-
Run
eb init
. Select the default region as choice 1 (us-east-1). Select the default application as choice 1 (stuyspec-api-prod). DO NOT create a new application and DO NOT create a new environment. -
You'll need an SSH key to be able to SSH. This comes in the form of a
.pem
key file that you put into your home ssh directory (~/.ssh
). Download the.pem
key (either from Jason Kao or Nicholas Yang) and place it into the aforementioned directory. NEVER share this file publically. -
eb ssh
and connect to our EB instance. -
Navigate to the directory of the Rails API (
cd /var/app/current
). -
Here, you can run
rails console
and manipulate the database.
To exit the Rails console, use Ctrl-D
. To exit the SSH, use Ctrl-D
as well.
-
Go to your local
stuy-spec-api
directory and check out and pull down the branch you want to deploy. -
Run
eb init
. Select the default region as choice 1 (us-east-1). Select the default application as choice 1 (stuyspec-api-prod). DO NOT create a new application and DO NOT create a new environment. -
Run
eb deploy
.
Use GraphiQL to test your GraphQL queries.