Skip to content

Commit

Permalink
Rename root route module (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
duffn authored Jun 21, 2022
1 parent 59d6a6b commit 080c8cb
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env rake
# frozen_string_literal: true

require 'rubygems'
require 'bundler'

ENV['RAKE_ENV'] ||= 'development'
Expand Down Expand Up @@ -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}"
Expand Down
10 changes: 6 additions & 4 deletions app/api/helpers.rb
Original file line number Diff line number Diff line change
@@ -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
62 changes: 32 additions & 30 deletions app/root.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'
require 'rack/cors'
Empty file added config/jwt/.gitkeep
Empty file.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ services:
build:
context: .
dockerfile: dev.Dockerfile
container_name: ruby-grape
environment:
RACK_ENV: development
WEB_CONCURRENCY: "0"
Expand Down
4 changes: 2 additions & 2 deletions spec/api/hello_world_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/api/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/api/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'rubygems'

ENV['RACK_ENV'] ||= 'test'

require 'simplecov'
Expand Down Expand Up @@ -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

0 comments on commit 080c8cb

Please sign in to comment.