Giter Club home page Giter Club logo

rails_api_base's Introduction

Rails API Template

Github Actions CI Code Climate Test Coverage

Rails API Base is a boilerplate project for JSON RESTful APIs. It follows the community best practices in terms of standards, security and maintainability, integrating a variety of testing and code quality tools. It's based on Rails 7.1 and Ruby 3.3.

Finally, it contains a plug an play Administration console (thanks to ActiveAdmin).

Features

This template comes with:

  • Schema
    • Users table
    • Admin users table
  • Endpoints
    • Sign up with user credentials
    • Sign in with user credentials
    • Sign out
    • Reset password
    • Get and update user profile
  • Administration panel
  • Feature flags support with a UI for management
  • Code quality tools
  • RSpec tests
  • RSpec API Doc Generator
  • API documentation following OpenAPI
  • Docker support

How to use

  1. Clone this repo
  2. Install PostgreSQL in case you don't have it
  3. Install node and yarn. Expected node version ">=16 || 14 >=14.17".
  4. Run bootstrap.sh with the name of your project like ./bin/bootstrap.sh --name=my_awesome_project
  5. Run yarn install and yarn build --watch. This bundles the JS assets in the administration site using esbuild.
  6. bundle exec rspec and make sure all tests pass (non-headless mode) or HEADLESS=true bundle exec rspec (headless mode)
  7. Run bin/dev.
  8. You can now try your REST services!

How to use with Docker

  1. Have docker and docker-compose installed (You can check this by doing docker -v and docker-compose -v)
  2. Run bootstrap.sh with the name of your project and the -d or --for-docker flag like ./bin/bootstrap.sh --name=my_awesome_project -d
    1. Run ./bin/bootstrap.sh --help for the full details.
  3. (Optional) If you want to deny access to the database from outside of the docker-compose network, remove the ports key in the docker-compose.yml from the db service.
  4. (Optional) Run the tests to make sure everything is working with: bin/rspec ..
  5. You can now try your REST services!

Dev scripts

This template provides a handful of scripts to make your dev experience better!

  • bin/bundle to run any bundle commands.
    • bin/bundle install
  • bin/rails to run any rails commands
    • bin/rails console
  • bin/web to run any bash commands
    • bin/web ls
  • bin/rspec to run specs
    • bin/rspec .
  • bin/dev to run both Rails and JS build processes at the same time in a single terminal tab.
    • bin/dev

You don't have to use these but they are designed to run the same when running with Docker or not. To illustrate, bin/rails console will run the console in the docker container when running with docker and locally when not.

Gems

Optional configuration

  • Set your frontend URL in config/initializers/rack_cors.rb
  • Set your mail sender in config/initializers/devise.rb
  • Config your timezone accordingly in application.rb
  • Config CI parallel execution. See docs
  • Fullstack development. See docs.

API Docs

See API documentation docs for more info

Code quality

With bundle exec rails code:analysis you can run the code analysis tool, you can omit rules with:

Impersonation

The rails_api_base incorporates a user impersonation feature, allowing AdminUsers to assume the identity of other Users. This feature is disabled by default.

See Impersonation docs for more info

Monitoring

In order to use New Relic to monitor your application requests and metrics, you must setup NEW_RELIC_API_KEY and NEW_RELIC_APP_NAME environment variables. To obtain an API key you must create an account in the platform.

Configuring Code Climate

  1. After adding the project to CC, go to Repo Settings
  2. On the Test Coverage tab, copy the Test Reporter ID
  3. Set the current value of CC_TEST_REPORTER_ID in the GitHub secrets and variables

Code Owners

You can use CODEOWNERS file to define individuals or teams that are responsible for code in the repository.

Code owners are automatically requested for review when someone opens a pull request that modifies code that they own.

Credits

Rails API Base is maintained by Rootstrap with the help of our contributors.

rails_api_base's People

Contributors

adicaff avatar blacksam07 avatar brunoparma88 avatar brunvez avatar dependabot[bot] avatar enzofab91 avatar fedeagripa avatar frankvielma avatar gakindustries avatar grilix avatar guilleleopold avatar guillermoap avatar jeroig avatar jpascual1994 avatar juan-apa avatar juanmanuelramallo avatar juannferrari avatar maicolben avatar martinjaimem avatar matiasmansilla1989 avatar mrodriguez90 avatar mwolman avatar perezignacio avatar rodrieiz avatar santiagovidal avatar santib avatar sebaherrera07 avatar sebastiancaraballo avatar timoperaza avatar vitogit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails_api_base's Issues

Update dependencies

I'm creating this issue so we do a general update on our dependencies. It's been a while since we updated them.

Some examples are:

  • draper
  • jbuilder
  • pg (this one in particular)
  • activeadmin

Replace jbuilder

Check jb, it's considered faster than jbuilder

Another alternative faster and cleaner is fast_jsonapi which is based on serializers, so it's like having views + decorators in the same file.

Feel free to close this issue if none of these gems is a good replacement.

Several database queries happening after processing the request

After the server processes the request and renders the Jbuilder view, the server performs several queries to the users table and it's not clear why it's doing that. It also appears to do a transaction even though it doesn't perform an update on the database.

Below there's a screenshot of the server output:
screen shot 2017-09-14 at 4 17 09 pm

ActiveAdmin panel for users should require password

Steps to reproduce the bug:

  1. Go to the admin panel
  2. Go to New User
  3. Fill every field in the form

Expected result: the user should be created successfully.
Actual result: the user is not created.

Reason: the user is rejected if no password is set.

Make easy to login with other methods like username

