neo_gruby 0.2.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 937ca306efab93ba61192077aacb966bf6383f794f7c1c949de411737720d495
4
+ data.tar.gz: 6e60760487b9c7ef9bab1536bf3dd0224be5f3214077d49af533139e41fb7d90
5
+ SHA512:
6
+ metadata.gz: d0b448be92b9cba100bfb5040d6b0a134a381e9512c6ef6d0ec4e36cfddacf13446668a0de8259b3ea0f07c4b7b2d615b42004fc8702ca21e10b2045bad02837
7
+ data.tar.gz: f4bbd4ad9610a2debf0c2b93931656288f08d8bb3d104e0a414ec96e1222f20fb2aacc8bfaccc82d40e48c5f2008af943898ddb6ae7c0cffc3383bc6db131e9d
data/exe/neogruby ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require "bundler/setup"
3
+ require "dry/cli"
4
+ require_relative '../lib/neo_gruby'
5
+ Dry::CLI.new(NeoGruby::CLI::Commands).call
data/lib/neo_gruby.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "neo_gruby/version"
2
+ require "neo_gruby/ext/class"
3
+ require "dotenv"
4
+
5
+ module NeoGruby
6
+ class Error < StandardError; end
7
+
8
+ autoload :Registry, 'neo_gruby/registry'
9
+ autoload :Application, 'neo_gruby/application'
10
+
11
+ module CLI
12
+ autoload :Commands, 'neo_gruby/cli/commands'
13
+ end
14
+
15
+ class << self
16
+
17
+ attr_accessor :root, :app, :env, :db
18
+
19
+ def boot
20
+ if @root.nil?
21
+ @root = File.expand_path('../../', File.expand_path(caller_locations.first.path))
22
+
23
+ def @root.join(*args)
24
+ File.join self, args.join(File::Separator)
25
+ end
26
+ end
27
+
28
+ @env = (ENV['NEOGRUBY_ENV'] || ENV['APP_ENV'] || ENV.fetch('RACK_ENV') { 'development' }).to_s
29
+ def @env.development?
30
+ self == 'development'
31
+ end
32
+
33
+ def @env.production?
34
+ self == 'production'
35
+ end
36
+
37
+ def @env.test?
38
+ self == 'test'
39
+ end
40
+
41
+ Dotenv.load(root.join('.env'), root.join(".env.#{NeoGruby.env}"), root.join(".env.#{NeoGruby.env}.local"))
42
+
43
+ @configs = []
44
+ NeoGruby::Registry.trigger(:before_boot)
45
+ @app = Application.new
46
+ @configs.each {|c| app.configure {|app| c.call(app) } }
47
+ app.loadpaths
48
+ NeoGruby::Registry.trigger(:after_boot, @app)
49
+ end
50
+
51
+ def configure(&block)
52
+ @configs << block if block_given?
53
+ end
54
+
55
+ def run!
56
+ app.run
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,114 @@
1
+
2
+ module NeoGruby
3
+ class Application
4
+
5
+ attr_accessor :logger,
6
+ :proto_path,
7
+ :protos,
8
+ :services,
9
+ :server,
10
+ :load_paths
11
+
12
+ def initialize
13
+ @protos = []
14
+ @services = []
15
+ @load_paths = [
16
+ NeoGruby.root.join('app', 'protobuf'),
17
+ NeoGruby.root.join('app', 'models'),
18
+ NeoGruby.root.join('app', 'services'),
19
+ ]
20
+
21
+ @proto_path = NeoGruby.root.join('protos')
22
+
23
+ prepare_application
24
+ running_initializers
25
+ end
26
+
27
+ def loadpaths
28
+ NeoGruby::Registry.trigger(:before_loadpaths, @load_paths, self)
29
+ @load_paths.each {|f| $: << f }
30
+ end
31
+
32
+ def run
33
+ create_logger if logger.nil?
34
+ prepare_grpc
35
+ load_services
36
+ start_services
37
+ end
38
+
39
+ def configure(&block)
40
+ yield self if block_given?
41
+ end
42
+
43
+ protected
44
+
45
+ def prepare_application
46
+ require NeoGruby.root.join('config', 'application')
47
+
48
+ environment_file = NeoGruby.root.join('config', 'environments', NeoGruby.env)
49
+ require environment_file if File.exist? environment_file
50
+ end
51
+
52
+ def running_initializers
53
+ NeoGruby::Registry.trigger(:before_initializers, @load_paths, self)
54
+ Dir[NeoGruby.root.join('config', 'initializers', '*.rb')].each do |f|
55
+ require f
56
+ end
57
+ NeoGruby::Registry.trigger(:after_initializers, @load_paths, self)
58
+
59
+ end
60
+
61
+ def prepare_grpc
62
+ require 'grpc'
63
+
64
+ load_grpc_resources
65
+
66
+ @port = "0.0.0.0:#{ENV.fetch('PORT') { 50051 }}"
67
+ @server = GRPC::RpcServer.new
68
+ @server.add_http2_port(@port, :this_port_is_insecure)
69
+ end
70
+
71
+ def load_services
72
+ Dir[NeoGruby.root.join('app', 'services', '*.rb')].each do |f|
73
+ require f
74
+ end
75
+ end
76
+
77
+ def start_services
78
+ NeoGruby::Registry.trigger(:before_start, self)
79
+ puts '[SERVER] Adding services'
80
+ ObjectSpace.each_object(Class).select { |c| c.included_modules.include? GRPC::GenericService }.each do |s|
81
+ unless s.to_s.end_with? "::Service"
82
+ puts "[SERVER] - #{s}"
83
+ server.handle(s.new)
84
+ end
85
+ end
86
+
87
+ puts("[SERVER] running insecurely on #{@port}")
88
+ # Runs the server with SIGHUP, SIGINT and SIGQUIT signal handlers to
89
+ # gracefully shutdown.
90
+ # User could also choose to run server via call to run_till_terminated
91
+ server.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
92
+
93
+ NeoGruby::Registry.trigger(:after_start, self)
94
+ end
95
+
96
+ def load_grpc_resources
97
+ Dir[NeoGruby.root.join('app', 'protobuf', '*_pb.rb')].each do |f|
98
+ require f
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def create_logger
105
+ NeoGruby::Registry.trigger(:before_create_logger, self)
106
+ return unless @logger.nil?
107
+ require 'logger'
108
+ FileUtils.mkdir_p NeoGruby.root.join('log')
109
+ output = NeoGruby.env.production? ? NeoGruby.root.join('log', "#{NeoGruby.env}.log") : '/dev/stdout'
110
+ @logger = Logger.new output
111
+ NeoGruby::Registry.trigger(:after_create_logger, self)
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,14 @@
1
+ require "dry/cli"
2
+
3
+ module NeoGruby
4
+ module CLI
5
+ module Commands
6
+ extend Dry::CLI::Registry
7
+ end
8
+ end
9
+ end
10
+
11
+ require_relative 'commands/db'
12
+ require_relative 'commands/protoc'
13
+ require_relative 'commands/server'
14
+ require_relative 'commands/console'
@@ -0,0 +1,24 @@
1
+ module NeoGruby
2
+ module CLI
3
+ module Commands
4
+ class Console < Dry::CLI::Command
5
+ def call(*)
6
+ require './config/boot'
7
+ require 'pry'
8
+ Pry.config.prompt_name = 'NeoGruby'
9
+ Pry.start
10
+ puts 'Goodbye 👋'
11
+
12
+ rescue Interrupt => e
13
+ puts 'Goodbye 👋'
14
+ rescue SignalException => e
15
+ rescue Exception => e
16
+ puts e.message
17
+ puts e.backtrace
18
+ end
19
+ end
20
+
21
+ register 'console', Console, aliases: ['c']
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ require "dry/cli"
2
+
3
+ module NeoGruby
4
+ module CLI
5
+ module Commands
6
+ module Db
7
+ class Migrate < Dry::CLI::Command
8
+ desc "Run database migration"
9
+
10
+ argument :version, type: :string, desc: "Informe the migration name to destination"
11
+
12
+ def call(version: nil, **)
13
+ require './config/boot'
14
+ require 'sequel'
15
+ require 'yaml'
16
+
17
+ Sequel.extension :migration
18
+
19
+ config = YAML.load_file(NeoGruby.root.join('config', 'database.yml'))
20
+ config[NeoGruby.env].keys.each do |name|
21
+ dir = NeoGruby.root.join('db/migrations', name)
22
+ puts dir if File.directory? dir
23
+ Sequel::Migrator.run(NeoGruby::Db.conn[name.to_sym], dir, target: version) if File.directory? dir
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ register "db" do |prefix|
30
+ prefix.register "migrate", Db::Migrate
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,56 @@
1
+ require "dry/cli"
2
+
3
+ module NeoGruby
4
+ module CLI
5
+ module Commands
6
+ class Protoc < Dry::CLI::Command
7
+ desc "Compile proto3 files into ruby classes"
8
+
9
+ def call(*)
10
+ require './config/boot'
11
+
12
+ prepare_folders
13
+
14
+ puts "> Loading proto files from '#{NeoGruby.app.proto_path.join ', '}'"
15
+ compiling_messages
16
+ compiling_services
17
+ end
18
+
19
+ def prepare_folders
20
+ @output_path = NeoGruby.root.join('app', 'protobuf')
21
+ FileUtils.rm_rf @output_path
22
+ FileUtils.mkdir_p @output_path
23
+ end
24
+
25
+ def compiling_messages
26
+ puts "> Compiling proto into MESSAGES classes"
27
+
28
+ NeoGruby.app.protos.flatten.each do |proto|
29
+ puts "#{proto}.proto"
30
+
31
+ system 'grpc_tools_ruby_protoc',
32
+ NeoGruby.app.proto_path.map {|p| "-I#{p}" }.join(' '),
33
+ "--ruby_out=#{@output_path}",
34
+ "--grpc_out=#{@output_path}",
35
+ "#{proto}.proto"
36
+ end
37
+ end
38
+
39
+ def compiling_services
40
+ puts "> Compiling proto into SERVICES classes"
41
+
42
+ NeoGruby.app.services.flatten.each do |proto|
43
+ puts "#{proto}.proto"
44
+
45
+ system 'grpc_tools_ruby_protoc',
46
+ NeoGruby.app.proto_path.map {|p| "-I#{p}" }.join(' '),
47
+ "--ruby_out=#{@output_path}",
48
+ "#{proto}.proto"
49
+ end
50
+ end
51
+ end
52
+
53
+ register "protoc", Protoc, aliases: ['compile']
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,22 @@
1
+ require "dry/cli"
2
+
3
+ module NeoGruby
4
+ module CLI
5
+ module Commands
6
+ class Server < Dry::CLI::Command
7
+ desc "Run GRPC server"
8
+
9
+ def call(*)
10
+ system '/usr/bin/env ruby config.ru'
11
+
12
+ rescue Interrupt => e
13
+ rescue SignalException => e
14
+ rescue Exception => e
15
+ puts e.backtrace
16
+ end
17
+ end
18
+
19
+ register "server", Server, aliases: ['s']
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ class Class
2
+ class << self
3
+ def descendants
4
+ descendants = []
5
+ ObjectSpace.each_object(singleton_class) do |k|
6
+ next if k.singleton_class?
7
+ descendants.unshift k unless k == self
8
+ end
9
+ descendants
10
+ end
11
+
12
+ def subclasses
13
+ subclasses, chain = [], descendants
14
+ chain.each do |k|
15
+ subclasses << k unless chain.any? { |c| c > k }
16
+ end
17
+ subclasses
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module NeoGruby
2
+ module Registry
3
+ class << self
4
+ attr_accessor :bucket
5
+
6
+ def add(name, &block)
7
+ bucket[name.to_sym] ||= []
8
+ bucket[name.to_sym] << block if block_given?
9
+ end
10
+
11
+ def trigger(name, *params)
12
+ if bucket.has_key?(name.to_sym)
13
+ bucket[name.to_sym].each {|b| b.call(*params)}
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ NeoGruby::Registry.bucket = {}
@@ -0,0 +1,3 @@
1
+ module NeoGruby
2
+ VERSION = "0.2.0-rc1"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neo_gruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0.pre.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Welington Sampaio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grpc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.26.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.26.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: dotenv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-cli
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: logger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.12.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.12.2
83
+ description:
84
+ email:
85
+ - welington@gruponeolife.com.br
86
+ executables:
87
+ - neogruby
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - exe/neogruby
92
+ - lib/neo_gruby.rb
93
+ - lib/neo_gruby/application.rb
94
+ - lib/neo_gruby/cli/commands.rb
95
+ - lib/neo_gruby/cli/commands/console.rb
96
+ - lib/neo_gruby/cli/commands/db.rb
97
+ - lib/neo_gruby/cli/commands/protoc.rb
98
+ - lib/neo_gruby/cli/commands/server.rb
99
+ - lib/neo_gruby/ext/class.rb
100
+ - lib/neo_gruby/registry.rb
101
+ - lib/neo_gruby/version.rb
102
+ homepage: https://github.com/neolifehq/neogruby
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ homepage_uri: https://github.com/neolifehq/neogruby
107
+ source_code_uri: https://github.com/neolifehq/neogruby.git
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 2.3.0
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">"
120
+ - !ruby/object:Gem::Version
121
+ version: 1.3.1
122
+ requirements: []
123
+ rubygems_version: 3.0.3
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Little ruby framework to works with GRPC
127
+ test_files: []