shatter-rb 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.husky/commit-msg +5 -0
  3. data/.rubocop.yml +4 -0
  4. data/.ruby-version +1 -1
  5. data/.tool-versions +1 -1
  6. data/Gemfile +0 -2
  7. data/Gemfile.lock +51 -8
  8. data/Guardfile +72 -0
  9. data/README.md +72 -14
  10. data/commitlint.config.js +1 -0
  11. data/example_app/Gemfile +3 -2
  12. data/example_app/Gemfile.lock +9 -16
  13. data/example_app/app/application.rb +4 -0
  14. data/example_app/app/functions/hello_world_function.rb +8 -16
  15. data/example_app/app/service_definition.rb +2 -6
  16. data/example_app/application.rb +4 -2
  17. data/example_app/bin/service +2 -2
  18. data/example_app/config/environment.rb +13 -7
  19. data/example_app/config.ru +8 -0
  20. data/exe/console +1 -1
  21. data/exe/shatter +55 -25
  22. data/lib/shatter/config.rb +14 -4
  23. data/lib/shatter/service/base.rb +28 -35
  24. data/lib/shatter/service/discovery.rb +32 -19
  25. data/lib/shatter/service/function.rb +45 -31
  26. data/lib/shatter/service/response_pool.rb +12 -7
  27. data/lib/shatter/service/service_definition.rb +10 -4
  28. data/lib/shatter/util.rb +21 -5
  29. data/lib/shatter/version.rb +1 -1
  30. data/lib/shatter/web/application.rb +34 -32
  31. data/lib/shatter/web/server.rb +25 -18
  32. data/lib/shatter.rb +59 -11
  33. data/package.json +7 -0
  34. data/templates/Gemfile.template +1 -3
  35. data/templates/application.erb +2 -6
  36. data/templates/config.ru +8 -0
  37. data/templates/environment.rb.erb +9 -8
  38. data/templates/function_definition.ts.erb +5 -0
  39. data/templates/hello_world_function.rb.erb +6 -14
  40. data/templates/service_client.ts.erb +25 -0
  41. data/templates/service_definition.rb.erb +3 -7
  42. data/yarn.lock +1338 -0
  43. metadata +115 -25
  44. data/example_app/bin/console +0 -11
  45. data/lib/shatter/service/function_params.rb +0 -21
  46. data/shatter.gemspec +0 -51
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 564bbc6ee68afe29dbe4466769257f1d433c4a2b59903bb3f3b776f3269dfe51
4
- data.tar.gz: b3b7a0dcb2a1da8c81c3bdeb5ed89187825bf20c8357efa7332c417a2c3c2924
3
+ metadata.gz: 779c71931585d9bb8e13eb0b22f672090d5323dcbdbe0d81369aad78571e8f77
4
+ data.tar.gz: 688822d6f2c2115247a285a3a04ad514392448562ebb6cd6fb7afdf5f412375a
5
5
  SHA512:
6
- metadata.gz: f8e8ff85420ef7537b745e10893162d8386e40bcd7cc00e471920fc3e3d50868ab4104af7c4d42c82464f146f794658673211082945ea96934d093bb4ec0509d
7
- data.tar.gz: 7b0d10b9f797a01dd4efb161413094134ed6d20bad41ada4628ea6da7cc31a255fcfa5844957e4e0aa175a9890fbd8fa944da597575676badebcbe15faeecbf7
6
+ metadata.gz: c770c0d2dd76d847ce9d1265a460b55aa8b5d7a772baaf61d7e85a85580a83155270ad05fd27508869f0d44024fd6df4cdddccdf6dc0194270d27e31ec14e446
7
+ data.tar.gz: f7a9c9269405e7636eca758850507d571923818cd4e20ceeb24a15b7c22d7b0cd8948486e400841021ead2d3a10f59428364f14d4a22e396449928ee7f0537f1
data/.husky/commit-msg ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ npx --no -- commitlint --edit
5
+ npx --no -- commitlint --edit
data/.rubocop.yml CHANGED
@@ -4,6 +4,10 @@ require:
4
4
  AllCops:
