tzispa 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4afde718b4afe167e5e25031d064aa92c826162b
4
- data.tar.gz: 51c74b98a6a21715a6cd1f23dcf70b5974c1211b
3
+ metadata.gz: ddd8b371c81a060fcfbe8d43042605073ee6961e
4
+ data.tar.gz: 72ecddae14e7e16f2c5f3a905941493b35364645
5
5
  SHA512:
6
- metadata.gz: 63d7bb48cd522eacd349ca5284271fc30ae263558f82a959e4308e31a5ed5e9de56b7cb77489b9bd07faf6382394f8243cee3dabe7b4dd5b399911f6818654fb
7
- data.tar.gz: 93450d79ad98a19086ff28b9555efb179bb3da19f4565e8157569b5ad1d44085dc9ee2978da87c4ba3724a8e88cb2efab3277e43ad4c413bb85be20420032fe7
6
+ metadata.gz: d968dca749d0d728d13a0294bc327010aee8845b17153b6d63916e350cb0a0fb052589348a685faf8c8dfe0704eb090108364845b94d8fa97a63a90cbb318b89
7
+ data.tar.gz: d79c5b31526e60335a8737b30ae6cab4cdfed10e4d69dafd67ef48badfe2ef91c4ae680adb93fa277fdebf7db01cec4c72592c241ed4c4cf97624143c92b79d1
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@ Tzispa
2
2
 
3
3
  General purpose web framework
4
4
 
5
+ ## v0.8.1
6
+ - rig api handler moved to tzispa_rig gem
7
+ - environment constants refactoring & improves env key access throught method_missing
8
+ - fix applocation global mutext for apps boot & initialization
9
+ - postpone server bind host/port to the Puma web server configuration phase
10
+
5
11
  ## v0.8.0
6
12
  - introducing support for other template engines
7
13
  - improve errors log
data/lib/tzispa/app.rb CHANGED
@@ -16,6 +16,8 @@ module Tzispa
16
16
 
17
17
  include Tzispa::Engine
18
18
 
19
+ @@appmutex = Mutex.new
20
+
19
21
  attr_reader :domain, :logger, :map_path, :engine, :routes
20
22
  def_delegators :@domain, :name, :path
21
23
 
@@ -28,15 +30,12 @@ module Tzispa
28
30
 
29
31
  # rubocop:disable Style/ClassVars
30
32
  def applications
33
+ return __applications_container if @@appmutex.locked?
31
34
  synchronize do
32
- @@applications ||= Hash.new { |_, key| raise UnknownApplication(key.to_s) }
35
+ __applications_container
33
36
  end
34
37
  end
35
38
 
36
- def synchronize
37
- Mutex.new.synchronize { yield }
38
- end
39
-
40
39
  def [](name)
41
40
  applications[name]
42
41
  end
@@ -47,6 +46,16 @@ module Tzispa
47
46
  applications[app.name] = app
48
47
  end
49
48
  end
49
+
50
+ def synchronize
51
+ @@appmutex.synchronize { yield }
52
+ end
53
+
54
+ private
55
+
56
+ def __applications_container
57
+ @@applications ||= Hash.new { |_, key| raise UnknownApplication(key.to_s) }
58
+ end
50
59
  end
51
60
 
52
61
  def initialize(appid, engine:, on: nil, &block)
@@ -38,13 +38,13 @@ module Tzispa
38
38
 
39
39
  def handler_code(namespace, class_name)
40
40
  Tzispa::Utils::Indenter.new.tap do |code|
41
- code << "require 'tzispa/api/handler'\n\n"
41
+ code << "require 'tzispa/rig/handler'\n\n"
42
42
  level = 0
43
43
  namespace.split('::').each do |ns|
44
44
  level.positive? ? code.indent << "module #{ns}\n" : code << "module #{ns}\n"
45
45
  level += 1
46
46
  end
47
- code.indent << "\nclass #{class_name} < Tzispa::Api::Handler\n\n"
47
+ code.indent << "\nclass #{class_name} < Tzispa::Rig::Handler\n\n"
48
48
  code << "end\n\n"
49
49
  namespace.split('::').each { code.unindent << "end\n" }
