From 080c8cb713d88bc7464cf61b119447d71e36e0b5 Mon Sep 17 00:00:00 2001 From: duffn <3457341+duffn@users.noreply.github.com> Date: Tue, 21 Jun 2022 08:48:48 -0600 Subject: [PATCH] Rename root route module (#5) --- .gitignore | 2 +- README.md | 8 +++++ Rakefile | 5 ++- app/api/helpers.rb | 10 +++--- app/root.rb | 62 +++++++++++++++++++----------------- config.ru | 4 +-- config/boot.rb | 1 - config/jwt/.gitkeep | 0 docker-compose.yml | 1 - spec/api/hello_world_spec.rb | 4 +-- spec/api/session_spec.rb | 4 +-- spec/api/widget_spec.rb | 4 +-- spec/spec_helper.rb | 8 ----- 13 files changed, 57 insertions(+), 56 deletions(-) create mode 100644 config/jwt/.gitkeep diff --git a/.gitignore b/.gitignore index 2e218e3..9be0675 100644 --- a/.gitignore +++ b/.gitignore @@ -122,5 +122,5 @@ build-iPhoneSimulator/ # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) .idea/ -config/jwt/* +config/jwt/*.key* .dccache diff --git a/README.md b/README.md index 62c680d..35c231d 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,14 @@ openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub docker compose up --build ``` +- Setup and seed the database. + +``` +docker compose exec app bundle exec rake db:setup +``` + +- Visit your API at http://localhost:3000 + ## Creating a user - Run the create users Rake task. diff --git a/Rakefile b/Rakefile index 7ec38ff..13bac88 100755 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,6 @@ #!/usr/bin/env rake # frozen_string_literal: true -require 'rubygems' require 'bundler' ENV['RAKE_ENV'] ||= 'development' @@ -37,12 +36,12 @@ begin require 'bundler/audit/task' Bundler::Audit::Task.new rescue LoadError - puts 'Not loading RSpec or Rubocop.' + puts 'Not loading development only rake tasks.' end # Shows app routes task routes: :environment do - API::Root.routes.each do |route| + GrapeApiBoilerplate::Api::Root.routes.each do |route| method = route.request_method.ljust(10) path = route.origin puts " #{method} #{path}" diff --git a/app/api/helpers.rb b/app/api/helpers.rb index 26ffb36..66532f5 100644 --- a/app/api/helpers.rb +++ b/app/api/helpers.rb @@ -1,9 +1,11 @@ # frozen_string_literal: true -module API - module Helpers - def logger - Root.logger +module GrapeApiBoilerplate + module Api + module Helpers + def logger + Root.logger + end end end end diff --git a/app/root.rb b/app/root.rb index cf427ee..55b0c2a 100644 --- a/app/root.rb +++ b/app/root.rb @@ -1,41 +1,43 @@ # frozen_string_literal: true -module API - class Root < Grape::API - format :json - default_format :json - content_type :json, 'application/json' - prefix :api +module GrapeApiBoilerplate + module Api + class Root < Grape::API + format :json + default_format :json + content_type :json, 'application/json' + prefix :api - rescue_from :grape_exceptions + rescue_from :grape_exceptions - # Logging - insert_before Grape::Middleware::Error, GrapeLogging::Middleware::RequestLogger, - { - logger:, - formatter: GrapeLogging::Formatters::Json.new, - include: [GrapeLogging::Loggers::FilterParameters.new([:password])] - } + # Logging + insert_before Grape::Middleware::Error, GrapeLogging::Middleware::RequestLogger, + { + logger:, + formatter: GrapeLogging::Formatters::Json.new, + include: [GrapeLogging::Loggers::FilterParameters.new([:password])] + } - # Helpers - helpers do - include Helpers - end + # Helpers + helpers do + include Helpers + end - # Routes - mount GrapeApiBoilerplate::Api::Endpoints::Session - mount GrapeApiBoilerplate::Api::Endpoints::V1::HelloWorldEndpoint - mount GrapeApiBoilerplate::Api::Endpoints::V1::WidgetEndpoint + # Routes + mount GrapeApiBoilerplate::Api::Endpoints::Session + mount GrapeApiBoilerplate::Api::Endpoints::V1::HelloWorldEndpoint + mount GrapeApiBoilerplate::Api::Endpoints::V1::WidgetEndpoint - add_swagger_documentation \ - info: { - title: 'Grape Boilerplate', - description: 'A full-featured API boilerplate to get you started with the Grape framework.' - } + add_swagger_documentation \ + info: { + title: 'Grape Boilerplate', + description: 'A full-featured API boilerplate to get you started with the Grape framework.' + } - # Handle 404s - route :any, '*path' do - error!({ message: 'resource does not exist' }, 404) + # Handle 404s + route :any, '*path' do + error!({ message: 'resource does not exist' }, 404) + end end end end diff --git a/config.ru b/config.ru index bbeef17..61ca1ce 100644 --- a/config.ru +++ b/config.ru @@ -14,5 +14,5 @@ use OTR::ActiveRecord::ConnectionManagement # Load Swagger UI when running locally. use Rack::Static, urls: ['/public/swagger'] unless ENV['RACK_ENV'] == 'production' -API::Root.compile! -run API::Root +GrapeApiBoilerplate::Api::Root.compile! +run GrapeApiBoilerplate::Api::Root diff --git a/config/boot.rb b/config/boot.rb index b5b6622..fc4da00 100644 --- a/config/boot.rb +++ b/config/boot.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true -require 'rubygems' require 'bundler/setup' require 'rack/cors' diff --git a/config/jwt/.gitkeep b/config/jwt/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/docker-compose.yml b/docker-compose.yml index a5908f5..18ad282 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,6 @@ services: build: context: . dockerfile: dev.Dockerfile - container_name: ruby-grape environment: RACK_ENV: development WEB_CONCURRENCY: "0" diff --git a/spec/api/hello_world_spec.rb b/spec/api/hello_world_spec.rb index e4f4dc7..6f666cd 100644 --- a/spec/api/hello_world_spec.rb +++ b/spec/api/hello_world_spec.rb @@ -2,11 +2,11 @@ require 'spec_helper' -describe API::Root do +describe GrapeApiBoilerplate::Api::Root do include Rack::Test::Methods def app - API::Root + GrapeApiBoilerplate::Api::Root end it 'gets hello world' do diff --git a/spec/api/session_spec.rb b/spec/api/session_spec.rb index 01e210e..4e440c1 100644 --- a/spec/api/session_spec.rb +++ b/spec/api/session_spec.rb @@ -2,11 +2,11 @@ require 'spec_helper' -describe API::Root do +describe GrapeApiBoilerplate::Api::Root do include Rack::Test::Methods def app - API::Root + GrapeApiBoilerplate::Api::Root end before do diff --git a/spec/api/widget_spec.rb b/spec/api/widget_spec.rb index 674e40f..56b245f 100644 --- a/spec/api/widget_spec.rb +++ b/spec/api/widget_spec.rb @@ -2,11 +2,11 @@ require 'spec_helper' -describe API::Root do +describe GrapeApiBoilerplate::Api::Root do include Rack::Test::Methods def app - API::Root + GrapeApiBoilerplate::Api::Root end before do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 776179e..c3c5a15 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'rubygems' - ENV['RACK_ENV'] ||= 'test' require 'simplecov' @@ -31,9 +29,3 @@ # Tell RSpec not to truncate output. RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil - -# require 'simplecov' -# SimpleCov.start -# -# require 'codecov' -# SimpleCov.formatter = SimpleCov::Formatter::Codecov