5
5
  NewCops: enable
6
6
  TargetRubyVersion: 3.1
7
+ Exclude:
8
+ - vendor/bundle/**/*
9
+ - exe/**/*
10
+ - Guardfile
7
11
 
8
12
  Style/StringLiterals:
9
13
  Enabled: true
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.2.0
1
+ 3.1.0
data/.tool-versions CHANGED
@@ -1 +1 @@
1
- ruby 3.2.0
1
+ ruby 3.1.0
data/Gemfile CHANGED
@@ -4,5 +4,3 @@ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in shatter.gemspec
6
6
  gemspec
7
-
8
- gem "zookeeper", "~>1.5.4", github: 'EricRoos/zookeeper'
data/Gemfile.lock CHANGED
@@ -1,39 +1,71 @@
1
- GIT
2
- remote: https://github.com/EricRoos/zookeeper.git
3
- revision: e024228fc1c3c22cd3b0854ff15ad0d53a98c4ae
4
- specs:
5
- zookeeper (1.5.4)
6
-
7
1
  PATH
8
2
  remote: .
9
3
  specs:
10
- shatter-rb (0.0.2)
4
+ shatter-rb (0.1.1)
11
5
  concurrent-ruby (~> 1.1)
12
6
  erb (~> 2.2.0)
13
7
  puma (~> 6.0)
14
8
  rack (~> 3.0)
9
+ rack-cors (~> 1.1)
15
10
  thor (~> 1.2.1)
11
+ zeitwerk
16
12
  zk (~> 1.10)
13
+ zookeeper (~> 1.5.4)
17
14
 
18
15
  GEM
19
16
  remote: https://rubygems.org/
20
17
  specs:
21
18
  ast (2.4.2)
22
19
  cgi (0.3.6)
20
+ coderay (1.1.3)
23
21
  concurrent-ruby (1.1.10)
24
22
  diff-lcs (1.5.0)
23
+ docile (1.4.0)
25
24
  erb (2.2.3)
26
25
  cgi
26
+ ffi (1.15.5)
27
+ formatador (1.1.0)
28
+ guard (2.18.0)
29
+ formatador (>= 0.2.4)
30
+ listen (>= 2.7, < 4.0)
31
+ lumberjack (>= 1.0.12, < 2.0)
32
+ nenv (~> 0.1)
33
+ notiffany (~> 0.0)
34
+ pry (>= 0.13.0)
35
+ shellany (~> 0.0)
36
+ thor (>= 0.18.1)
37
+ guard-compat (1.2.1)
38
+ guard-rspec (4.7.3)
39
+ guard (~> 2.1)
40
+ guard-compat (~> 1.1)
41
+ rspec (>= 2.99.0, < 4.0)
27
42
  json (2.6.3)
43
+ listen (3.7.1)
44
+ rb-fsevent (~> 0.10, >= 0.10.3)
45
+ rb-inotify (~> 0.9, >= 0.9.10)
46
+ lumberjack (1.2.8)
47
+ method_source (1.0.0)
48
+ nenv (0.3.0)
28
49
  nio4r (2.5.8)
50
+ notiffany (0.1.3)
51
+ nenv (~> 0.1)
52
+ shellany (~> 0.0)
29
53
  parallel (1.22.1)
30
54
  parser (3.1.3.0)
31
55
  ast (~> 2.4.1)
56
+ pry (0.14.1)
57
+ coderay (~> 1.1)
58
+ method_source (~> 1.0)
32
59
  puma (6.0.2)
33
60
  nio4r (~> 2.0)
34
61
  rack (3.0.3)
62
+ rack-cors (1.1.1)
63
+ rack (>= 2.0.0)
35
64
  rainbow (3.1.1)
36
65
  rake (13.0.6)
66
+ rb-fsevent (0.11.2)
67
+ rb-inotify (0.10.1)
68
+ ffi (~> 1.0)
37
69
  regexp_parser (2.6.1)
38
70
  rexml (3.2.5)
39
71
  rspec (3.12.0)