50
50
  end.to_s
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tzispa
4
+ module Config
5
+ module Environment
6
+
7
+ RACK_ENV = 'RACK_ENV'
8
+
9
+ TZISPA_ENV = 'TZISPA_ENV'
10
+
11
+ DEVELOPMENT_ENV = 'development'
12
+
13
+ DEFAULT_ENV = 'development'
14
+
15
+ PRODUCTION_ENV = 'deployment'
16
+
17
+ RACK_ENV_DEPLOYMENT = 'deployment'
18
+
19
+ DEFAULT_DOTENV_ENV = '.env.%s'
20
+
21
+ DEFAULT_CONFIG = 'config'
22
+
23
+ TZISPA_HOST = 'TZISPA_HOST'
24
+
25
+ TZISPA_SSL = 'TZISPA_SSL'
26
+
27
+ TZISPA_SERVER_HOST = 'TZISPA_SERVER_HOST'
28
+
29
+ DEFAULT_HOST = 'localhost'
30
+
31
+ TZISPA_PORT = 'TZISPA_PORT'
32
+
33
+ TZISPA_SERVER_PORT = 'TZISPA_SERVER_PORT'
34
+
35
+ DEFAULT_PORT = 9412
36
+
37
+ DEFAULT_RACKUP = 'tzispa.ru'
38
+
39
+ DEFAULT_ENVIRONMENT_CONFIG = 'environment'
40
+
41
+ DEFAULT_DOMAINS_PATH = 'apps'
42
+
43
+ DOMAINS = 'domains'
44
+
45
+ DOMAINS_PATH = 'apps/%s'
46
+
47
+ APPLICATION = 'application'
48
+
49
+ APPLICATION_PATH = 'app'
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tzispa
4
+ module Config
5
+ module Rc
6
+
7
+ DEFAULT_ARCHITECTURE = 'domains'
8
+
9
+ APP_ARCHITECTURE = 'app'
10
+
11
+ ARCHITECTURE_KEY = 'architecture'
12
+
13
+ PROJECT_NAME = 'project'
14
+
15
+ DEFAULT_TEST_SUITE = 'minitest'
16
+
17
+ TEST_KEY = 'test'
18
+
19
+ end
20
+ end
21
+ end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'tzispa/engine/rig/layout'
4
- require 'tzispa/helpers/login'
4
+ require 'tzispa/helpers/session_auth'
5
5
 
6
6
  module Tzispa
7
7
  module Engine
8
8
  module Rig
9
9
 
10
10
  class AuthLayout < Tzispa::Engine::Rig::Layout
11
- include Tzispa::Helpers::Login
11
+ include Tzispa::Helpers::SessionAuth
12
12
 
13
13
  before :login_redirect
14
14
  end
data/lib/tzispa/env.rb CHANGED
@@ -17,6 +17,10 @@ module Tzispa
17
17
  @env[key] = value
18
18
  end
19
19
 
20
+ def key?(key)
21
+ @env.key? key
22
+ end
23
+
20
24
  def load!(path)
21
25
  return unless defined?(Dotenv)
22
26
 
@@ -6,59 +6,17 @@ require 'singleton'
6
6
  require 'tzispa/env'
7
7
  require 'tzispa/tzisparc'
8
8
  require 'tzispa/utils/hash'
9
+ require 'tzispa/config/environment'
9
10
 
10
11
  module Tzispa
11
12
 
12
13
  class Environment
13
14
  include Singleton
15
+ include Tzispa::Config::Environment
14
16
  using Tzispa::Utils::TzHash
15
17
 
16
18
  LOCK = Mutex.new
17
19
 
18
- RACK_ENV = 'RACK_ENV'
19
-
20
- TZISPA_ENV = 'TZISPA_ENV'
21
-
22
- DEVELOPMENT_ENV = 'development'
23
-
24
- DEFAULT_ENV = 'development'
25
-
26
- PRODUCTION_ENV = 'deployment'
27
-
28
- RACK_ENV_DEPLOYMENT = 'deployment'
29
-
30
- DEFAULT_DOTENV_ENV = '.env.%s'
31
-
32
- DEFAULT_CONFIG = 'config'
33
-
34
- TZISPA_HOST = 'TZISPA_HOST'
35
-
36
- TZISPA_SSL = 'TZISPA_SSL'
37
-
38
- TZISPA_SERVER_HOST = 'TZISPA_SERVER_HOST'
39
-
40
- DEFAULT_HOST = 'localhost'
41
-
42
- TZISPA_PORT = 'TZISPA_PORT'
43
-
44
- TZISPA_SERVER_PORT = 'TZISPA_SERVER_PORT'
45
-
46
- DEFAULT_PORT = 9412
47
-
48
- DEFAULT_RACKUP = 'tzispa.ru'
49
-
50
- DEFAULT_ENVIRONMENT_CONFIG = 'environment'
51
-
52
- DEFAULT_DOMAINS_PATH = 'apps'
53
-
54
- DOMAINS = 'domains'
55
-
56
- DOMAINS_PATH = 'apps/%s'
57
-
58
- APPLICATION = 'application'
59
-
60
- APPLICATION_PATH = 'app'
61
-
62
20
  # rubocop:disable Style/ClassVars
