schemaless_rest_api 0.4.0 → 0.4.1

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: c80f60796bfe046a8af8d4e4068ae847379ab7a6a2f78b03e06732f2b6b1d8a6
4
- data.tar.gz: 1473a6597e940ad4949b306d6cb72b5b49badda3ec3b16b2e118407eaefaa45d
3
+ metadata.gz: 3ab21f348c0122fbfa54365f8f1ed20c80bcf2f110e522bf8e782315414419e9
4
+ data.tar.gz: a5ac30ed8f1efe06561474fed073f9e270788b74502e85dfcf8e61b11131a21d
5
5
  SHA512:
6
- metadata.gz: b9d2068d71bef0434faa40fe237864fcb6fbebedbb0109013d995055a26975ca4109817215840b2cc22b178efdb3cdc1924e19d1450fcdda251f2ef5aad89e6a
7
- data.tar.gz: ad9734ec4b544a6a686e791cacbb45cd70234e071f6af8d6d2ced82624e401d03a7d77ba33001a2cd0529a0ace33205c28eed296b853a8e728ed396c2ff0a7b1
6
+ metadata.gz: a63738895a1ae2c01ecf3a0d81d70978231f3c92493a13f9a72cb111d1b41a6f90fde28f9dddd2de662c9fe7d3e270bdd1f7462870c02516e1a042957f950169
7
+ data.tar.gz: aa2e4411f2aace139b6b9d40edb672e32513d6dcc07a4e6359b2338191dd3bf583f433d9d239f3d7263fde38eb169990290d73e31aa2d2df51b9e7074f55480e
@@ -3,6 +3,7 @@
3
3
  require "sinatra"
4
4
  require "puma"
5
5
  require "route_downcaser"
6
+ require_relative "server_utils"
6
7
 
7
8
  # Server with endpoints generated based on Entities with CRUD operations for them
8
9
  class RestServer < Sinatra::Base
@@ -10,12 +11,23 @@ class RestServer < Sinatra::Base
10
11
  enable :logging if ENV["debug"] == "true"
11
12
  set :bind, "0.0.0.0"
12
13
  use RouteDowncaser::DowncaseRouteMiddleware
14
+ helpers ServerUtils
15
+
16
+ before do
17
+ @request_id = SecureRandom.uuid
18
+ headers["X-Request-Id"] = @request_id
19
+ headers["Access-Control-Allow-Origin"] = "*"
20
+ log({ msg: "Request"}) if (request.fullpath != "")
21
+ end
22
+
23
+ after do
24
+ log({ msg: "Response", status: response.status }) if (request.fullpath != "")
25
+ end
13
26
 
14
27
  SWAGGER_FILES = %w[index.css swagger.html swagger-initializer.js swagger-ui-bundle.js swagger-ui-standalone-preset.js swagger-ui.css]
15
28
 
16
29
  SWAGGER_FILES.each do |filename|
17
30
  get "/#{filename.gsub(".html", "")}" do
18
- response["Access-Control-Allow-Origin"] = "*"
19
31
  if filename.end_with? '.json'
20
32
  content_type :json
21
33
  elsif filename.end_with? '.css'
@@ -29,7 +41,6 @@ class RestServer < Sinatra::Base
29
41
  end
30
42
 
31
43
  get "/swagger.json" do
32
- response["Access-Control-Allow-Origin"] = "*"
33
44
  content_type :json
34
45
  SwaggerBuilder.build_swagger_for(Entities.models, request.host_with_port)
35
46
  end
@@ -53,11 +64,6 @@ class RestServer < Sinatra::Base
53
64
 
54
65
  Entities.models.each_key do |model|
55
66
  post "/#{model.downcase}" do
56
- response["Access-Control-Allow-Origin"] = "*"
57
- request_id = SecureRandom.uuid
58
- headers["X-Request-Id"] = request_id
59
- SchemalessRestApi.logger.info env
60
- SchemalessRestApi.logger.info "POST #{request.fullpath}, CorrelationId: #{request_id}"
61
67
  request_body = request.body.read
62
68
  data = {}
63
69
  data = JSON.parse(request_body) unless request_body.empty?
@@ -73,20 +79,17 @@ class RestServer < Sinatra::Base
73
79
  else
74
80
  Entities.models[model][id] = data
75
81
  end
76
- SchemalessRestApi.logger.info "Created #{id}, CorrelationId: #{request_id}"
82
+ log "Created #{id}"
77
83
  [201, JSON.generate({ id: id })]
78
84
  end
79
85
 