Right now you have to paste a huge method from the sessions controller from devise token auth and remove the constraint: q = “#{field.to_s} = ? AND provider=‘email’“, it will be great if the method is split, but the maintainers of the gem aren't active as before. Maybe we should paste the code to our sessions controller

Returns error key instead of errors when there is only one error

Nowadays, when there is any kind of error, the base returns a json response with:

However, we are using devise_token_auth_gem gem and they return a json response with the key errors doesn't matter if there is only one error or more than one. Example: https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/sessions_controller.rb#L131

The proposed solution is override devise_token_auth render error methods and adjust them to our current standard.

Add/Define a new code_climate.yml file.

This PR #90 removes the current Code Climate configuration since it was invalid.

We should discuss and define the proper configuration and creating a new code_climate.yml file according with what we want.

Add shoulda

Tasks:

  • Add gem

  • Add config in rspec

  • Add some tests with shoulda

Rack attack

https://github.com/kickstarter/rack-attack

Rack attack give apps the ability to throttle and block user IPs. This can be helpful for preventing brute-force passwords attacks, scrapers, DDoS attacks, or any other offenders by throttling requests from IP addresses making large volumes of requests.

They even provide a sample configuration that would work most of the times (maybe it doesn't work with sophisticated hackers). https://github.com/kickstarter/rack-attack/blob/master/docs/example_configuration.md

Would it be valuable to add rack attack and base config to this repo?

Reek and brakeman are not running in CodeClimate

Reek is currently not running in CodeClimate because the channel that we specified (reek-5-3-1) is not available.

Apparently the only channel available is stable, which means that we don't have control over what version of reek to run in CodeClimate.

For this reason, I believe it would be best to remove it from CodeClimate and run it only in CircleCI, same as Rails Best Practices

Use I18n

Use it in every text that is going to be used. For example, error messaging in ApiController

Sign Up is wrongly requiring an "Accept: application/json" header to work

A request like this one

curl 
-H "Content-Type: application/json"
-X POST
-d '{ "user": { "username": "test", "email": "[email protected]", "password": "password", "password_confirmation": "password" } }' 
http://localhost:3000/api/v1/users

Is raising an ActionController::InvalidAuthenticityToken exception unless you add a "Accept: application/json" header too, which shouldn't be necessary

ActionViewTemplateError when record is not found in a view

When:
When trying to call a helper method inside a view and the helper hasn't been called before

What:
If the record is not found, it raises an error that it's catched by the render_error method in the ApiController instead of the render_record_not_found method.

Why:
When raising the error in the view, instead of raising an ActiveRecord:RecordNotFound error, it's raising an ActionViewTemplateError and that's why is not being catched by the render_record_not_found

For further details, ask @jdrosales17 who is the one that found the issue

Error 500 on check_json_request when content-type is missing

When the content-type header is missing, the match? method in check_json_request causes a 500 error as try to do match? for a nil object.

  • Version: v.1.0.5
  • Ruby Version: 2.4.1
  • Environment: development and staging
  • Rails Stacktrace:
    2018-05-18T13:22:39.551097+00:00 app[web.1]: E, [2018-05-18T13:22:39.549374 #4] ERROR -- : [042ea98c-c045-4129-814b-1429ebeaf83e] undefined method `match?' for nil:NilClass 2018-05-18T13:22:39.553823+00:00 app[web.1]: I, [2018-05-18T13:22:39.553748 #4] INFO -- : [042ea98c-c045-4129-814b-1429ebeaf83e] Completed 500 Internal Server Error in 5ms (Views: 0.3ms | ActiveRecord: 0.0ms)

problem with Fog?

last time i try this and i don't have a problem and last night i try to clone and everything setup but i got this problem with Fog?

after i try to bundle exec rake db:migrate

rake aborted!
NameError: uninitialized constant CarrierWave::Storage::Fog
/Users/ericsonluciano/code/side_projects/www.hospitalitycareers.com/server/config/initializers/carrierwave.rb:9:in `block in <top (required)>'
/Users/ericsonluciano/code/side_projects/www.hospitalitycareers.com/server/config/initializers/carrierwave.rb:1:in `<top (required)>'
/Users/ericsonluciano/code/side_projects/www.hospitalitycareers.com/server/config/environment.rb:5:in `<top (required)>'
/Users/ericsonluciano/.rbenv/versions/2.4.1/bin/bundle:22:in `load'
/Users/ericsonluciano/.rbenv/versions/2.4.1/bin/bundle:22:in `<main>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with `--trace)`

Remove draper gem

As discussed, draper adds complexity that we don't really need. We'll replace our current presenter by making a PORO that inherits from a BasePresenter. We should also add some documentation to this BasePresenter on when and how to use presenters, maybe by linking to a place where this is well explained.

Increase version of rubocop

The current version of rubocop complains if you try to use the Ruby Safe navigator.

Using the newest version should resolve the issue.

ActionCable authentication

The current implementation it's not compatible with the officially supported npm module. It requires a token to be added to a header.
Maybe a previously generated token could be sent during the handshake, as explained in this Heroku article

Feature idea: forced update

I'm creating this issue to keep track of an idea that has been going around for some time: forced updates for mobile apps.

Remove factories/spec

Pasting @grilix's arguments here which makes totally sense:

Testing factories doesn't really make sense, factories are part of the specs so it's like writing tests for the specs. Secs for the models are already in place in /spec/models.

Also, FactoryGirl will load and execute all the files inside /spec/factories with no specific order, which means: 1. There's no guarantee this spec is going to test all the factories, 2. There's no way of turning these specs (for example if we want to run just one specific file), which results in confusing output.

So we should remove them, another possibility will be using FactoryBot.lint.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.