63
21
  @@opts = {}
64
22
 
@@ -69,22 +27,34 @@ module Tzispa
69
27
  LOCK.synchronize { set_env_vars! }
70
28
  end
71
29
 
72
- def self.opts=(hash)
73
- @@opts = hash.to_h.dup
74
- end
30
+ class << self
31
+ def opts=(hash)
32
+ @@opts = hash.to_h.dup
33
+ end
75
34
 
76
- def self.[](key)
77
- instance[key]
78
- end
35
+ def [](key)
36
+ instance[key]
37
+ end
79
38
 
80
- def self.development?
81
- instance.development?
39
+ def method_missing(name, *args, &block)
40
+ if instance.respond_to? name
41
+ instance.send name, *args, &block
42
+ elsif instance.key? ekup = name.to_s.upcase
43
+ instance[ekup]
44
+ else
45
+ super
46
+ end
47
+ end
82
48
  end
83
49
 
84
50
  def [](key)
85
51
  @env[key]
86
52
  end
87
53
 
54
+ def key?(key)
55
+ @env.key? key
56
+ end
57
+
88
58
  def environment
89
59
  @environment ||= env[TZISPA_ENV] || rack_env || DEFAULT_ENV
90
60
  end
@@ -105,21 +75,25 @@ module Tzispa
105
75
  [:default, environment.to_sym]
106
76
  end
107
77
 
78
+ def root
79
+ @root ||= Pathname.new(Dir.pwd)
80
+ end
81
+
82
+ def config
83
+ @config ||= root.join(@options.fetch(:config) { DEFAULT_CONFIG })
84
+ end
85
+
108
86
  def project_name
109
87
  @options.fetch(:project)
110
88
  end
111
89
 
112
90
  def architecture
113
91
  @options.fetch(:architecture) do
114
- puts "Tzispa architecture unknown: see `.tzisparc'"
92
+ warn "Tzispa architecture unknown: see `.tzisparc'"
115
93
  exit 1
116
94
  end
117
95
  end
118
96
 
119
- def root
120
- @root ||= Pathname.new(Dir.pwd)
121
- end
122
-
123
97
  def apps_path
124
98
  @options.fetch(:path) do
125
99
  case architecture
@@ -131,10 +105,6 @@ module Tzispa
131
105
  end
132
106
  end
133
107
 
134
- def config
135
- @config ||= root.join(@options.fetch(:config) { DEFAULT_CONFIG })
136
- end
137
-
138
108
  def host
139
109
  @host ||= @options.fetch(:host) do
140
110
  env[TZISPA_HOST] || DEFAULT_HOST
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'yaml'
4
4
  require 'http_router'
5
- require 'tzispa/utils/string'
5
+ require 'tzispa_utils'
6
6
  require 'tzispa/controller/http_error'
7
7
 
8
8
  module Tzispa
data/lib/tzispa/server.rb CHANGED
@@ -54,8 +54,8 @@ module Tzispa
54
54
  {
55
55
  environment: env.environment,
56
56
  config: rackup_file,
57
- Host: env.server_host,
58
- Port: env.server_port,
57
+ # Host: env.server_host,
58
+ # Port: env.server_port,
59
59
  AccessLog: []
60
60
  }
61
61
  end
@@ -1,30 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'pathname'
4
- require 'tzispa/utils/hash'
4
+ require 'tzispa_utils'
5
+ require 'tzispa/config/rc'
5
6
 
6
7
  module Tzispa
7
8
 
8
9
  class Tzisparc
9
10
  using Tzispa::Utils::TzHash
10
11
 
