service_template 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.rubocop.yml +23 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.md +64 -0
- data/Gemfile +4 -0
- data/LICENSE +24 -0
- data/README.md +217 -0
- data/Rakefile +9 -0
- data/bin/service_template +5 -0
- data/lib/service_template.rb +55 -0
- data/lib/service_template/active_record_extensions/notifications_subscriber.rb +17 -0
- data/lib/service_template/active_record_extensions/seeder.rb +14 -0
- data/lib/service_template/active_record_extensions/stats.rb +37 -0
- data/lib/service_template/authentication.rb +8 -0
- data/lib/service_template/cli.rb +111 -0
- data/lib/service_template/deploy.rb +98 -0
- data/lib/service_template/gem_dependency.rb +37 -0
- data/lib/service_template/generators.rb +3 -0
- data/lib/service_template/generators/api_generator.rb +30 -0
- data/lib/service_template/generators/readme_generator.rb +47 -0
- data/lib/service_template/generators/scaffold_generator.rb +29 -0
- data/lib/service_template/generators/templates/api/app/apis/%name_tableize%_api.rb.tt +40 -0
- data/lib/service_template/generators/templates/api/app/models/%name_underscore%.rb.tt +2 -0
- data/lib/service_template/generators/templates/api/app/representers/%name_underscore%_representer.rb.tt +4 -0
- data/lib/service_template/generators/templates/api/spec/apis/%name_tableize%_api_spec.rb.tt +16 -0
- data/lib/service_template/generators/templates/api/spec/models/%name_underscore%_spec.rb.tt +9 -0
- data/lib/service_template/generators/templates/readme/README.md.tt +55 -0
- data/lib/service_template/generators/templates/readme/spec/docs/readme_spec.rb +7 -0
- data/lib/service_template/generators/templates/scaffold/.env.development.tt +9 -0
- data/lib/service_template/generators/templates/scaffold/.env.test.tt +10 -0
- data/lib/service_template/generators/templates/scaffold/.gitignore.tt +13 -0
- data/lib/service_template/generators/templates/scaffold/.rubocop.yml +24 -0
- data/lib/service_template/generators/templates/scaffold/.ruby-version.tt +1 -0
- data/lib/service_template/generators/templates/scaffold/Gemfile.tt +29 -0
- data/lib/service_template/generators/templates/scaffold/README.md +3 -0
- data/lib/service_template/generators/templates/scaffold/Rakefile +21 -0
- data/lib/service_template/generators/templates/scaffold/app.rb +19 -0
- data/lib/service_template/generators/templates/scaffold/app/apis/application_api.rb +9 -0
- data/lib/service_template/generators/templates/scaffold/app/apis/hello_api.rb.tt +10 -0
- data/lib/service_template/generators/templates/scaffold/config.ru.tt +21 -0
- data/lib/service_template/generators/templates/scaffold/config/database.yml.tt +19 -0
- data/lib/service_template/generators/templates/scaffold/config/initializers/active_record.rb +5 -0
- data/lib/service_template/generators/templates/scaffold/db/schema.rb +11 -0
- data/lib/service_template/generators/templates/scaffold/lib/.keep +0 -0
- data/lib/service_template/generators/templates/scaffold/log/.keep +0 -0
- data/lib/service_template/generators/templates/scaffold/spec/apis/hello_api_spec.rb.tt +17 -0
- data/lib/service_template/generators/templates/scaffold/spec/factories/.gitkeep +0 -0
- data/lib/service_template/generators/templates/scaffold/spec/spec_helper.rb +47 -0
- data/lib/service_template/grape_extenders.rb +30 -0
- data/lib/service_template/grape_extensions/error_formatter.rb +18 -0
- data/lib/service_template/grape_extensions/grape_helpers.rb +27 -0
- data/lib/service_template/identity.rb +45 -0
- data/lib/service_template/json_error.rb +24 -0
- data/lib/service_template/logger/log_transaction.rb +17 -0
- data/lib/service_template/logger/logger.rb +42 -0
- data/lib/service_template/logger/parseable.rb +37 -0
- data/lib/service_template/middleware/app_monitor.rb +17 -0
- data/lib/service_template/middleware/authentication.rb +32 -0
- data/lib/service_template/middleware/database_stats.rb +15 -0
- data/lib/service_template/middleware/logger.rb +67 -0
- data/lib/service_template/middleware/request_stats.rb +42 -0
- data/lib/service_template/output_formatters/entity.rb +15 -0
- data/lib/service_template/output_formatters/include_nil.rb +16 -0
- data/lib/service_template/output_formatters/json_api_representer.rb +9 -0
- data/lib/service_template/param_sanitizer.rb +30 -0
- data/lib/service_template/rspec_extensions/response_helpers.rb +46 -0
- data/lib/service_template/setup.rb +36 -0
- data/lib/service_template/sortable_api.rb +17 -0
- data/lib/service_template/stats.rb +43 -0
- data/lib/service_template/stats_d_timer.rb +26 -0
- data/lib/service_template/version.rb +45 -0
- data/lib/tasks/deploy.rake +11 -0
- data/lib/tasks/routes.rake +11 -0
- data/service_template.gemspec +42 -0
- data/spec/active_record_extensions/filter_by_hash_spec.rb +23 -0
- data/spec/active_record_extensions/seeder_spec.rb +13 -0
- data/spec/authentication_spec.rb +17 -0
- data/spec/deprecations/application_api_spec.rb +19 -0
- data/spec/deprecations/entity_spec.rb +9 -0
- data/spec/deprecations/filter_by_hash_spec.rb +9 -0
- data/spec/deprecations/napa_setup_spec.rb +52 -0
- data/spec/generators/api_generator_spec.rb +63 -0
- data/spec/generators/migration_generator_spec.rb +105 -0
- data/spec/generators/readme_generator_spec.rb +35 -0
- data/spec/generators/scaffold_generator_spec.rb +90 -0
- data/spec/grape_extenders_spec.rb +50 -0
- data/spec/grape_extensions/error_formatter_spec.rb +29 -0
- data/spec/grape_extensions/include_nil_spec.rb +23 -0
- data/spec/identity_spec.rb +50 -0
- data/spec/json_error_spec.rb +33 -0
- data/spec/logger/log_transaction_spec.rb +34 -0
- data/spec/logger/logger_spec.rb +14 -0
- data/spec/logger/parseable_spec.rb +16 -0
- data/spec/middleware/authentication_spec.rb +54 -0
- data/spec/middleware/database_stats_spec.rb +64 -0
- data/spec/middleware/request_stats_spec.rb +21 -0
- data/spec/sortable_api_spec.rb +56 -0
- data/spec/spec_helper.rb +45 -0
- data/spec/stats_d_timer_spec.rb +23 -0
- data/spec/stats_spec.rb +66 -0
- data/spec/version_spec.rb +40 -0
- data/tasks/spec.rake +9 -0
- data/tasks/version.rake +51 -0
- metadata +456 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 02a8715ac1b0820f58fd3e031d1b293903eb2e7b
|
|
4
|
+
data.tar.gz: 6b83d3dd52b80b7b7abb0d3c0429f5e8537be154
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3ac128223d3b4c1e455c6e90d4d6d74969a47ab409e2b4add409713e4206fd8ee95f11762d94738d17abae2809a504a54fb1537ea64cc6cc7b54c1a1c1e29e20
|
|
7
|
+
data.tar.gz: 98a4fb6716da38f790d051dc8447798c4fcb25941901a821c40154c8f7edb5feefcba6405a11813a69df9656e5b53dd4ee39aef14fc55d7fcb5a102fd3461f4f
|
data/.gitignore
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
.bundle
|
|
4
|
+
.config
|
|
5
|
+
.yardoc
|
|
6
|
+
Gemfile.lock
|
|
7
|
+
InstalledFiles
|
|
8
|
+
_yardoc
|
|
9
|
+
coverage
|
|
10
|
+
doc/
|
|
11
|
+
lib/bundler/man
|
|
12
|
+
pkg
|
|
13
|
+
rdoc
|
|
14
|
+
spec/reports
|
|
15
|
+
test/tmp
|
|
16
|
+
test/version_tmp
|
|
17
|
+
tmp
|
|
18
|
+
.ruby-version
|
|
19
|
+
.ruby-gemset
|
|
20
|
+
.rspec
|
|
21
|
+
.DS_Store
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Documentation:
|
|
2
|
+
Enabled: false
|
|
3
|
+
|
|
4
|
+
Encoding:
|
|
5
|
+
Enabled: false
|
|
6
|
+
|
|
7
|
+
HandleExceptions:
|
|
8
|
+
Enabled: false
|
|
9
|
+
|
|
10
|
+
LineLength:
|
|
11
|
+
Max: 120
|
|
12
|
+
|
|
13
|
+
NumericLiterals:
|
|
14
|
+
Enabled: false
|
|
15
|
+
|
|
16
|
+
MethodLength:
|
|
17
|
+
Enabled: false
|
|
18
|
+
|
|
19
|
+
AllCops:
|
|
20
|
+
Excludes:
|
|
21
|
+
- db/**
|
|
22
|
+
- config/**
|
|
23
|
+
- script/**
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
master
|
|
2
|
+
===
|
|
3
|
+
* [Explicitly require `filter_parameters.rb` instead of relying on `action_dispatch` autoload](https://github.com/bellycard/service_template/pull/181)
|
|
4
|
+
* [Default rake task runs specs](https://github.com/bellycard/service_template/pull/176)
|
|
5
|
+
* [Set ServiceTemplate.env to the environment arg we pass in the console](https://github.com/bellycard/service_template/pull/179)
|
|
6
|
+
* [Add thor command to start shotgun server](https://github.com/bellycard/service_template/pull/177)
|
|
7
|
+
|
|
8
|
+
0.4.1
|
|
9
|
+
===
|
|
10
|
+
* Added a reasons object to error responses to represent active record validation errors for individual attributes
|
|
11
|
+
* Added ability to filter sensitive data from logs.
|
|
12
|
+
|
|
13
|
+
0.4.0
|
|
14
|
+
===
|
|
15
|
+
* Added `ServiceTemplate.cache` to wrap `ActiveSupport::Cache`
|
|
16
|
+
* Added README generator
|
|
17
|
+
* Update the HoneyBadger scaffolding
|
|
18
|
+
* Fixed a bug with ServiceTemplate::StatsDTimer where time was being reported in seconds, not milliseconds
|
|
19
|
+
* Removed additional StatsD counter metric for request stats middleware
|
|
20
|
+
* Added new deploy CLI with `force` and `revision` options `service_template deploy production`
|
|
21
|
+
* Added deprecation warnings
|
|
22
|
+
* Added initialization hook to run code when a ServiceTemplate service boots
|
|
23
|
+
* Added Migration generators from Rails
|
|
24
|
+
* Added rake db:seed functionality
|
|
25
|
+
* Added some convenience methods to spec_helper
|
|
26
|
+
|
|
27
|
+
0.3.0
|
|
28
|
+
===
|
|
29
|
+
* Added `rake db:rollback` to rollback migrations just like Rails
|
|
30
|
+
* Fixed bug in migration generator causing constant not defined errors
|
|
31
|
+
* Fixed CORS config in scaffold generator
|
|
32
|
+
* Fixed logging bug in grape_extenders
|
|
33
|
+
* Set UTF-8 encoding in generated database.yml
|
|
34
|
+
* Removed unneeded gem dependencies (shotgun and unicorn)
|
|
35
|
+
* Fixed spec_helper that gets generated to ignore spec files and gems (on CI servers)
|
|
36
|
+
* Added spec response helpers `parsed_response`, `response_code` and `response_body` to make tests easier to DRY up
|
|
37
|
+
* Removed #filter and `include ServiceTemplate::FilterByHash` from generated code.
|
|
38
|
+
* Fix when using IRB and service_template console
|
|
39
|
+
* Added IncludeNil module for Representable/Roar output
|
|
40
|
+
* Template updates to include spec files for APIs
|
|
41
|
+
* Removing FilterByHash in the API template
|
|
42
|
+
* Fix when ErrorFormatter is passed a non-hash
|
|
43
|
+
* Added more descriptive messages on git based deploy errors
|
|
44
|
+
* Added RequestStats and DatabaseStats middlewares to report data to StatsD
|
|
45
|
+
* All String logs are now wrapped in a hash before being written to the log file
|
|
46
|
+
|
|
47
|
+
0.2.1
|
|
48
|
+
===
|
|
49
|
+
* Updated ServiceTemplate console. It now takes an optional environment parameter, i.e. `service_template console production`.
|
|
50
|
+
* Added `c` alias for ServiceTemplate console, i.e. `service_template c` or `service_template c production`
|
|
51
|
+
* Fixed a bug causing `rake db:schema:load` to fail
|
|
52
|
+
* Fixed a bug affecting `rake db:create` and `rake db:drop` using Postgres
|
|
53
|
+
* Fixed a bug ServiceTemplate::GrapeHelpers to bypass the representer when given an array
|
|
54
|
+
|
|
55
|
+
0.2.0
|
|
56
|
+
===
|
|
57
|
+
* The console is now run with `service_template console`, added support for racksh
|
|
58
|
+
* Scaffold generator now supports the `--database (-d)` flag
|
|
59
|
+
* Scaffold generator now supports Mysql or Postgres with ActiveRecord
|
|
60
|
+
* Scaffold generator now uses Roar instead of Grape Entity
|
|
61
|
+
* Fixed a bug in `rake routes`
|
|
62
|
+
* Fixed a bug in `rake db:reset`
|
|
63
|
+
* Added StatsD instrumentation (experimental)
|
|
64
|
+
* Added a CHANGELOG
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Copyright (c) 2013 Bellycard Inc.
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
“ServiceTemplate”, and the ServiceTemplate logo are registered trademarks of Bellycard Inc. All rights reserved.
|
data/README.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
THIS IS A FORK and different opinions of a VERY good framwork called Napa
|
|
2
|
+
https://github.com/bellycard/napa
|
|
3
|
+
|
|
4
|
+
Here are the main differences
|
|
5
|
+
+ Roar with hypermedia JSON-API default rather than a bespoke response format
|
|
6
|
+
+ ActiveRecord 4.2 rather than 4.0
|
|
7
|
+
+ Rollbar rather than Honeybadger
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
ServiceTemplate is available as a gem, to install it run:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
gem install service_template
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or, if you're using Bundler, add it to your Gemfile:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
gem 'service_template'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
And run:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
$ bundle install
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Getting Started
|
|
30
|
+
|
|
31
|
+
See the [Quickstart Guide](https://github.com/bellycard/service_template/blob/master/docs/quickstart.md) for an intro to creating a simple service with ServiceTemplate.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
Run `service_template` terminal prompt to see available features:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
Commands:
|
|
39
|
+
service_template console [environment] # Start the ServiceTemplate console
|
|
40
|
+
service_template server # Start the ServiceTemplate server
|
|
41
|
+
service_template generate api <api_name> # Create a Grape API, Model and Representer
|
|
42
|
+
service_template generate migration <migration_name> [field[:type][:index] field[:type][:index]] # Create a Database Migration
|
|
43
|
+
service_template generate readme # Create a formatted README
|
|
44
|
+
service_template deploy <environment> # Deploy to <environment> by setting a git tag
|
|
45
|
+
service_template help [COMMAND] # Describe available commands or one specific command
|
|
46
|
+
service_template new <app_name> [app_path] # Create a scaffold for a new ServiceTemplate service
|
|
47
|
+
service_template version # Shows the ServiceTemplate version number
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
### Console
|
|
52
|
+
Similar to the Rails console, load an IRB session with your applications environment by running:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
service_template console
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Deploy
|
|
59
|
+
ServiceTemplate provides a CLI for deploying to a given environment by setting a git tag. This is useful for chef-based deploys where deploys are trigged when a git SHA changes.
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
service_template deploy production
|
|
63
|
+
Are you sure you want to deploy this service? Y
|
|
64
|
+
#=> <git SHA> tagged as production by danielmackey at October 09, 2014 14:41
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If you want to skip the 'Are you sure?' prompt, pass the `--confirm` flag to set the tag automatically
|
|
68
|
+
```sh
|
|
69
|
+
service_template deploy production --confirm
|
|
70
|
+
#=> <git SHA> tagged as production by danielmackey at October 09, 2014 14:41
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Rake Tasks
|
|
74
|
+
|
|
75
|
+
`rake -T` will give you a list of all available rake tasks:
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
rake db:create # Create the database
|
|
79
|
+
rake db:drop # Delete the database
|
|
80
|
+
rake db:migrate # Migrate the database through scripts in db/migrate
|
|
81
|
+
rake db:reset # Create the test database
|
|
82
|
+
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
|
|
83
|
+
rake db:schema:load # Load a schema.rb file into the database
|
|
84
|
+
rake db:seed # Load the seed data from db/seeds.rb
|
|
85
|
+
rake git:set_tag[tag] # Set tag, which triggers deploy
|
|
86
|
+
rake git:verify # Verify git repository is in a good state for deployment
|
|
87
|
+
rake routes # display all routes for Grape
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Middlewares
|
|
91
|
+
ServiceTemplate includes a number of Rack middlewares that can be enabled to add functionality to your project.
|
|
92
|
+
|
|
93
|
+
### Authentication
|
|
94
|
+
The Authentication middleware will add a simple header based authentication layer to all requests. This is just looking for a header of `'Password' = 'Your Password'`. The passwords are defined in the `.env` file. You can allow multiple passwords by supplying a comma separated list. For example:
|
|
95
|
+
|
|
96
|
+
`HEADER_PASSWORDS='password1,password2'`
|
|
97
|
+
|
|
98
|
+
If your application doesn't require authentication, you can simply remove the middleware.
|
|
99
|
+
|
|
100
|
+
### Health Check
|
|
101
|
+
The Health Check middleware will add an endpoint at `/health` that will return some data about your app. This was created to allow monitoring tools a standardized way to monitor multiple services. This endpoint will return a response similar to this:
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
{
|
|
105
|
+
"name": "service-name",
|
|
106
|
+
"hostname": "host-name",
|
|
107
|
+
"revision": "current-git-sha-of-app",
|
|
108
|
+
"pid": 1234,
|
|
109
|
+
"parent_pid": 1233,
|
|
110
|
+
"service_template_revision": "running-version-of-service_template"
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Logger
|
|
115
|
+
The *Logger* module is used to create a common log format across applications. The Logger is enable via a rack middleware by adding the line below to your `config.ru` file:
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
use ServiceTemplate::Middleware::Logger
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
You can also enable the logger for ActiveRecord by adding the following line to an initializer:
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
ActiveRecord::Base.logger = ServiceTemplate::Logger.logger
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`ServiceTemplate::Logger.logger` returns a Singleton instance of the Logging object, so it can be passed to other libraries or called directly. For example:
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
ServiceTemplate::Logger.logger.debug 'Some Debug Message'
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Scrubbing Logs of Sensitive Data
|
|
134
|
+
|
|
135
|
+
Some requests may contain sensitive information, such as passwords or credit card numbers. In order to protect this information, they should be filtered out from logs.
|
|
136
|
+
|
|
137
|
+
To do so, add the following line to an initializer:
|
|
138
|
+
```ruby
|
|
139
|
+
ServiceTemplate::ParamSanitizer.filter_params = [:password, :password_confirmation, :cvv, :card_number]
|
|
140
|
+
```
|
|
141
|
+
Note that the keys in the array above are just examples. They should be replaced with the parameters that have sensitive data in their value.
|
|
142
|
+
|
|
143
|
+
Example **unfiltered** request ( ... denotes other information):
|
|
144
|
+
```
|
|
145
|
+
{ ... "message":{"request":{"method":"POST","path":"/example","query":"name=Test%20User%200039\u0026password=password", ... "params":{"name":"Test User 0039","password":"password"} ...}}}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Example **filtered** request ( ... denotes other information):
|
|
149
|
+
```
|
|
150
|
+
{ ... "message":{"request":{"method":"POST","path":"/example","query":"name=Test%20User%200039\u0026password=[FILTERED]", ... "params":{"name":"Test User 0039","password":"[FILTERED]"} ...}}}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### StatsD
|
|
154
|
+
There are two middlewares available to enable StatsD reporting, `RequestStats` and `DatabaseStats`. They can be enabled independently in your `config.ru` file:
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
use ServiceTemplate::Middleware::RequestStats
|
|
158
|
+
use ServiceTemplate::Middleware::DatabaseStats
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**RequestStats** will emit information about your application's request count and response time.
|
|
162
|
+
|
|
163
|
+
**DatabaseStats** will emit information from ActiveRecord about query times.
|
|
164
|
+
|
|
165
|
+
##### Configuration
|
|
166
|
+
|
|
167
|
+
To configure StatsD in your application you will need to supply the `STATSD_HOST` and `STATSD_PORT` in your environment. Optionally, if your StatsD host requires an api token (i.e. hostedgraphite), you can configure that with the `STATSD_API_KEY` environment variable.
|
|
168
|
+
|
|
169
|
+
##### Logging
|
|
170
|
+
|
|
171
|
+
If you want to see the StatsD reporting in action you can hook up the logger to the ServiceTemplate logger to see the requests in your logs.
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
Statsd.logger = ServiceTemplate::Logger.logger
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Caching
|
|
178
|
+
|
|
179
|
+
ServiceTemplate adds a simple wrapper around `ActiveSupport::Cache` that allows you to easily access it similar to how it works in Rails. `ServiceTemplate.cache` will give you access to all of the methods available in `ActiveSupport::Cache::Store` [http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html](). So, for example:
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
ServiceTemplate.cache.read
|
|
183
|
+
ServiceTemplate.cache.write
|
|
184
|
+
ServiceTemplate.cache.fetch
|
|
185
|
+
...
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
By default it will use `:memory_store`, but you can override it to use any other caching strategy, like Memcache by setting the store:
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
ServiceTemplate.cache = :dalli_store
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Sorting
|
|
195
|
+
|
|
196
|
+
ServiceTemplate has an optional module you can include in any Api called
|
|
197
|
+
`ServiceTemplate::SortableApi`. To include this, add `include SortableApi` in the
|
|
198
|
+
`helpers` block of the Api.
|
|
199
|
+
|
|
200
|
+
`SortableApi` takes in a parameter for sort in the format of
|
|
201
|
+
`field1,field2,-field3`, where `field1` and `field2` are used to sort
|
|
202
|
+
ascending, and `field3` is sorted descending. For example,
|
|
203
|
+
`-field4,field1` would be equivalent to `ORDER BY field4 DESC, field1'.
|
|
204
|
+
|
|
205
|
+
Call `sorted_from_params(ar_relation, params[:sort])` passing in an
|
|
206
|
+
`ActiveRecord::Relation` for `ar_relation`, and a comma-delimited string of field names for `params[:sort]`.
|
|
207
|
+
|
|
208
|
+
## Bugs & Feature Requests
|
|
209
|
+
Please add an issue in [Github](https://github.com/bellycard/service_template/issues) if you discover a bug or have a feature request.
|
|
210
|
+
|
|
211
|
+
## Contributing
|
|
212
|
+
|
|
213
|
+
1. Fork it
|
|
214
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
215
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
216
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
217
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env rake
|
|
2
|
+
$LOAD_PATH.unshift 'lib', __dir__
|
|
3
|
+
|
|
4
|
+
Dir.glob('./tasks/*.rake').each { |r| import r }
|
|
5
|
+
require 'bundler/gem_tasks'
|
|
6
|
+
require 'service_template/active_record_extensions/stats.rb'
|
|
7
|
+
require 'service_template/active_record_extensions/seeder.rb'
|
|
8
|
+
|
|
9
|
+
task default: :spec
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# require external libraries
|
|
2
|
+
require 'rake'
|
|
3
|
+
require 'dotenv'
|
|
4
|
+
require 'logging'
|
|
5
|
+
require 'octokit'
|
|
6
|
+
require 'grape'
|
|
7
|
+
require 'grape-entity'
|
|
8
|
+
require 'json'
|
|
9
|
+
|
|
10
|
+
# require internal files
|
|
11
|
+
require 'service_template/setup'
|
|
12
|
+
require 'service_template/version'
|
|
13
|
+
require 'service_template/logger/logger'
|
|
14
|
+
require 'service_template/logger/log_transaction'
|
|
15
|
+
require 'service_template/logger/parseable'
|
|
16
|
+
require 'service_template/identity'
|
|
17
|
+
require 'service_template/json_error'
|
|
18
|
+
require 'service_template/stats'
|
|
19
|
+
require 'service_template/stats_d_timer'
|
|
20
|
+
require 'service_template/active_record_extensions/stats'
|
|
21
|
+
require 'service_template/active_record_extensions/seeder'
|
|
22
|
+
require 'service_template/grape_extensions/error_formatter'
|
|
23
|
+
require 'service_template/grape_extensions/grape_helpers'
|
|
24
|
+
require 'service_template/output_formatters/entity'
|
|
25
|
+
require 'service_template/output_formatters/include_nil'
|
|
26
|
+
require 'service_template/output_formatters/json_api_representer'
|
|
27
|
+
require 'service_template/grape_extenders'
|
|
28
|
+
require 'service_template/middleware/logger'
|
|
29
|
+
require 'service_template/middleware/app_monitor'
|
|
30
|
+
require 'service_template/middleware/authentication'
|
|
31
|
+
require 'service_template/middleware/request_stats'
|
|
32
|
+
require 'service_template/middleware/database_stats'
|
|
33
|
+
require 'service_template/authentication'
|
|
34
|
+
require 'service_template/sortable_api'
|
|
35
|
+
|
|
36
|
+
require 'service_template/deploy'
|
|
37
|
+
require 'service_template/gem_dependency'
|
|
38
|
+
|
|
39
|
+
# load rake tasks if Rake installed
|
|
40
|
+
if defined?(Rake)
|
|
41
|
+
load 'tasks/deploy.rake'
|
|
42
|
+
load 'tasks/routes.rake'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
module ServiceTemplate
|
|
46
|
+
class << self
|
|
47
|
+
def initialize
|
|
48
|
+
unless ServiceTemplate.skip_initialization
|
|
49
|
+
ServiceTemplate::Logger.logger.info ServiceTemplate::GemDependency.log_all if ServiceTemplate.env.production?
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
ServiceTemplate.initialize
|