{"id":23910808,"url":"https://github.com/stuyspec/stuyspec-api","last_synced_at":"2025-04-11T12:23:58.846Z","repository":{"id":23381768,"uuid":"98824525","full_name":"stuyspec/stuyspec-api","owner":"stuyspec","description":"🗄🛰📲 Rails API for The Spectator; GraphQL server, written with PostgreSQL.","archived":false,"fork":false,"pushed_at":"2023-03-08T17:24:57.000Z","size":3076,"stargazers_count":11,"open_issues_count":20,"forks_count":5,"subscribers_count":7,"default_branch":"develop","last_synced_at":"2025-03-25T08:42:42.857Z","etag":null,"topics":["database","docker","graphql-server","postgresql","rails-api"],"latest_commit_sha":null,"homepage":"https://api.stuyspec.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stuyspec.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-30T21:00:08.000Z","updated_at":"2022-11-07T02:31:33.000Z","dependencies_parsed_at":"2025-01-05T07:32:49.828Z","dependency_job_id":"f5f014b4-7ade-4713-9e63-3038f8f47548","html_url":"https://github.com/stuyspec/stuyspec-api","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuyspec%2Fstuyspec-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuyspec%2Fstuyspec-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuyspec%2Fstuyspec-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuyspec%2Fstuyspec-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stuyspec","download_url":"https://codeload.github.com/stuyspec/stuyspec-api/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248401415,"owners_count":21097328,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","docker","graphql-server","postgresql","rails-api"],"created_at":"2025-01-05T07:28:22.413Z","updated_at":"2025-04-11T12:23:58.827Z","avatar_url":"https://github.com/stuyspec.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"https://imgur.com/aPbzogH.png\"/\u003e\n\n# Stuyvesant Spectator API\n\nThe official API for the Stuyvesant Spectator.\n\n## Setting Up\n\n1. Clone the repo\n```\n$ git clone https://github.com/stuyspec/stuy-spec-api.git\n```\n\n2. Follow the directions below to set up Rails. Ignore the MySQL section; instead, only complete the PostgreSQL section: https://gorails.com/setup/\n\n3. On linux, go to /etc/postgresql/YOUR_VERSION/main/pg_hba.conf. At the bottom, change the method to trust for all lines that start with local all or host all. Then, run `sudo systemctl restart postgresql`\n\n4. In the `stuy-spec-api` repository, create your [dotenv](https://github.com/bkeepers/dotenv) file.\n```\n$ echo PG_HOST=localhost \u003e .env\n```\n\n5. If you intend to use this API while working with client-app or cli-uploader, follow the setup instructions in the [AWS S3](#setting-up-s3) section below. Then create, migrate, and seed the database with media.\n```\n$ rails db:create db:migrate db:seed media=true\n```\n\n6. If you did not follow instruction 4, create, migrate, and seed the database.\n```\n$ rails db:create db:migrate db:seed\n```\n\n7. To start the server, run:\n```\nrails server\n```\n\n## Docker\n\nIf the above doesn't work, you can use Docker. Docker encapsulates your runtime environment into\na \"container\", basically making your configuration deterministic and reproducible.\n\n1. Follow steps 1-3 above\n\n2. Run `docker-compose build`\n\n3. Run `docker-compose up`. If you get an error saying it can't\nconnect to db, try stopping and rerunning.\n\n4. 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 like `Created database stuy-spec-api_development` was created. If so, then ignore the errors.\n\n5. Run `docker-compose run web rails db:migrate db:seed`\n\n6. To start the server, run `docker-compose run web rails server`.\n\n## AWS\n\nYou will need to be an IAM user for the Spectator Web AWS account. Request an account by messaging one of the editors on Facebook.\n\nWhile you wait for your beloved editors to get the account set up, watch this [IAM introduction](https://www.youtube.com/watch?v=Ul6FW4UANGc).\n\nAfter you receive your login information, navigate to the [stuyspec AWS console](https://stuyspec.signin.aws.amazon.com/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.\n\nIf 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:\n\n```\n[default]\nregion=us-east-2\noutput=json\n```\n\nNext, create a file called `credentials` with this content:\n\n```\n[default]\naws_access_key_id=YOUR_ACCESS_KEY_ID\naws_secret_access=YOUR_SECRET_ACCESS\n```\n\n#### `rails console` \n\nThe [Rails console](http://guides.rubyonrails.org/command_line.html#rails-console) lets you interact with the Rails API from the command line with Ruby. For instance:\n\n```\n\u003e a = Article.find_by(title: 'The Original Title')\n\u003e a.title = 'New Title'\n\u003e a.save\n```\n\nThis 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:\n\n1. 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 (ask the current Web Editors if you need help with that) and place it into the aforementioned directory. **NEVER** share this file publically.\n\n2. Use the `ssh` command to connect to our Lightsail instance. You can find the IP by following ssh instructions on [Lightsail](https://lightsail.aws.amazon.com/ls/webapp/home/instances)\n\n3. Once you're in, navigate to the directory of the Rails API (`cd /deploy/current`).\n\n6. Here, you can run `rails console` or `rails c` for short to manipulate the database.\n\nTo exit the Rails console, use `Ctrl-D`. To exit the SSH, use `Ctrl-D` as well.\n\n#### Deploying `stuy-spec-api`\n\n1. Go to your local `stuy-spec-api` directory and check out and pull down the branch you want to deploy.\n\n2. Run `bundle exec cap production deploy`. The automatic capistrano script spearheaded by [Darius Jankauskas](https://github.com/DJankauskas) should do the trick. Thanks Darius!\n\n## Testing GraphQL\n\nUse [GraphQL](https://github.com/graphql/graphiql) to test your GraphQL queries.\n\n\u003c!--\n![alt text](https://i.imgur.com/uti8BnI.png))\n# Docker\n\n## Setting Up\n1. Clone the repo (`git clone https://github.com/stuyspec/stuy-spec-api.git`)\n2. Install Ruby. We highly suggest rbenv or rvm\n3. Install Rails 5.1\n4. Install PostgreSQL (`brew install postgres` on Mac OS)\n5. Install Docker\n6. Create a file with name `.env` in the repository and write in it: `PG_HOST-db`. Run `docker-compose build`\n7. Run `docker-compose up`. If you get an error saying it can't connect to db, try stopping\nand rerunning.\n8. 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 like `Created database stuy-spec-api_development` was created. If so, then ignore the errors.\n9. Run `docker-compose run web rails db:migrate db:seed`\n10. To start the server, run `docker-compose run web rails server`.\n\n\n## Troubleshooting\n\n### Server exited at `docker-compose up`\nCheck the last few lines of the server log in your shell.\n1. A server is already running.\n```\nweb_1  | A server is already running. Check /stuy-spec-api/tmp/pids/server.pid.\nweb_1  | =\u003e Booting Puma\nweb_1  | =\u003e Rails 5.1.2 application starting in development on http://0.0.0.0:3000\nweb_1  | =\u003e Run `rails server -h` for more startup options\nweb_1  | Exiting\nstuyspecapi_web_1 exited with code 1\n```\nTo solve this problem, we need to remove the `server.pid` file. Navigate to the stuy-spec-api directory and run:\n```\nrm tmp/pids/server.pid\n```\n\n### Connection refused at `docker-compose run web ...`\n```\ncould not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n```\nYou might have a server already running that has not shut down correctly. Run `brew services stop postgresql`\n\nIn general, if you run into this error, the command may have already worked. Look at the top of the error. If you tried to run `docker-compose run web rails db:create` and, on top of the Connection refusal, it says \"Created database...\", the command worked. It may have interrupted the `db:migrate`, so run `docker-compose run web rails db:migrate` as an individual function separated from the `db:create`.\n\nIf that is not the case, run `postgres -D /usr/local/var/postgres`. You may see something like this:\n```\nFATAL:  lock file \"postmaster.pid\" already exists\nHINT:  Is another postmaster (PID 15556) running in data directory \"/usr/local/var/postgres\"?\n```\nRun `kill -9 THE_PID`, and you should be good to go.\n\n### Database drop/reset fails\n```\nCouldn't drop database 'stuy-spec-api_development'\nrails aborted!\nActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR:  database \"stuy-spec-api_development\" is being accessed by other users\nDETAIL:  There are {SOME_NUMBER} other sessions using the database.\n```\nThere is a rake task for deleting these sessions in `lib/tasks/kill_postgres_connections.rake`. To run the task, do\n```sh\ndocker-compose run web rake kill_postgres_connections\n```\nThis should kill related postgres connections, and database drop/reset should now work.\n\nIf dropping the database still does not work, use the initializer at `config/initializers/postgresql_database_tasks.rb` by adding an environment option to the rake task like so:\n```sh\ndocker-compose run web rake environment db:drop\n```\n\n### Cannot `bundle install`\nIf you need to add gems and the `bundle install` is [repetitively failing](https://stackoverflow.com/questions/6971290/running-bundle-install-fails-and-asks-me-to-run-bundle-install), you need to rebuild your Docker image to update the `Gemfile.lock`.\n```\n$ docker run web bundle install\n$ docker build\n```\n--\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuyspec%2Fstuyspec-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstuyspec%2Fstuyspec-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuyspec%2Fstuyspec-api/lists"}