@@ -66,22 +98,33 @@ GEM
66
98
  rubocop-rspec (2.16.0)
67
99
  rubocop (~> 1.33)
68
100
  ruby-progressbar (1.11.0)
101
+ shellany (0.0.1)
102
+ simplecov (0.22.0)
103
+ docile (~> 1.1)
104
+ simplecov-html (~> 0.11)
105
+ simplecov_json_formatter (~> 0.1)
106
+ simplecov-html (0.12.3)
107
+ simplecov_json_formatter (0.1.4)
69
108
  thor (1.2.1)
70
109
  unicode-display_width (2.3.0)
110
+ zeitwerk (2.6.6)
71
111
  zk (1.10.0)
72
112
  zookeeper (~> 1.5.0)
113
+ zookeeper (1.5.5)
73
114
 
74
115
  PLATFORMS
75
116
  x86_64-linux
76
117
 
77
118
  DEPENDENCIES
119
+ guard
120
+ guard-rspec
78
121
  rake (~> 13.0)
79
122
  rspec (~> 3.0)
80
123
  rubocop (~> 1.21)
81
124
  rubocop-rake (~> 0.6.0)
82
125
  rubocop-rspec (~> 2.16.0)
83
126
  shatter-rb!
84
- zookeeper (~> 1.5.4)!
127
+ simplecov
85
128
 
86
129
  BUNDLED WITH
87
130
  2.4.1
data/Guardfile ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A sample Guardfile
4
+ # More info at https://github.com/guard/guard#readme
5
+
6
+ ## Uncomment and set this to only include directories you want to watch
7
+ # directories %w(app lib config test spec features) \
8
+ # .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
9
+
10
+ ## Note: if you are using the `directories` clause above and you are not
11
+ ## watching the project directory ('.'), then you will want to move
12
+ ## the Guardfile to a watched dir and symlink it back, e.g.
13
+ #
14
+ # $ mkdir config
15
+ # $ mv Guardfile config/
16
+ # $ ln -s config/Guardfile .
17
+ #
18
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
19
+
20
+ # NOTE: The cmd option is now required due to the increasing number of ways
21
+ # rspec may be run, below are examples of the most common uses.
22
+ # * bundler: 'bundle exec rspec'
23
+ # * bundler binstubs: 'bin/rspec'
24
+ # * spring: 'bin/rspec' (This will use spring if running and you have
25
+ # installed the spring binstubs per the docs)
26
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
27
+ # * 'just' rspec: 'rspec'
28
+
29
+ guard :rspec, cmd: "bundle exec rspec" do
30
+ require "guard/rspec/dsl"
31
+ dsl = Guard::RSpec::Dsl.new(self)
32
+
33
+ # Feel free to open issues for suggestions and improvements
34
+
35
+ # RSpec files
36
+ rspec = dsl.rspec
37
+ watch(rspec.spec_helper) { rspec.spec_dir }
38
+ watch(rspec.spec_support) { rspec.spec_dir }
39
+ watch(rspec.spec_files)
40
+
41
+ # Ruby files
42
+ ruby = dsl.ruby
43
+ dsl.watch_spec_files_for(ruby.lib_files)
44
+
45
+ # Rails files
46
+ rails = dsl.rails(view_extensions: %w[erb haml slim])
47
+ dsl.watch_spec_files_for(rails.app_files)
48
+ dsl.watch_spec_files_for(rails.views)
49
+
50
+ watch(rails.controllers) do |m|
51
+ [
52
+ rspec.spec.call("routing/#{m[1]}_routing"),
53
+ rspec.spec.call("controllers/#{m[1]}_controller"),
54
+ rspec.spec.call("acceptance/#{m[1]}")
55
+ ]
56
+ end
57
+
58
+ # Rails config changes
59
+ watch(rails.spec_helper) { rspec.spec_dir }
60
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
61
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
62
+
63
+ # Capybara features specs
64
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
65
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
66
+
67
+ # Turnip features and steps
68
+ watch(%r{^spec/acceptance/(.+)\.feature$})
69
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
70
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
71
+ end
72
+ end
data/README.md CHANGED
@@ -1,30 +1,84 @@
1
- # Shatter
2
1
 
