This is a WIP template that can be used to create a simple backend service in ruby. It was just recently repurposed, so some things may still need cleanup. It's generally not finished yet, but you can already use it and it should be functional.
The service currently supports a simple User entity that can have a hashed password (with salt and pepper as well).
I will probably add some more security measurements to this template in the future.
docker compose up -d
Make sure that your .env files are correctly configured for what you need and want.
# db creation
ENV=db bundle exec rake db:create
# db migration
ENV=db bundle exec rake db:migrate
# or both
ENV=db bundle exec rake db:create db:migrate
(It can be dropped by running db:drop if you need to clean something up)
Rubocop:
bundle exec rubocop
# or, if you want it to autocorrect (when possible)
bundle exec rubocop -a
Run with the -A flag if you want it to autocorrect things if possible
Tests:
bundle exec rspec
Both:
bundle exec rake
bundle exec puma -p 8000
After starting, you can check if the service is up and running by calling the /healthz route (just add "/healthz" to the end of the URL).
- Add a migration file with the table structure to
./lib/migrations(Make sure to migrate your database afterwards) - Add a new matching model for that table to
./lib/models - Add a new controller (or expand an already existing one if it matches the purpose) to
./lib/web/controllers - Make the router in
./lib/web/router.rbsupport newly added controllers - Add routes and functionality to your controller
I'd suggest keeping the code clean by using serializer-, service- and repository-classes to encapsulate your functionality when adding new routes.
This repository also makes use of the factory_bot gem. Make sure to use it when writing tests.
Generate api_docs.yml (requires npm):
npx swagger-cli bundle openapi/openapi.yml --dereference -o openapi/api_docs.yml
The resulting file can be used for frontend type generation and visual/interactive API documentation, for example.