3
3
How to Run
4
4
----------
5
5
6
+ 1 . Prerequisites
7
+ * [ Log For ELK stack (Elastic Search, Logstash, Kibana)] ( #log-for-elk-stack-elastic-search-logstash-kibana )
8
+ * [ elk.yml config] ( #elkyml-config )
9
+ * [ lograge.rb with custom config] ( #logragerb-with-custom-config )
10
+ * [ ELK Setup] ( /rails_log_with_elk_setup.md )
6
11
1 . Setup
7
- 1 . non docker-compose
8
- 1 . Run Postgresql and migrate
9
- ``` bash
10
- bundle install
11
- ````
12
- ` ` ` bash
13
- rake docker:pg:init
14
- rake docker:pg:run
15
- ` ` `
16
- ` ` ` bash
17
- rake db:migrate
18
- rake db:migrate RAILS_ENV=test
19
- ` ` ` `
20
- ` ` ` bash
21
- rake db:seed
22
- ```
23
- 2. Run Redis for cache
24
- ` ` ` bash
25
- docker run --rm --name my-redis-container -p 6379:6379 -d redis redis-server --appendonly yes
26
- redis-cli -h localhost -p 7001
27
- ` ` `
28
- 3. Rails Server Run
29
- ` ` ` bash
30
- rails s
31
- ` ` `
32
- 2. docker-compose
12
+ > You can run with ``` docker-compose ``` or non docker-compose
13
+ 1 . docker-compose
14
+ > server run
15
+ ````
16
+ docker-compose up --build -d
17
+ ````
18
+ > db setup
33
19
````bash
34
- docker-compose up --build
35
20
docker-compose run web bundle exec rake db:test:load
36
21
docker-compose run web bundle exec rake db:migrate
37
22
docker-compose run web bundle exec rake db:seed
38
23
````
24
+ > Testing
25
+ ```bash
26
+ docker-compose run --no-deps web bundle exec rspec --format documentation
27
+ ```
28
+ > Rswag for documentation ```http://localhost:3000/api-docs/index.html```
29
+ ```bash
30
+ docker-compose run --no-deps web bundle exec rake rswag
31
+ ```
39
32
> rails console
40
33
```bash
41
34
docker-compose exec web bin/rails c
@@ -44,28 +37,39 @@ How to Run
44
37
```bash
45
38
docker-compose run --no-deps web bundle exec rake routes
46
39
```
47
- > rswag and rspec testing
40
+ 2. non docker-compose
41
+ > bundle
48
42
```bash
49
- docker-compose run --no-deps web bundle exec rake rswag
50
- docker-compose run --no-deps web bundle exec rspec --format documentation
43
+ bundle install
44
+ ```
45
+ > postgresql run
46
+ ```bash
47
+ rake docker:pg:init
48
+ rake docker:pg:run
49
+ ```
50
+ > migrate
51
+ ```bash
52
+ rake db:migrate RAILS_ENV=test
53
+ rake db:migrate
54
+ rake db:seed
55
+ ```
56
+ > redis run
57
+ ```bash
58
+ docker run --rm --name my-redis-container -p 6379:6379 -d redis redis-server --appendonly yes
59
+ redis-cli -h localhost -p 7001
60
+ ```
61
+ > server run
62
+ ```bash
63
+ rails s
64
+ ```
65
+ > Testing
66
+ ```bash
67
+ bundle exec rpsec --format documentation
68
+ ```
69
+ > Rswag for documentation ```http://localhost:3000/api-docs/index.html```
70
+ ```bash
71
+ rake rswag
51
72
```
52
- 2. ELK
53
- ` ` ` bash
54
- git clone https://github.com/deviantony/docker-elk
55
- cd docker-elk
56
- docker-compose exec -T elasticsearch bin/elasticsearch-setup-passwords auto --batch
57
- # https://github.com/deviantony/docker-elk#setting-up-user-authentication
58
- docker-compose up --build
59
- ` ` `
60
- 3. Testing
61
- ` ` ` bash
62
- bundle exec rpsec --format documentation
63
- ` ` `
64
- 4. Swagger - Restful Api Documentation
65
- ` ` ` bash
66
- rake rswag
67
- ` ` `
68
- > http://localhost:3000/api-docs/index.html
69
73
70
74
TODO
71
75
----
@@ -117,6 +121,7 @@ How what to do
117
121
* [rswag for API Documentation](#rswag-for-api-documentation)
118
122
* [Codegen](#codegen)
119
123
* [Log For ELK stack (Elastic Search, Logstash, Kibana)](#log-for-elk-stack-elastic-search-logstash-kibana)
124
+ * [elk.yml config](#elkyml-config)
120
125
* [lograge.rb with custom config](#logragerb-with-custom-config)
121
126
* [ELK Setup](/rails_log_with_elk_setup.md)
122
127
* [Redis](#redis)
@@ -170,6 +175,100 @@ How what to do
170
175
# config/initializers/active_model_serializer.rb
171
176
ActiveModelSerializers.config.default_includes = '**'
172
177
```
178
+ 5. Pagination with serializer
179
+
180
+ [/app/helpers/category_helper.rb](/app/helpers/category_helper.rb)
181
+
182
+ ```ruby
183
+ # app/helpers/category_helper.rb
184
+ module CategoryHelper
185
+ def fetch_categories pagaination_param
186
+ page = pagaination_param[:category_page]
187
+ per = pagaination_param[:category_per]
188
+ key = "categories"+pagaination_param.to_s
189
+ categories = $redis.get(key)
190
+ if categories.nil?
191
+ @categories = Category.published.by_date.page(page).per(per)
192
+ categories = Pagination.build_json(@categories, pagaination_param).to_json
193
+ $redis.set(key, categories)
194
+ $redis.expire(key, 1.hour.to_i)
195
+ end
196
+ categories
197
+ end
198
+ def clear_cache
199
+ keys = $redis.keys "*categories*"
200
+ keys.each {|key| $redis.del key}
201
+ end
202
+ end
203
+
204
+ class CategoriesController < ApplicationController
205
+ include CategoryHelper
206
+ //... your code
207
+
208
+ # GET /categories
209
+ def index
210
+ page = params[:page].presence || 1
211
+ per = params[:per].presence || Pagination.per
212
+ pagaination_param = {
213
+ category_page: page,
214
+ category_per: per,
215
+ post_page: @post_page,
216
+ post_per: @post_per
217
+ }
218
+ @categories = fetch_categories pagaination_param
219
+ render json: @categories
220
+ end
221
+ ```
222
+ ```ruby
223
+ class Category < ApplicationRecord
224
+ include CategoryHelper
225
+ belongs_to :user
226
+ has_many :posts
227
+ scope :published, -> { where(published: true) }
228
+ scope :by_date, -> { order('created_at DESC, id DESC') }
229
+ validates :title, presence: true
230
+ validates :body, presence: true
231
+ after_save :clear_cache
232
+ end
233
+ ```
234
+ ```ruby
235
+ class CategorySerializer < ActiveModel::Serializer
236
+ attributes :id, :title, :body, :posts_pagination
237
+ has_one :user
238
+ has_many :posts
239
+ def posts
240
+ post_page = (instance_options.dig(:pagaination_param, :post_page).presence || 1).to_i
241
+ post_per = (instance_options.dig(:pagaination_param, :post_per).presence || 0).to_i
242
+ object.posts.published.by_date.page(post_page).per(post_per)
243
+ end
244
+ def posts_pagination
245
+ post_per = (instance_options.dig(:pagaination_param, :post_per).presence || Pagination.per).to_i
246
+ Pagination.build_json(posts)[:posts_pagination] if post_per > 0
247
+ end
248
+ end
249
+ ```
250
+ ```ruby
251
+ # /lib/pagination.rb
252
+ class Pagination
253
+ def self.information array
254
+ {
255
+ current_page: array.current_page,
256
+ next_page: array.next_page,
257
+ prev_page: array.prev_page,
258
+ total_pages: array.total_pages,
259
+ total_count: array.total_count
260
+ }
261
+ end
262
+ def self.build_json array, pagaination_param = {}
263
+ ob_name = array.name.downcase.pluralize.to_sym
264
+ json = Hash.new
265
+ json[ob_name] = ActiveModelSerializers::SerializableResource.new(array.to_a, pagaination_param: pagaination_param)
266
+ pagination_name = "#{ob_name}_pagination".to_sym
267
+ json[pagination_name] = self.information array
268
+ json
269
+ end
270
+ end
271
+ ```
173
272
174
273
### Nested Model
175
274
1. Comment Controller
@@ -327,20 +426,45 @@ How what to do
327
426

328
427
329
428
### Codegen
330
- > We developed server side code and We shoud need Client code. you can use Swagger-Codegen https://github.com/swagger-api/swagger-codegen#swagger-code-generator
331
- ` ` ` bash
332
- brew install swagger-codegen
333
- swagger-codegen generate -i http://localhost:3000/api-docs/v1/swagger.yaml -l swift5 -o ./swift
334
- ` ` `
429
+ > We developed server side code and We shoud need Client code. you can use Swagger-Codegen https://github.com/swagger-api/swagger-codegen#swagger-code-generator
430
+
431
+ ```bash
432
+ brew install swagger-codegen
433
+ swagger-codegen generate -i http://localhost:3000/api-docs/v1/swagger.yaml -l swift5 -o ./swift
434
+ ```
335
435
336
436
### Log For ELK stack (Elastic Search, Logstash, Kibana)
337
437
438
+ #### elk.yml config
439
+ > enable value is ```false``` or ```true``` <br/>
440
+ exmaple : ```enable: false```<br/>
441
+ [elk.yml](/config/elk.yml) <br/>
442
+
443
+ ```yml
444
+ # config/elk.yml
445
+
446
+ default: &default
447
+ enable: false
448
+ protocal: udp
449
+ host: localhost
450
+ port: 5000
451
+
452
+ development:
453
+ <<: *default
454
+
455
+ test:
456
+ <<: *default
457
+
458
+ production:
459
+ <<: *default
460
+ ```
461
+
338
462
#### lograge.rb with custom config
339
463
https://github.com/roidrage/lograge
340
464
https://ericlondon.com/2017/01/26/integrate-rails-logs-with-elasticsearch-logstash-kibana-in-docker-compose.html
341
465
[lograge.rb](/config/lograge.rb) <br/>
342
- [elk.yml](/config/elk.yml) < br/>
343
466
[application.rb](/config/application.rb) https://guides.rubyonrails.org/v4.2/configuring.html#custom-configuration
467
+
344
468
> override append_info_to_payload for lograge, append_info_to_payload method put parameter to payload[]
345
469
```ruby
346
470
class ApplicationController < ActionController::API
@@ -397,8 +521,8 @@ $redis = Redis::Namespace.new("tutorial_post", :redis => Redis.new(:host => '127
397
521
```ruby
398
522
# GET /categories
399
523
def index
400
- page = params[:page].present ? ? params[:page] : 1
401
- per = params[:per].present ? ? params[: per] : 10
524
+ page = params[:page].presence || 1
525
+ per = params[:per].presence || Pagination. per
402
526
pagaination_param = {
403
527
category_page: page,
404
528
category_per: per,
0 commit comments