micro_api 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: 760d9919d69b54e4fedf75c0d33c3e5fd92b40b90d276f0c5b03957e59c1cc03
4
- data.tar.gz: f4d1dbd92376d089ba6e1dbc2ec68b4f4a7f19c9dd6da4c98c991533ec8dfc49
3
+ metadata.gz: 931fe90b19dce2b382d2bf2a9aaa8c8387d8e473cd12ce31a9f9c7faa74cd10b
4
+ data.tar.gz: '091df9223364158ce18f3adaf343e8ee42c62df40b0e2745af75e8d1e132d7be'
5
5
  SHA512:
6
- metadata.gz: 6f18a322f332c97f230b3cd92dcd5a674b74556c3c3ddbccd45fc2afcd8546661924e61d8e7d70dcc09648eecf07baac78da4dc0517852b8081fbe778bb24831
7
- data.tar.gz: 6fbe29b7dd071e6d80d7f64c28e32e9bf4f04c615a74fe9b6c954ac2b6df93aed373efec7ed66ec60ceffc0c2d1ed3a678ae37bc204be402d4e2cb3f8e21fda6
6
+ metadata.gz: c1fb1c1fa9ec7b39e6f37ce75493e0d0ebc5693d107d93cedf259771d444b0b11aca0e850eafa00ad5ae2caa72b9245d92a7314c6f95df3366009250545243b5
7
+ data.tar.gz: 452621f6f949ccaa271e62952ea28708841d1e589fff4268fbd223c5fae47486eee3bf741b42db0995a67cae650522e0a04b07dc541cd57611183ea1325fa438
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # MicroApi
2
- Short description and motivation.
2
+ This is a Rails plugin that would like to help the startup of rails applications oriented to microservices or, in general, application deployed on the cloud.
3
3
 
4
4
  ## Usage
5
5
  How to use my plugin.
@@ -13,7 +13,8 @@ gem "micro_api"
13
13
 
14
14
  And then execute:
15
15
  ```bash
16
- $ bundle
16
+ bundle
17
+ bundle exec rails generate micro_api:install
17
18
  ```
18
19
 
19
20
  Or install it yourself as:
@@ -12,5 +12,10 @@ module MicroApi
12
12
  itag: ENV.fetch("IMAGE_TAG", nil), # image tag
13
13
  }
14
14
  end
15
+
16
+ def no_route_matches
17
+ exception = ActionController::RoutingError.new("No route matches [#{request.method}] #{request.path}")
18
+ render json: { error: exception.message }, status: :not_found
19
+ end
15
20
  end
16
21
  end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Main installation generator.