80
86
  options "/#{model.downcase}" do
81
87
  response["Allow"] = "*"
82
- response["Access-Control-Allow-Origin"] = "*"
83
88
  response["Access-Control-Allow-Methods"] = "*"
84
89
  response["Access-Control-Allow-Headers"] = "*"
85
90
  end
86
91
 
87
92
  get "/#{model.downcase}" do
88
- response["Access-Control-Allow-Origin"] = "*"
89
- SchemalessRestApi.logger.info "GET #{request.fullpath}"
90
93
  if ENV["mongodb"]
91
94
  if params == {}
92
95
  JSON.generate(MongoClient.get_all(model))
@@ -106,8 +109,6 @@ class RestServer < Sinatra::Base
106
109
  end
107
110
 
108
111
  get "/#{model.downcase}/:id" do |id|
109
- response["Access-Control-Allow-Origin"] = "*"
110
- SchemalessRestApi.logger.info "GET #{request.fullpath}"
111
112
  if ENV["mongodb"]
112
113
  results = MongoClient.find(model, id)
113
114
  return not_have(id) unless results.first
@@ -128,8 +129,6 @@ class RestServer < Sinatra::Base
128
129
  end
129
130
 
130
131
  put "/#{model.downcase}/:id" do |id|
131
- response["Access-Control-Allow-Origin"] = "*"
132
- SchemalessRestApi.logger.info "PUT #{request.fullpath}"
133
132
  data = JSON.parse(request.body.read)
134
133
  if ENV["mongodb"]
135
134
  results = MongoClient.find(model, id)
@@ -145,8 +144,6 @@ class RestServer < Sinatra::Base
145
144
  end
146
145
 
147
146
  delete "/#{model.downcase}/:id" do |id|
148
- response["Access-Control-Allow-Origin"] = "*"
149
- SchemalessRestApi.logger.info "DEL #{request.fullpath}"
150
147
  if ENV["mongodb"]
151
148
  results = MongoClient.find(model, id)
152
149
  return not_have(id) unless results.first
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # typed: true
4
+
5
+ module ServerUtils
6
+ def log(messages)
7
+ if SchemalessRestApi.log_type == :ougai
8
+ log_msg = {
9
+ method: request.request_method,
10
+ path: request.fullpath,
11
+ correlationId: @request_id
12
+ }
13
+ messages.each do |key, value|
14
+ log_msg[key] = value
15
+ end
16
+ SchemalessRestApi.logger.info(log_msg)
17
+ else
18
+ message = messages.values.join(", ")
19
+ SchemalessRestApi.logger.info "#{message}, #{request.request_method} #{request.fullpath}, CorrelationId: #{@request_id}"
20
+ end
21
+ end
22
+ end
@@ -3,5 +3,5 @@
3
3
  # typed: true
4
4
 
5
5
  module SchemalessRestApi
6
- VERSION = "0.4.0"
6
+ VERSION = "0.4.1"
7
7
  end
@@ -7,16 +7,26 @@ require_relative "schemaless_rest_api/entities"
7
7
  require_relative "schemaless_rest_api/swagger_builder"
8
8
  require "tapioca"
9
9
  require "logger"
10
-
10
+ require "ougai"
11
11
  require "json"
12
12
  require "securerandom"
13
13
 
14
14
  # Global params for Schemalass REST API
15
15
  module SchemalessRestApi
16
- @logger = Logger.new($stdout)
16
+ if ENV["Log"] == 'structured'
17
+ @logger = Ougai::Logger.new($stdout)
18
+ @log_type = :ougai
19
+ else
20
+ @logger = Logger.new($stdout)
21
+ @log_type = :basic
22
+ end
23
+
17
24
  class << self
18
25
  # @return Logger
19
26
  attr_accessor :logger
27
+
28
+ # @return Logger type
29
+ attr_accessor :log_type
20
30
  end
21
31
 
22
32
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemaless_rest_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-10 00:00:00.000000000 Z
11
+ date: 2024-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ougai
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: puma
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +123,7 @@ files:
109
123
  - lib/schemaless_rest_api/entities.rb
110
124
  - lib/schemaless_rest_api/mongo_client.rb
111
125
  - lib/schemaless_rest_api/rest_server.rb
126
+ - lib/schemaless_rest_api/server_utils.rb
112
127
  - lib/schemaless_rest_api/swagger/index.css
113
128
  - lib/schemaless_rest_api/swagger/swagger-initializer.js
114
129
  - lib/schemaless_rest_api/swagger/swagger-ui-bundle.js