11
- FILE_NAME = '.tzisparc'
12
-
13
- DEFAULT_ARCHITECTURE = 'domains'
14
-
15
- APP_ARCHITECTURE = 'app'
16
-
17
- ARCHITECTURE_KEY = 'architecture'
18
-
19
- PROJECT_NAME = 'project'
12
+ include Tzispa::Config::Rc
20
13
 
21
- DEFAULT_TEST_SUITE = 'minitest'
22
-
23
- TEST_KEY = 'test'
24
-
25
- DEFAULT_TEMPLATE = 'rig'
26
-
27
- TEMPLATE_KEY = 'template'
14
+ FILE_NAME = '.tzisparc'
28
15
 
29
16
  SEPARATOR = '='
30
17
 
@@ -39,8 +26,7 @@ module Tzispa
39
26
  def default_options
40
27
  @default_options ||= { ARCHITECTURE_KEY => DEFAULT_ARCHITECTURE,
41
28
  PROJECT_NAME => project_name,
42
- TEST_KEY => DEFAULT_TEST_SUITE,
43
- TEMPLATE_KEY => DEFAULT_TEMPLATE }
29
+ TEST_KEY => DEFAULT_TEST_SUITE }
44
30
  end
45
31
 
46
32
  def exists?
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tzispa
4
- VERSION = '0.8.0'
4
+ VERSION = '0.8.1'
5
5
  FRAMEWORK_NAME = 'Tzispa'
6
6
  GEM_NAME = 'tzispa'
7
7
  end
data/lib/tzispa.rb CHANGED
@@ -1,8 +1,16 @@
1
1
  module Tzispa
2
2
 
3
3
  require 'tzispa/version'
4
- require 'tzispa/environment'
5
- require 'tzispa/server'
6
- require 'tzispa/app'
4
+
5
+ autoload :Environment, 'tzispa/environment'
6
+ autoload :Server, 'tzispa/server'
7
+ autoload :Application, 'tzispa/app'
8
+ autoload :Cli, 'tzispa/cli'
9
+ autoload :Context, 'tzispa/context'
10
+ autoload :Domain, 'tzispa/domain'
11
+ autoload :Env, 'tzispa/env'
12
+ autoload :RouteSet, 'tzispa/route_set'
13
+ autoload :Server, 'tzispa/server'
14
+ autoload :Tzisparc, 'tzispa/tzisparc'
7
15
 
8
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tzispa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Antonio Piñero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-22 00:00:00.000000000 Z
11
+ date: 2017-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -183,8 +183,6 @@ files:
183
183
  - README.md
184
184
  - bin/tzispa
185
185
  - lib/tzispa.rb
186
- - lib/tzispa/api/handler.rb
187
- - lib/tzispa/api/handler_error.rb
188
186
  - lib/tzispa/app.rb
189
187
  - lib/tzispa/bin/tzispa
190
188
  - lib/tzispa/cli.rb
@@ -203,6 +201,8 @@ files:
203
201
  - lib/tzispa/config/app_config.rb
204
202
  - lib/tzispa/config/base.rb
205
203
  - lib/tzispa/config/db_config.rb
204
+ - lib/tzispa/config/environment.rb
205
+ - lib/tzispa/config/rc.rb
206
206
  - lib/tzispa/config/yaml.rb
207
207
  - lib/tzispa/context.rb
208
208
  - lib/tzispa/controller/base.rb
@@ -246,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
246
246
  version: '0'
247
247
  requirements: []
248
248
  rubyforge_project:
249
- rubygems_version: 2.6.11
249
+ rubygems_version: 2.6.13
250
250
  signing_key:
251
251
  specification_version: 4