3
+
4
+ Example:
5
+ bin/rails generate micro_api:install
6
+
7
+ This will create:
8
+ - config/initializers/micro_api.rb
9
+ - config/environments/staging.rb
@@ -0,0 +1,68 @@
1
+ module MicroApi
2
+ # main generator installer
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ argument :component, type: :string, default: 'all',
7
+ banner: "all|application|initializer|route"
8
+
9
+ def main
10
+ method_name = "generate_micro_api_#{component}"
11
+ return unless self.class.private_method_defined?(method_name)
12
+
13
+ send(method_name)
14
+ end
15
+
16
+ private
17
+
18
+ def local_methods
19
+ private_methods.select { |e| e.to_s.include? "generate_micro_api_" }
20
+ end
21
+
22
+ desc "This generator creates all installs"
23
+ def generate_micro_api_all
24
+ local_methods.each do |method_name|
25
+ next if method_name == __method__
26
+
27
+ send(method_name)
28
+ end
29
+ end
30
+
31
+ desc "This generator add row in application.rb file"
32
+ def generate_micro_api_application
33
+ application_list = ["\n"]
34
+ application_list << "config.active_record.default_timezone = :utc" if defined?(ActiveRecord)
35
+ application_list << "config.time_zone = 'CET'"
36
+
37
+ inject_into_file 'config/application.rb', before: /\n end$/ do
38
+ application_list.join("\n ").gsub(" \n", "\n")
39
+ end
40
+ end
41
+
42
+ desc "This generator creates an initializer file at config/initializers"
43
+ def generate_micro_api_initializer
44
+ template "micro_api.rb", "#{Rails.root}/config/initializers/micro_api.rb"
45
+ template "staging.rb", "#{Rails.root}/config/environments/staging.rb"
46
+ end
47
+
48
+ desc "This generator add 'api namespace' rows in routes.rb file"
49
+ def generate_micro_api_route_api_namespace
50
+ inject_into_file 'config/routes.rb', before: " # Defines the root path route" do
51
+ " namespace :api, defaults: { format: :json } do\n end\n\n"
52
+ end
53
+ end
54
+
55
+ desc "This generator add final rows in routes.rb file"
56
+ def generate_micro_api_route_last
57
+ routes_list = [
58
+ "\n",
59
+ "mount MicroApi::Engine, at: MicroApi.routes_path, as: '#{MicroApi.routes_path}'",
60
+ "match '*path', to: 'micro_api/static#no_route_matches', via: :all"
61
+ ]
62
+
63
+ inject_into_file 'config/routes.rb', before: /\nend$/ do
64
+ routes_list.join("\n ").gsub(" \n", "\n")
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ require 'micro_api'
2
+
3
+ MicroApi.setup do |config|
4
+ config.routes_path = "/mse"
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "production"
2
+
3
+ Rails.application.configure do
4
+ # Your overriders goes here...
5
+ end
@@ -1,8 +1,34 @@
1
+ require 'lograge'
2
+
1
3
  module MicroApi
2
4
  class Engine < ::Rails::Engine
3
5
  isolate_namespace MicroApi
4
6
  config.generators.api_only = true
5
7
 
8
+ initializer "micro_api.lograge.init" do |app|
9
+ app.configure do
10
+ config.lograge.enabled = true
11
+ config.lograge.base_controller_class = "ActionController::API"
12
+ config.lograge.formatter = Lograge::Formatters::Json.new
13
+ config.colorize_logging = false
14
+ config.lograge.logger = ActiveSupport::Logger.new($stdout)
15
+
16
+ config.lograge.ignore_custom = lambda do |event|
17
+ event.payload[:path] == "#{MicroApi.routes_path}/healthz" && event.payload[:request].remote_ip =~ /^10\./
18
+ end
19
+ config.lograge.custom_payload do |controller|
20
+ {
21
+ level: :info,
22
+ log_type: :rails,
23
+ request_id: controller.request.request_id,
24
+ host: controller.request.host,
25
+ remote_ip: controller.request.remote_ip,
26
+ image_tag: ENV['IMAGE_TAG']
27
+ }
28
+ end
29
+ end
30
+ end
31
+
6
32
  config.generators do |g|
7
33
  g.test_framework :rspec,
8
34
  controller_specs: true,
@@ -1,3 +1,3 @@
1
1
  module MicroApi
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/micro_api.rb CHANGED
@@ -2,5 +2,12 @@ require "micro_api/version"
2
2
  require "micro_api/engine"
3
3
 
4
4
  module MicroApi
5
- # Your code goes here...
5
+
6
+ mattr_accessor :routes_path
7
+ @@routes_path = "/mse"
8
+
9
+ def self.setup
10
+ yield self
11
+ end
12
+
6
13
  end
@@ -37,5 +37,7 @@ module Dummy
37
37
  config.api_only = true
38
38
 
39
39
  config.hosts << "www.example.com"
40
+
41
+ config.time_zone = 'CET'
40
42
  end
41
43
  end
@@ -1,3 +1,14 @@
1
1
  Rails.application.routes.draw do
2
- mount MicroApi::Engine => "/micro_api"
2
+ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
3
+
4
+ namespace :api, defaults: { format: :json } do
5
+ end
6
+
7
+ # Defines the root path route ("/")
8
+ # root "articles#index"
9
+
10
+ # mount MicroApi::Engine => "/micro_api"
11
+
12
+ mount MicroApi::Engine, at: MicroApi.routes_path, as: '/mse'
13
+ match '*path', to: 'micro_api/static#no_route_matches', via: :all
3
14
  end