3
- TODO: Delete this and the text below, and describe your gem
2
+ ![shatter-banner](https://user-images.githubusercontent.com/1334489/211411749-49d3377f-e413-4838-bae7-60705688b514.png#gh-light-mode-only)
3
+ ![shatter-banner-darkmode](https://user-images.githubusercontent.com/1334489/211412250-0660c441-31f1-4d74-87f8-5118ccebffa0.png#gh-dark-mode-only)
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/shatter`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ <p align='center'>
6
+ An async first RPC app server built in Ruby. Supported by DRb, Puma, and Zookeeper.
7
+ </p>
6
8
 
7
- ## Installation
8
9
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+ ## Whats needed
10
11
 
11
- Install the gem and add to the application's Gemfile by executing:
12
+ * Ruby3.1 and later
13
+ * A box to run it on
14
+ * Zookeeper instance
15
+ * Optional: Extra server to run the service layer across different boxes.
12
16
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
17
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
18
+ ## Developing
16
19
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
20
+ ### Installation
21
+ Start with creating your project directory.
22
+ ```
23
+ mkdir MyApp
24
+ ```
18
25
 
19
- ## Usage
26
+ Create a new Gemfile and `bundle install`.
27
+ ```ruby
28
+ source "https://rubygems.org"
20
29
 
21
- TODO: Write usage instructions here
30
+ gem 'shatter-rb'
31
+ ```
22
32
 
23
- ## Development
33
+ Create the project with:
34
+ ```
35
+ bundle exec shatter new <AppName>
36
+ ```
37
+ For now, app name should be in provided in UpperCamelCase.
24
38
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
39
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+ Init zookeeper with the required keys needed
41
+ ```ruby
42
+ bundle exec shatter init_service_discovery
43
+ ```
44
+
45
+
46
+ Finally, you start shatter by running both the web server and service applications.
47
+
48
+ ```bash
49
+ bin/service
50
+ bin/server
51
+ ```
52
+
53
+ Congrats! Shatter is up and running!
54
+
55
+ ### Testing things out
56
+
57
+ Included in the setup is one example `HelloWorldFunction` to show you how things work. You can invoke it with curl like:
58
+
59
+ ```
60
+ curl -i -X POST -d '{}' localhost:9292/hello_world
61
+ ```
62
+
63
+ Check the `location` header in the response and follow that url till your response is ready. The Javascript client does all this for you, however.
64
+
65
+
66
+ You can check out a demo js script here:
67
+ https://github.com/EricRoos/shatter/blob/main/javascript/dist/demo.js
68
+
69
+
70
+
71
+ ### Front end things
72
+
73
+ We suggest you use Vite to setup your front end. Shatter comes with the ability to export your functions to a typescript definition that will give you a configured out of the box client ready for use. No need for wiring yourself except for importing whats generated.
74
+
75
+ You can generate your typescript with:
76
+
77
+ ```
78
+ bundle exec shatter generate_typescript --path web/src/
79
+ ```
80
+
81
+ Assuming you have setup your javascript based front end at ./web
28
82
 
29
83
  ## Contributing
30
84
 
@@ -37,3 +91,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
37
91
  ## Code of Conduct
38
92
 
39
93
  Everyone interacting in the Shatter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/shatter/blob/master/CODE_OF_CONDUCT.md).
94
+
95
+ ## Attribution
96
+
97
+ Shatter logo provided by: <a href='https://pngtree.com/so/Beautiful'>Beautiful png from pngtree.com/</a>
@@ -0,0 +1 @@
1
+ module.exports = {extends: ['@commitlint/config-conventional']}
data/example_app/Gemfile CHANGED
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
- gem 'shatter', path: '..'
3
- gem "zookeeper", "~>1.5.4", github: 'EricRoos/zookeeper'
4
+ gem "shatter-rb", path: ".."
4
5
 
5
6
  gem "foreman", "~> 0.87.2"
@@ -1,21 +1,16 @@
1
- GIT
2
- remote: https://github.com/EricRoos/zookeeper.git
3
- revision: e024228fc1c3c22cd3b0854ff15ad0d53a98c4ae
4
- specs:
5
- zookeeper (1.5.4)
6
-
7
1
  PATH
8
2
  remote: ..
9
3
  specs:
10
- shatter (0.1.0)
4
+ shatter-rb (0.0.2)
11
5
  concurrent-ruby (~> 1.1)
12
6
  erb (~> 2.2.0)
13
- pg (~> 1.4)
14
7
  puma (~> 6.0)
15
8
  rack (~> 3.0)
16
- rackup (~> 0.2.3)
9
+ rack-cors (~> 1.1)
17
10
  thor (~> 1.2.1)
11
+ zeitwerk
18
12
  zk (~> 1.10)
13
+ zookeeper (~> 1.5.4)
19
14
 
20
15
  GEM
21
16
  remote: https://rubygems.org/
@@ -26,25 +21,23 @@ GEM
26
21
  cgi
27
22
  foreman (0.87.2)
28
23
  nio4r (2.5.8)
29
- pg (1.4.5)
30
24
  puma (6.0.2)
31
25
  nio4r (~> 2.0)
32
26
  rack (3.0.3)
33
- rackup (0.2.3)
34
- rack (>= 3.0.0.beta1)
35
- webrick
27
+ rack-cors (1.1.1)
28
+ rack (>= 2.0.0)
36
29
  thor (1.2.1)
37
- webrick (1.7.0)
30
+ zeitwerk (2.6.6)
38
31
  zk (1.10.0)
39
32
  zookeeper (~> 1.5.0)
33
+ zookeeper (1.5.5)
40
34
 
41
35
  PLATFORMS
42
36
  x86_64-linux
43
37
 
44
38
  DEPENDENCIES
45
39
  foreman (~> 0.87.2)
46
- shatter!
47
- zookeeper (~> 1.5.4)!
40
+ shatter-rb!
48
41
 
49
42
  BUNDLED WITH
50
43
  2.4.1
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Application < Shatter::Web::Application
4
+ end
@@ -1,19 +1,11 @@
1
- require "pg"
2
- require "shatter/service/function"
3
- require "shatter/service/function_params"
1
+ # frozen_string_literal: true
4
2
 
5
- module MyApp
6
- module Functions
7
- class HelloWorldFunction < Shatter::Service::Function
3
+ class HelloWorldFunction < Shatter::Service::Function
4
+ define_param :name, nullable: false, type: "string"
5
+ define_param :number, nullable: false, type: "integer"
8
6
 
9
- define_param :name, nullable: false, type: 'string'
10
- define_param :number, nullable: false, type: 'integer'
11
-
12
-
13
- def invoke
14
- params.to_h => name:, number:
15
- { result: "Hello #{name}, your number is #{number || 'unknown'}.", error: nil, }
16
- end
17
- end
7
+ def invoke
8
+ params.to_h => name:, uuid:
9
+ { result: "Hello #{name}", error: nil, uuid: }
18
10
  end
19
- end
11
+ end
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './functions/hello_world_function'
4
-
5
- module MyApp
6
- class ServiceDefinition < Shatter::Service::ServiceDefinition
7
- register_function :hello_world, MyApp::Functions::HelloWorldFunction
8
- end
3
+ class ServiceDefinition < Shatter::Service::ServiceDefinition
4
+ register_function :hello_world, HelloWorldFunction
9
5
  end
@@ -1,6 +1,8 @@
1
- module MyApp
1
+ # frozen_string_literal: true
2
+
3
+ module ExampleApp
2
4
  class Application < Shatter::Web::Application
3
5
  end
4
6
  end
5
7
 
6
- require_relative './app/service_definition'
8
+ require_relative "./app/service_definition"
@@ -10,7 +10,7 @@ require_relative "../config/environment"
10
10
  begin
11
11
  Shatter::Service::Base.init
12
12
  rescue Interrupt => e
13
- Shatter::Service::Base.close
14
- rescue SignalException => e
13
+ puts e.message
14
+ puts e.backtrace
15
15
  Shatter::Service::Base.close
16
16
  end
@@ -1,9 +1,15 @@
1
- Shatter::Config.zookeeper_host = "localhost:2181"
2
- Shatter::Config.initial_delay = 100
3
- Shatter::Config.missing_result_delay = 100
4
- Shatter::Config.service_port = ENV.fetch('SHATTER_SERVICE_PORT') { 8787 }
1
+ # frozen_string_literal: true
5
2
 
6
- require_relative '../application'
3
+ Shatter.config do |config|
4
+ config.root = File.expand_path("..", __dir__)
5
+ config.zookeeper_host = "localhost:2181"
6
+ config.initial_delay = 100
7
+ config.missing_result_delay = 100
8
+ config.service_port = ENV.fetch("SHATTER_SERVICE_PORT", 8787)
9
+ config.autoload_paths = %w[app app/functions]
10
+ end
7
11
 
8
- Shatter::Service::Base.service_definition = MyApp::ServiceDefinition
9
- Shatter::Web::Server.application = MyApp::Application
12
+ Shatter.load
13
+
14
+ Shatter::Service::Base.service_definition = ServiceDefinition
15
+ Shatter::Web::Server.application = Application
@@ -2,8 +2,16 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "shatter"
5
+ require "rack/cors"
5
6
 
6
7
  require_relative "./config/environment"
7
8
 
8
9
  use Rack::CommonLogger
10
+
11
+ use Rack::Cors do
12
+ allow do
13
+ origins "*"
14
+ resource "*", headers: :any, methods: %i[get post patch put], expose: %w[delay location]
15
+ end
16
+ end
9
17
  run Shatter::Web::Server
data/exe/console CHANGED
@@ -8,5 +8,5 @@ $stdout.sync = true
8
8
  require_relative "../config/environment"
9
9
  require_relative "../application"
10
10
 
11
- require 'irb'
11
+ require "irb"
12
12
  IRB.start(__FILE__)
data/exe/shatter CHANGED
@@ -1,7 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
- require 'shatter'
3
- require 'thor'
4
- require 'erb'
2
+ # frozen_string_literal: true
3
+
4
+ require "shatter"
5
+ require "thor"
6
+ require "erb"
7
+ require "fileutils"
8
+ require 'zk'
9
+ require 'puma/cli'
10
+
5
11
  $stdout.sync = true
6
12
 
7
13
  def safe_makedir(path)
@@ -11,54 +17,78 @@ end
11
17
  class ShatterCLI < Thor
12
18
  desc "init_service_discovery", "Sets up required keys for service discovery to work properly"
13
19
 
20
+ desc "init_service_discovery", "creates necessary keys in zookeeper"
14
21
  def init_service_discovery
22
+ Shatter.load_environment
15
23
  zk = ZK.new(Shatter::Config.zookeeper_host)
16
- zk.create('/shater_service_instances')
17
- zk.create('/shatter::response_data_locations')
24
+ [
25
+ Shatter::Util.instances_key,
26
+ Shatter::Util.zookeeper_response_key_root,
27
+ ].each do |key|
28
+ next if zk.exists?(key)
29
+ zk.create(key)
30
+ end
18
31
  zk.close
19
32
  end
20
33
 
34
+ desc "service", "starts the service layer"
35
+ def service
36
+ Shatter.load_environment
37
+ begin
38
+ Shatter::Service::Base.init
39
+ rescue Interrupt => e
40
+ puts e.message
41
+ puts e.backtrace
42
+ Shatter::Service::Base.close
43
+ end
44
+ end
45
+
46
+ desc "server", "starts the web server layer"
47
+ def server
48
+ cli = Puma::CLI.new ["#{Shatter.root}/config.ru"]
49
+ puts cli.run
50
+ end
51
+
52
+ desc "generate_typescript", "generates typescript defs of your service definition"
53
+ option :path
54
+ def generate_typescript
55
+ Shatter.load_environment
56
+ File.open("#{Shatter.root}/#{options[:path]}/Client.ts", "w") { |f| f.write(ServiceDefinition.to_typescript) }
57
+ end
58
+
21
59
  desc "new APP_NAME", "scaffold a new shatter app"
22
60
  def new(app_name)
23
61
  puts "Generating new Shatter APP - #{app_name}"
24
62
 
25
63
  puts "Updating Gemfile"
26
- FileUtils.cp("#{__dir__}/../templates/Gemfile.template","./Gemfile")
64
+ ::FileUtils.cp("#{__dir__}/../templates/Gemfile.template", "./Gemfile")
27
65
  system("bundle install")
28
66
 
29
67
  puts "Generating scripts"
30
- FileUtils.mkdir("./bin", noop: Dir.exist?('./bin'))
31
- FileUtils.cp("#{__dir__}/../bin/service", "./bin/service", verbose: true)
32
- FileUtils.cp("#{__dir__}/../bin/server", "./bin/server", verbose: true)
68
+ #FileUtils.mkdir("./bin", noop: Dir.exist?("./bin"))
69
+ #FileUtils.cp("#{__dir__}/../bin/service", "./bin/service", verbose: true)
70
+ #FileUtils.cp("#{__dir__}/../bin/server", "./bin/server", verbose: true)
33
71
 
34
72
  puts "Generating boilerplate"
35
73
 
36
74
  application_code = ERB.new(File.read("#{__dir__}/../templates/application.erb")).result(binding)
37
- File.open("./application.rb", "w") do |f|
38
- f.write application_code
39
- end
75
+ safe_makedir("./app")
76
+ File.write("./app/application.rb", application_code)
40
77
 
41
78
  FileUtils.cp("#{__dir__}/../templates/config.ru", "./config.ru", verbose: true)
42
79
  FileUtils.cp("#{__dir__}/../templates/docker-compose.yml.example", "./docker-compose.yml", verbose: true)
43
80
 
44
- safe_makedir('./app')
45
81
 
46
82
  service_definition = ERB.new(File.read("#{__dir__}/../templates/service_definition.rb.erb")).result(binding)
47
- File.open("./app/service_definition.rb", "w") do |f|
48
- f.write service_definition
49
- end
83
+ File.write("./app/service_definition.rb", service_definition)
50
84
 
51
- safe_makedir('./app/functions')
85
+ safe_makedir("./app/functions")
52
86
  sample_function = ERB.new(File.read("#{__dir__}/../templates/hello_world_function.rb.erb")).result(binding)
53
- File.open("./app/functions/hello_world_function.rb", "w") do |f|
54
- f.write sample_function
55
- end
56
- safe_makedir('./config')
87
+ File.write("./app/functions/hello_world_function.rb", sample_function)
88
+ safe_makedir("./config")
57
89
 
58
90
  environment_config = ERB.new(File.read("#{__dir__}/../templates/environment.rb.erb")).result(binding)
59
- File.open("./config/environment.rb", "w") do |f|
60
- f.write environment_config
61
- end
91
+ File.write("./config/environment.rb", environment_config)
62
92
  end
63
93
  end
64
- ShatterCLI.start(ARGV)
94
+ ShatterCLI.start(ARGV)
@@ -3,10 +3,20 @@
3
3
  module Shatter
4
4
  class Config
5
5
  class << self
6
- attr_accessor :zookeeper_host
7
- attr_accessor :initial_delay
8
- attr_accessor :missing_result_delay
9
- attr_accessor :service_port
6
+ attr_accessor :zookeeper_host, :service_port, :root, :reload_classes
7
+ attr_writer :autoload_paths, :initial_delay, :missing_result_delay
8
+
9
+ def autoload_paths
10
+ @autoload_paths ||= []
11
+ end
12
+
13
+ def initial_delay
14
+ @initial_delay || 100
15
+ end
16
+
17
+ def missing_result_delay
18
+ @missing_result_delay || 100
19
+ end
10
20
  end
11
21
  end
12
22
  end