Skip to content

Commit d59e9c1

Browse files
author
Felipe Rodrigues Michetti
committed
Added config files.
1 parent 4e377c3 commit d59e9c1

File tree

11 files changed

+26
-27
lines changed

11 files changed

+26
-27
lines changed

api/app.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../lib/loadpath'
22
require 'models/base'
33

4+
require 'yaml'
45
require 'sinatra'
56
require 'sinatra/cross_origin'
67
require 'sinatra/contrib/all'
@@ -10,15 +11,17 @@
1011

1112
# @class App
1213
class App < Sinatra::Application
13-
register Sinatra::SequelExtension, Sinatra::Namespace, Sinatra::CrossOrigin
14+
register Sinatra::SequelExtension, Sinatra::Namespace, Sinatra::CrossOrigin, Sinatra::ConfigFile
15+
config_file File.join("#{Dir.pwd}/lib/config/rack.conf.yml")
1416
extend Models
1517

16-
DATABASE = load_db
18+
DATASOURCE = load_db
19+
CLASS_FACTORY_OPTIONS = YAML.safe_load(File.open("#{Dir.pwd}/lib/config/class_factory.conf.yml"))
1720

1821
require 'requires'
1922
require 'aliases'
2023

21-
%w[controllers].each {|folder| Dir["#{Dir.pwd}/lib/#{folder}/*.rb"].each {|file| require file}}
24+
%w[controllers].each {|folder| Dir["#{Dir.pwd}/lib/#{folder}/*.rb"].each { |file| require file}}
2225

2326
Controllers.constants.each{|ctrl_sym|
2427
module_name = Kernel.const_get('Controllers::?'.gsub('?', ctrl_sym.to_s))
@@ -28,17 +31,16 @@ class App < Sinatra::Application
2831
extend Utils::ClassFactory
2932

3033
configure {
31-
set :environment, :development
34+
set :environment, settings.environment
3235
set server: 'webrick'
33-
34-
set :raise_errors, true
35-
set :show_exceptions, true
36-
37-
enable :cross_origin
38-
36+
set :raise_errors, settings.raise_errors
37+
set :show_exceptions, settings.show_exceptions
3938
set :root, File.dirname(__FILE__)
4039
set :public_folder, File.join(root, '../', 'public')
40+
}
4141

42+
configure(:development) {
43+
enable :cross_origin
4244
set :allow_origin, :any
4345
set :allow_methods, %i[get post put delete options]
4446
set :allow_credentials, true
@@ -65,7 +67,7 @@ def sample_method
6567
end
6668
}
6769

68-
ClassMap = create_classes(DATABASE, Dynamics, false)
70+
ClassMap = create_classes(DATASOURCE, Dynamics, CLASS_FACTORY_OPTIONS['name_conflicts'])
6971
Classes = get_classes(Dynamics)
7072

7173
require 'extensions/models/dynamics_common'

docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ services:
88
POSTGRES_USER: postgres
99
POSTGRES_PASSWORD: postgres
1010
ports:
11-
- "5423:5432"
11+
- "5432:5432"
1212
expose:
1313
- "5432"
14-
- "5423"
1514
ruby:
1615
image: ruby:2.5.0
1716
environment:
1817
RACK_ENV: DEV
1918
RUBYLIB: ./lib
2019
DATABASE_URL_LOCAL: postgres://postgres:postgres@localhost:5422/postgres
2120
build: .
22-
command: rackup config.ru -E DEV
21+
command: rackup config.ru -E $RACK_ENV
2322
ports:
2423
- "9595:9595"
2524
depends_on:

docker/docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# Use postgres/example user/password credentials
21
version: '3.1'
3-
42
services:
53
postgres:
64
image: postgres:9.6

lib/config/class_factory.conf.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# if name conflicts = true, don't singularize class names
2+
name_conflicts: true

lib/config/rack.conf.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
environment: development
2+
raise_errors: true
3+
show_exceptions: true

lib/controllers/mini_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def included(controller)
1515
c.namespace('/tables') {|c|
1616
c.get('') {
1717
make_default_json_api(self) {
18-
{database: App::DATABASE.opts[:database], schema: App::ClassMap.first[:schema], tables: App::ClassMap}
18+
{database: App::DATASOURCE.opts[:database], schema: App::ClassMap.first[:schema], tables: App::ClassMap}
1919
}
2020
}
2121

lib/extensions/models/dynamics_common.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
require 'inflector'
2-
31
module Extensions
42
module DynamicsCommon
53
Dynamics.constants.each {|klass_sym|
64
klass = Kernel.const_get('Dynamics::?'.gsub('?', klass_sym.to_s))
75
klass.class_eval {
86
# Do everything(whatever) you want here.
9-
include Inflector
107
puts "Extending #{klass.name}"
118

129
class << self

lib/helpers/api_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module ApiBuilder
1212
include Utils::ClassFactory
1313

1414
CONTENT_TYPE = 'application/json;charset=utf-8'
15+
RESCUES = [ModelException, UniqueConstraintViolation, ConstraintViolation, CheckConstraintViolation,
16+
NotNullConstraintViolation, ForeignKeyConstraintViolation, MassAssignmentRestriction, ValidationFailed].freeze
1517

1618
def make_default_json_api(api_instance, payload = {}, table_name = nil)
1719
request_method = api_instance.env['REQUEST_METHOD']
@@ -44,8 +46,7 @@ def make_default_json_api(api_instance, payload = {}, table_name = nil)
4446
else
4547
response = { msg: 'Api not implemented yet.' }
4648
end
47-
rescue ModelException, ConstraintViolation, UniqueConstraintViolation, CheckConstraintViolation,
48-
NotNullConstraintViolation, ForeignKeyConstraintViolation, MassAssignmentRestriction, ValidationFailed => e
49+
rescue RESCUES => e
4950
status = 400
5051
response = prepare_error_response(e)
5152
end

lib/models/base.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'yaml'
22
require 'sequel'
33
require 'sequel/adapters/postgresql'
4-
54
require_relative '../utils/discover_os'
65

76
module Models

lib/utils/class_factory.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ def create_classes(connection, module_constant, name_conflicts = true)
1717
next if t[:table_name].eql?('configurations')
1818

1919
name = t[:table_name]
20-
schema = t[:schema]
20+
_schema = t[:schema]
2121

2222
constraints = table_constraints(connection, name)
2323
foreign_keys = table_foreign_keys(connection, name)
2424
fields = scan_fields(connection, name)
2525
t[:constraints] = constraints.map {|tc| {type: tc[:constraint_type], name: tc[:constraint_name]}}
2626
t[:foreign_keys] = foreign_keys
27-
primary_key_name = t[:constraints].detect {|tc| tc[:type].eql?('PRIMARY KEY')}[:name] rescue nil
27+
_primary_key_name = t[:constraints].detect {|tc| tc[:type].eql?('PRIMARY KEY')}[:name] rescue nil
2828
t[:scanned_fields] = fields
2929
t[:columns_n_types] = table_columns(connection, name)
3030

0 commit comments

Comments
 (0)