@@ -0,0 +1,34 @@
1
+ require 'rails_helper'
2
+ require 'rails/generators'
3
+ require 'generators/micro_api/install/install_generator'
4
+
5
+ RSpec.describe MicroApi::InstallGenerator, type: :generator do
6
+ before :all do
7
+ remove_config
8
+ end
9
+
10
+ after :all do
11
+ remove_config
12
+ end
13
+
14
+ it 'installs config file properly' do
15
+ described_class.start
16
+ system "cd spec/dummy/ && bin/rails generate micro_api:install"
17
+ expect(File.file?(config_file)).to be true
18
+ expect(File.file?(staging_env_file)).to be true
19
+ end
20
+
21
+ it 'installs config file properly routing', type: :routing do
22
+ expect(get("#{MicroApi.routes_path}/version")).to route_to(
23
+ controller: 'micro_api/static',
24
+ action: 'version',
25
+ format: :json
26
+ )
27
+
28
+ expect(get("#{MicroApi.routes_path}/healthz")).to route_to(
29
+ controller: 'micro_api/static',
30
+ action: 'healthz',
31
+ format: :json
32
+ )
33
+ end
34
+ end
data/spec/rails_helper.rb CHANGED
@@ -57,4 +57,5 @@ RSpec.configure do |config|
57
57
  # config.filter_gems_from_backtrace("gem name")
58
58
 
59
59
  config.include RequestSpecHelper, type: :request
60
+ config.include FileSpecHelper, type: :generator
60
61
  end
@@ -0,0 +1,21 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "Statics" do
4
+ describe "GET /healthz" do
5
+ before { @route = get("#{MicroApi.routes_path}/healthz") }
6
+
7
+ it "returns http success", type: :request do
8
+ expect(response).to have_http_status(:success)
9
+ expect(response.header['Content-Type']).to include 'application/json'
10
+ expect(json).to have_key('status')
11
+ end
12
+
13
+ it "routes /micro_api/version to the static controller", type: :routing do
14
+ expect(@route).to route_to(
15
+ controller: 'micro_api/static',
16
+ action: 'healthz',
17
+ format: :json
18
+ )
19
+ end
20
+ end
21
+ end
@@ -1,25 +1,14 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe "Statics", type: :request do
4
- describe "GET /healthz" do
5
- it "returns http success" do
6
- get "/micro_api/healthz"
7
- expect(response).to have_http_status(:success)
8
- expect(response.header['Content-Type']).to include 'application/json'
9
- expect(json).not_to be_empty
10
- expect(json).to have_key('status')
11
- end
12
- end
13
-
14
- describe "GET /version" do
15
- it "returns http success" do
16
- get "/micro_api/version"
17
- expect(response).to have_http_status(:success)
18
- expect(response.header['Content-Type']).to include 'application/json'
19
- expect(json).not_to be_empty
20
- expect(json).to have_key('env')
21
- expect(json).to have_key('cenv')
22
- expect(json).to have_key('itag')
4
+ describe "GET /" do
5
+ context 'when the route does not exist' do
6
+ it "returns http success" do
7
+ get "#{MicroApi.routes_path}/"
8
+ expect(response).to have_http_status(:not_found)
9
+ expect(response.header['Content-Type']).to include 'application/json'
10
+ expect(json).not_to be_empty
11
+ end
23
12
  end
24
13
  end
25
14
  end