252
252
  summary: A sparkling web framework
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
- require 'tzispa/helpers/provider'
5
- require 'tzispa/helpers/sign_requirer'
6
- require 'tzispa/helpers/hooks/before'
7
- require 'tzispa/helpers/hooks/after'
8
- require_relative 'handler_error'
9
-
10
- module Tzispa
11
- module Api
12
-
13
- class ApiException < StandardError; end
14
- class UnknownHandlerVerb < ApiException
15
- def initialize(s, name)
16
- super("Unknown verb: '#{s}' called in api handler '#{name}'")
17
- end
18
- end
19
- class InvalidSign < ApiException; end
20
-
21
- class Handler
22
- extend Forwardable
23
-
24
- include Tzispa::Api::HandlerError
25
- include Tzispa::Helpers::Provider
26
- include Tzispa::Helpers::SignRequirer
27
- include Tzispa::Helpers::Hooks::Before
28
- include Tzispa::Helpers::Hooks::After
29
-
30
- using Tzispa::Utils::TzString
31
-
32
- attr_reader :context, :type, :data, :error, :rescue_hook, :status
33
- def_delegators :@context, :request, :response, :app, :repository,
34
- :config, :logger, :error_log, :info_log
35
-
36
- def initialize(context)
37
- @context = context
38
- @error = nil
39
- @status = nil
40
- @rescue_hook = nil
41
- end
42
-
43
- def result(type:, data: nil)
44
- @type = type
45
- @data = data
46
- end
47
-
48
- def result_json(data)
49
- result type: :json, data: data
50
- end
51
-
52
- def result_download(data)
53
- result type: :download, data: data
54
- end
55
-
56
- def result_redirect(data)
57
- result type: :redirect, data: data
58
- end
59
-
60
- def error_status(error_code, http_status = nil)
61
- @error = error_code
62
- @status = http_status
63
- result_json error_message: error_message
64
- end
65
- # alias result_error error_status
66
-
67
- def on_error(http_error, error_code = nil, logger = nil)
68
- @rescue_hook = { logger: logger, error_code: error_code, http_error: http_error }
69
- end
70
-
71
- def run!(verb, predicate = nil)
72
- raise UnknownHandlerVerb.new(verb, self.class.name) unless provides? verb
73
- raise InvalidSign if sign_required? && !sign_valid?
74
- do_before
75
- begin
76
- send verb, *(predicate&.split(','))
77
- rescue => err
78
- if rescue_hook
79
- send(rescue_hook[:logger], err) if rescue_hook.include? :logger
80
- send(rescue_hook[:http_error], rescue_hook[:error_code])
81
- else
82
- raise
83
- end
84
- end
85
- do_after
86
- end
87
-
88
- def redirect_url(url)
89
- if url && !url.strip.empty?
90
- url.start_with?('#') ? "#{request.referer}#{url}" : url
91
- else
92
- request.referer
93
- end
94
- end
95
-
96
- protected
97
-
98
- def static_path_sign?
99
- context.path_sign? context.router_params[:sign],
100
- context.router_params[:handler],
101
- context.router_params[:verb],
102
- context.router_params[:predicate]
103
- end
104
- end
105
-
106
- end
107
- end
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'i18n'
4
- require 'tzispa/utils/string'
5
-
6
- module Tzispa
7
- module Api
8
- module HandlerError
9
-
10
- using Tzispa::Utils::TzString
11
-
12
- HANDLER_OK = :ok
13
-
14
- def error?
15
- @error && @error != HANDLER_OK
16
- end
17
-
18
- def error_message
19
- I18n.t(error_id, default: error.to_s) if error
20
- end
21
-
22
- def error_id
23
- "#{self.class.name.dottize}.#{error}" if error
24
- end
25
-
26
- def http_bad_request(code = nil)
27
- error_status code || :bad_request, 400
28
- end
29
-
30
- def http_unauthorized(code = nil)
31
- error_status code || :unauthorized, 401
32
- end
33
-
34
- def http_forbidden(code = nil)
35
- error_status code || :forbidden, 403
36
- end
37
-
38
- def http_not_found(code = nil)
39
- error_status code || :not_found, 404
40
- end
41
-
42
- def http_not_aceptable(code = nil)
43
- error_status code || :not_acceptable, 406
44
- end
45
-
46
- def http_conflict(code = nil)
47
- error_status code || :conflict, 409
48
- end
49
-
50
- def http_gone(code = nil)
51
- error_status code || :gone, 410
52
- end
53
-
54
- def http_token_required(code = nil)
55
- error_status code || :token_required, 499
56
- end
57
-
58
- def http_server_error(code = nil)
59
- error_status code || :internal_server_error, 500
60
- end
61
-
62
- def http_not_implemented(code = nil)
63
- error_status code || :not_implemented, 501
64
- end
65
-
66
- def http_bad_gateway(code = nil)
67
- error_status code || :bad_gateway, 502
68
- end
69
-
70
- def http_service_unavailable(code = nil)
71
- error_status code || :service_unavailable, 503
72
- end
73
-
74
- end
75
- end
76
- end