team_fastlane-base_service 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.adoc +64 -0
- data/README.adoc +39 -0
- data/app/controllers/api_base_controller.rb +9 -0
- data/app/controllers/concerns/performance_logging.rb +16 -0
- data/app/controllers/concerns/responses.rb +50 -0
- data/app/controllers/health_check_controller.rb +19 -0
- data/bin/health_check +9 -0
- data/config/initializers/camelizer.rb +11 -0
- data/config/initializers/filter_parameter_logging.rb +8 -0
- data/config/initializers/performance_logger.rb +24 -0
- data/config/routes.rb +5 -0
- data/lib/team_fastlane/base_service/version.rb +7 -0
- data/lib/team_fastlane/base_service.rb +10 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 195fc359676df3324652ec431e1ca0ad5443816ab8009bb2068eb07d0dca78e9
|
4
|
+
data.tar.gz: c55fb78490e3e26f353ba669072b4227e7d87b96f24e5f38b4de572e5659cd82
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '094b279ed57eda10e552dd02a2c0e7fd5978ee8a8184f8facd5a808c5658be58513e2158618bfcc49400e00374628dfc36aaab051af64d3b12668e057b9fe8f3'
|
7
|
+
data.tar.gz: 8c79dbfb2e36760f9a60f6d5dae5290caa7d4508255e4230ddae592c19a3f83832c254d8bcb1de5e75e2b6f64961e453638c8709f5d16d5c095b47ee6e3cbfc1
|
data/CHANGELOG.adoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on https://keepachangelog.com[Keep a Changelog],
|
6
|
+
and this project adheres to https://semver.org[Semantic Versioning].
|
7
|
+
|
8
|
+
== Unreleased
|
9
|
+
|
10
|
+
== [1.0.0] - 2023-08-01
|
11
|
+
=== Changed
|
12
|
+
Renamed and restructured for release on rubygems
|
13
|
+
|
14
|
+
== [0.3.0] - 2023-06-20
|
15
|
+
=== Added
|
16
|
+
- New method for handling validation errors
|
17
|
+
|
18
|
+
== [0.2.1] - 2022-10-13
|
19
|
+
=== Changed
|
20
|
+
- Health check uses the connection pool now instead of creating a new connection
|
21
|
+
|
22
|
+
== [0.2.0] - 2022-06-03
|
23
|
+
=== Added
|
24
|
+
- Default behavior for parameter filtering
|
25
|
+
|
26
|
+
== [0.1.1] - 2022-03-29
|
27
|
+
=== Updated
|
28
|
+
- olive_branch version updated to 4.0.1
|
29
|
+
|
30
|
+
== [0.1.0]- 2022-01-03
|
31
|
+
=== Removed
|
32
|
+
- the rubymine debug mode fix seems to be no longer necessary
|
33
|
+
|
34
|
+
== [0.0.7]- 2021-09-07
|
35
|
+
=== Changed
|
36
|
+
- Revert change of 0.0.6 and inherit from ActionController::Base again. It wasn't working correctly in our setup.
|
37
|
+
|
38
|
+
== [0.0.6]- 2021-05-17
|
39
|
+
=== Changed
|
40
|
+
- Inherit from ApplicationController instead of ActionController::Base
|
41
|
+
(link:https://docs.rubocop.org/rubocop-rails/cops_rails.html#railsapplicationcontroller[Rubocop]).
|
42
|
+
|
43
|
+
== [0.0.5]- 2021-04-27
|
44
|
+
=== Changed
|
45
|
+
- Updated olive_branch dependency to v4.0.0.
|
46
|
+
|
47
|
+
== [0.0.4] - 2020-01-13
|
48
|
+
=== Added
|
49
|
+
- Save from ArgumentError with `unprocessable_entity` method.
|
50
|
+
|
51
|
+
== [0.0.3] - 2020-07-22
|
52
|
+
=== Added
|
53
|
+
- Added response method `no_content`.
|
54
|
+
|
55
|
+
=== Changed
|
56
|
+
- Use json instead of raw text body for error responses `not_found` and `unprocessable_entity`.
|
57
|
+
|
58
|
+
== [0.0.2] - 2020-07-21
|
59
|
+
=== Added
|
60
|
+
- Added response methods `created` and `unprocessable_entity`.
|
61
|
+
|
62
|
+
== [0.0.1] - 2020-06-16
|
63
|
+
=== Added
|
64
|
+
- Initial gem version with the extracted api controllers and health check.
|
data/README.adoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= Team Fastlane Base Service
|
2
|
+
|
3
|
+
A service base for a ruby micro service
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
As described by the link:../README.adoc[main readme].
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
=== Health Check
|
12
|
+
|
13
|
+
You can use the link:../bin/health_check[health_check] executable to provide a method of checking the health of the server (database included).
|
14
|
+
For example within a docker file like this:
|
15
|
+
|
16
|
+
[source, Dockerfile]
|
17
|
+
----
|
18
|
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=30s --retries=3 CMD [ "bundle", "exec", "health_check" ]
|
19
|
+
----
|
20
|
+
|
21
|
+
This makes use of an exposed endpoint called `/health_check`, which you can only use for a custom ping to check for it.
|
22
|
+
|
23
|
+
=== Performance Logging
|
24
|
+
|
25
|
+
There is an easy to use performance logging (please only use in development) included. Simply add `with_measure` around the block you want to measure.
|
26
|
+
|
27
|
+
The result will be added to the rails time stamp log output.
|
28
|
+
|
29
|
+
=== Default responses
|
30
|
+
|
31
|
+
This also provides some default json responses to be used in your controller. See link:./app/controllers/concerns/responses.rb[responses.rb] for more details.
|
32
|
+
|
33
|
+
To use these responses let your `ApplicationController` inherit from `ApiBaseController` and you are set.
|
34
|
+
|
35
|
+
=== Parameter handling
|
36
|
+
|
37
|
+
You get automatically camelCase to snake_case transformation for the request and the reverse for responses via the olive_branch gem.
|
38
|
+
|
39
|
+
There will be a default parameter filtering enabled. It filters passwords of various key names in development and everything in production.
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PerformanceLogging
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
attr_internal :runtimes
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def with_measure(key)
|
11
|
+
result = nil
|
12
|
+
self.runtimes ||= {}
|
13
|
+
runtimes[key] = Benchmark.ms { result = yield }
|
14
|
+
result
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Responses
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
def bad_request(errors = {})
|
9
|
+
render json: errors, status: :bad_request
|
10
|
+
end
|
11
|
+
|
12
|
+
def created(body = {})
|
13
|
+
render json: body, status: :created
|
14
|
+
end
|
15
|
+
|
16
|
+
def no_content
|
17
|
+
render json: {}, status: :no_content
|
18
|
+
end
|
19
|
+
|
20
|
+
def not_found(body = {})
|
21
|
+
render json: body, status: :not_found
|
22
|
+
end
|
23
|
+
|
24
|
+
def ok(content = {})
|
25
|
+
render json: content, status: :ok
|
26
|
+
end
|
27
|
+
|
28
|
+
def technical_error(error, status: :internal_server_error)
|
29
|
+
Rails.logger.error("#{error.message}\n#{error.backtrace.join("\n")}")
|
30
|
+
response = {error: 'Something technical went wrong. Please try again later'}
|
31
|
+
if Rails.env.development?
|
32
|
+
response[:exception] = "#{error.class.name} : #{error.message}"
|
33
|
+
response[:trace] = error.backtrace[0, 10]
|
34
|
+
end
|
35
|
+
render json: response, status: status
|
36
|
+
end
|
37
|
+
|
38
|
+
def unprocessable_entity(body = {})
|
39
|
+
render json: body, status: :unprocessable_entity
|
40
|
+
end
|
41
|
+
|
42
|
+
def unauthorized(error)
|
43
|
+
technical_error(error, status: :unauthorized)
|
44
|
+
end
|
45
|
+
|
46
|
+
def validation_error(error)
|
47
|
+
Rails.logger.warn("Malformed Request: #{error.message}") if Rails.env.production?
|
48
|
+
unprocessable_entity(exception: "#{error.class.name}: #{error.message}")
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class HealthCheckController < ApiBaseController
|
4
|
+
def check
|
5
|
+
return bad_request unless ActiveRecord::Base.connection_pool.with_connection(&:active?)
|
6
|
+
|
7
|
+
ok
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def bad_request(_errors = {})
|
13
|
+
render plain: 'Health Check failed', status: :bad_request
|
14
|
+
end
|
15
|
+
|
16
|
+
def ok(_content = {})
|
17
|
+
render plain: 'Health Check OK', status: :ok
|
18
|
+
end
|
19
|
+
end
|
data/bin/health_check
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'olive_branch'
|
4
|
+
|
5
|
+
rails_routes = ->(env) { env['PATH_INFO'].match(%r{^/rails}) }
|
6
|
+
|
7
|
+
Rails.application.middleware.use OliveBranch::Middleware,
|
8
|
+
inflection: 'camel',
|
9
|
+
exclude_params: rails_routes,
|
10
|
+
exclude_response: rails_routes,
|
11
|
+
content_type_check: ->(_) { true }
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Be sure to restart your server when you modify this file.
|
4
|
+
|
5
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
6
|
+
Rails.application.config.filter_parameters += %i[passw secret token _key crypt salt certificate otp ssn]
|
7
|
+
|
8
|
+
Rails.application.config.filter_parameters += [/.*/] if Rails.env.production?
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PerformanceLogger
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def append_info_to_payload(payload)
|
9
|
+
super
|
10
|
+
payload[:runtimes] = runtimes
|
11
|
+
end
|
12
|
+
|
13
|
+
class_methods do
|
14
|
+
def log_process_action(payload)
|
15
|
+
super.concat(runtime_messages(payload[:runtimes]))
|
16
|
+
end
|
17
|
+
|
18
|
+
def runtime_messages(runtimes)
|
19
|
+
runtimes&.map { |key, runtime| format("#{key.to_s.camelize}: %.1fms", runtime.to_f) } || []
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveSupport.on_load(:action_controller) { include PerformanceLogger }
|
data/config/routes.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: team_fastlane-base_service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Team Fastlane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: olive_branch
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.1
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
executables:
|
30
|
+
- health_check
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README.adoc
|
34
|
+
- CHANGELOG.adoc
|
35
|
+
files:
|
36
|
+
- CHANGELOG.adoc
|
37
|
+
- README.adoc
|
38
|
+
- app/controllers/api_base_controller.rb
|
39
|
+
- app/controllers/concerns/performance_logging.rb
|
40
|
+
- app/controllers/concerns/responses.rb
|
41
|
+
- app/controllers/health_check_controller.rb
|
42
|
+
- bin/health_check
|
43
|
+
- config/initializers/camelizer.rb
|
44
|
+
- config/initializers/filter_parameter_logging.rb
|
45
|
+
- config/initializers/performance_logger.rb
|
46
|
+
- config/routes.rb
|
47
|
+
- lib/team_fastlane/base_service.rb
|
48
|
+
- lib/team_fastlane/base_service/version.rb
|
49
|
+
homepage:
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata:
|
53
|
+
rubygems_mfa_required: 'true'
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '2.7'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.4.10
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Gem for an internal project
|
73
|
+
test_files: []
|