@@ -0,0 +1,21 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "Statics" do
4
+ describe "GET /version" do
5
+ before { @route = get("#{MicroApi.routes_path}/version") }
6
+
7
+ it "returns http success", type: :request do
8
+ expect(response).to have_http_status(:success)
9
+ expect(response.header['Content-Type']).to include 'application/json'
10
+ expect(json.keys).to contain_exactly('ac', 'cenv', 'env', 'itag')
11
+ end
12
+
13
+ it "routes /micro_api/version to the static controller", type: :routing do
14
+ expect(@route).to route_to(
15
+ controller: 'micro_api/static',
16
+ action: 'version',
17
+ format: :json
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ require 'fileutils'
2
+
3
+ module FileSpecHelper
4
+ def config_file
5
+ "#{Rails.root}/config/initializers/micro_api.rb"
6
+ end
7
+
8
+ def staging_env_file
9
+ "#{Rails.root}/config/environments/staging.rb"
10
+ end
11
+
12
+ def remove_config
13
+ FileUtils.remove_file config_file if File.file?(config_file)
14
+ FileUtils.remove_file staging_env_file if File.file?(staging_env_file)
15
+ end
16
+ end
@@ -2,6 +2,6 @@ module RequestSpecHelper
2
2
  def json
3
3
  @json ||= JSON.parse(response.body)
4
4
  rescue
5
- @json ||= nil
5
+ @json ||= {}
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,31 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micro_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-19 00:00:00.000000000 Z
11
+ date: 2023-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 6.0.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 6.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.48'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.48'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
@@ -39,13 +53,13 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: simplecov
56
+ name: lograge
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
- type: :development
62
+ type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
@@ -58,14 +72,14 @@ dependencies:
58
72
  requirements:
59
73
  - - ">="
60
74
  - !ruby/object:Gem::Version
61
- version: 7.0.4.3
75
+ version: 6.0.0
62
76
  type: :runtime
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: 7.0.4.3
82
+ version: 6.0.0
69
83
  description: Rails engine plugin that would like to help the startup of rails applications
70
84
  oriented to microservices or, in general, to the cloud.
71
85
  email:
@@ -81,6 +95,10 @@ files:
81
95
  - app/controllers/micro_api/static_controller.rb
82
96
  - app/models/micro_api/application_record.rb
83
97
  - config/routes.rb
98
+ - lib/generators/micro_api/install/USAGE
99
+ - lib/generators/micro_api/install/install_generator.rb
100
+ - lib/generators/micro_api/install/templates/micro_api.rb
101
+ - lib/generators/micro_api/install/templates/staging.rb
84
102
  - lib/micro_api.rb
85
103
  - lib/micro_api/engine.rb
86
104
  - lib/micro_api/version.rb
@@ -103,9 +121,13 @@ files:
103
121
  - spec/dummy/config/locales/en.yml
104
122
  - spec/dummy/config/puma.rb
105
123
  - spec/dummy/config/routes.rb
124
+ - spec/generator/micro_api/installs_generator_spec.rb
106
125
  - spec/rails_helper.rb
126
+ - spec/requests/micro_api/static_healthz_spec.rb
107
127
  - spec/requests/micro_api/static_spec.rb
128
+ - spec/requests/micro_api/static_version_spec.rb
108
129
  - spec/spec_helper.rb
130
+ - spec/support/file_spec_helper.rb
109
131
  - spec/support/request_spec_helper.rb
110
132
  homepage: https://github.com/lelered/micro_api
111
133
  licenses:
@@ -130,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
152
  - !ruby/object:Gem::Version
131
153
  version: '0'
132
154
  requirements: []
133
- rubygems_version: 3.4.6
155
+ rubygems_version: 3.4.10
134
156
  signing_key:
135
157
  specification_version: 4
136
158
  summary: Engine plugin for Ruby on Rails applications
@@ -153,7 +175,11 @@ test_files:
153
175
  - spec/dummy/config/puma.rb
154
176
  - spec/dummy/config/routes.rb
155
177
  - spec/dummy/config.ru
178
+ - spec/generator/micro_api/installs_generator_spec.rb
156
179
  - spec/rails_helper.rb
180
+ - spec/requests/micro_api/static_healthz_spec.rb
157
181
  - spec/requests/micro_api/static_spec.rb
182
+ - spec/requests/micro_api/static_version_spec.rb
158
183
  - spec/spec_helper.rb
184
+ - spec/support/file_spec_helper.rb
159
185
  - spec/support/request_spec_helper.rb