schemaless_rest_api 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cd0875a0972ff3b33119a209a1e0a4a338e89cc52949c3014f12e9193315fbc
4
- data.tar.gz: 5a15b247ae28b9ec1329637dac2649bc6104425d4f576a77d0ca4725b3a1ae0d
3
+ metadata.gz: 6f42e7bccaab28e6dc750c4351e18500689d1fb13a72bda6d0f1417cc5cef630
4
+ data.tar.gz: d47a29830a56c986c04bdc92c0c07758efcbe197de71a0183552af628ee66dc0
5
5
  SHA512:
6
- metadata.gz: da922d8ce69b812d26fa7f6887e604190a22c4f9924a9fb2d33a93c5de4a52aca09c92c0b172674d51970704abb09fec1e26851d8254009ba96f56df4e8966be
7
- data.tar.gz: 3aeb83be262190ef8273dc1fcf521b11ee637f6ccbb7e389b7e379be528fc3a593cdb31497652016225484b222af2a4e1cd539ab2d301af798c7893061041baa
6
+ metadata.gz: 366cd2cf898fc3cca3ba5b90d29d44fa08e43b5a0a8154eec309c21d9a7012a8c2498912818f2251e664bf2568bfdbcead72ac59e6d837f6a844fd43b64b2a07
7
+ data.tar.gz: 65cfacda5630298c9e50165b3a2a5420ecf6965347f32f92226afcb6137716207732377714d9b59d7bdda228e5aacc6f23adb39a97ea0be062bc269804b61568
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # typed: true
4
+
3
5
  # Entities mapped by environment variables
4
6
  class Entities
5
7
  @models = {}
6
8
  class << self
9
+ # @return [Hash] Hash of models
7
10
  attr_accessor :models
8
11
  end
9
12
  end
@@ -2,6 +2,7 @@ require "mongo"
2
2
 
3
3
  # typed: false - MongoClient new
4
4
 
5
+ # Client to talk to Mongo database
5
6
  module MongoClient
6
7
  class << self
7
8
  # @return Client to work with MongoDb
Binary file
@@ -3,6 +3,8 @@
3
3
  require "sinatra"
4
4
  require "puma"
5
5
  require "route_downcaser"
6
+ require "prometheus/middleware/collector"
7
+ require "prometheus/middleware/exporter"
6
8
  require_relative "server_utils"
7
9
 
8
10
  # Server with endpoints generated based on Entities with CRUD operations for them
@@ -11,32 +13,39 @@ class RestServer < Sinatra::Base
11
13
  enable :logging if ENV["debug"] == "true"
12
14
  set :bind, "0.0.0.0"
13
15
  use RouteDowncaser::DowncaseRouteMiddleware
16
+ use Prometheus::Middleware::Collector
17
+ use Prometheus::Middleware::Exporter
14
18
  helpers ServerUtils
15
19
 
16
20
  before do
17
- @request_id = SecureRandom.uuid
18
- headers["X-Request-Id"] = @request_id
21
+ @request_id = SecureRandom.uuid
22
+ headers["X-Correlation-Id"] = @request_id
19
23
  headers["Access-Control-Allow-Origin"] = "*"
20
- log({ msg: "Request"}) if (request.fullpath != "")
24
+ log({ msg: "Request" }) if request.fullpath != ""
21
25
  end
22
26
 
23
- after do
24
- log({ msg: "Response", status: response.status }) if (request.fullpath != "")
27
+ after do
28
+ log({ msg: "Response", status: response.status }) if request.fullpath != ""
25
29
  end
26
30
 
27
- SWAGGER_FILES = %w[index.css swagger.html swagger-initializer.js swagger-ui-bundle.js swagger-ui-standalone-preset.js swagger-ui.css]
31
+ get "/favicon.ico" do
32
+ send_file File.join(__dir__, "rest.ico")
33
+ end
34
+
35
+ SWAGGER_FILES = %w[index.css swagger.html swagger-initializer.js swagger-ui-bundle.js swagger-ui-standalone-preset.js
36
+ swagger-ui.css]
28
37
 
29
38
  SWAGGER_FILES.each do |filename|
30
39
  get "/#{filename.gsub(".html", "")}" do
31
- if filename.end_with? '.json'
40
+ if filename.end_with? ".json"
32
41
  content_type :json
33
- elsif filename.end_with? '.css'
42
+ elsif filename.end_with? ".css"
34
43
  content_type :css
35
- elsif filename.end_with? '.js'
44
+ elsif filename.end_with? ".js"
36
45
  content_type :js
37
46
  end
38
47
 
39
- File.read("./lib/schemaless_rest_api/swagger/#{filename}")
48
+ File.read(File.join(__dir__, "swagger", filename))
40
49
  end
41
50
  end
42
51
 
@@ -55,7 +64,7 @@ class RestServer < Sinatra::Base
55
64
 
56
65
  get "/" do
57
66
  summary = { models: Entities.models.keys.to_s,
58
- docs_url: "<a href=/swagger>Swagger docs</a>" }
67
+ docs_url: "<a href='#{ENV["base_path"]}/swagger'>Swagger docs</a>" }
59
68
  summary[:db] = MongoClient.client.summary.to_s if ENV["mongodb"]
60
69
  JSON.generate(summary)
61
70
  rescue StandardError => e
@@ -79,7 +88,7 @@ class RestServer < Sinatra::Base
79
88
  else
80
89
  Entities.models[model][id] = data
81
90
  end
82
- log({msg: "Created #{id}"})
91
+ log({ msg: "Created #{id}" })
83
92
  [201, JSON.generate({ id: id })]
84
93
  end
85
94
 
@@ -13,7 +13,7 @@ class SwaggerBuilder
13
13
  doc["info"] = build_info(models)
14
14
  doc["host"] = host_with_port
15
15
  doc["schemes"] = "http"
16
- doc["basePath"] = "/"
16
+ doc["basePath"] = "/#{ENV["base_path"]}"
17
17
  doc["paths"] = generate_paths_for models
18
18
  JSON.generate(doc)
19
19
  end
@@ -57,7 +57,7 @@ class SwaggerBuilder
57
57
  }
58
58
  },
59
59
  get: {
60
- parameters: [
60
+ parameters: [
61
61
  {
62
62
  name: "params",
63
63
  description: "Any key can be used to filter #{model} by",
@@ -94,7 +94,7 @@ class SwaggerBuilder
94
94
  end
95
95
 
96
96
  def paths_at_id(model)
97
- {
97
+ {
98
98
  get: {
99
99
  description: "Data of #{model} at passed id",
100
100
  parameters: id_params(model),
@@ -3,5 +3,5 @@
3
3
  # typed: true
4
4
 
5
5
  module SchemalessRestApi
6
- VERSION = "0.5.0"
6
+ VERSION = "0.6.0"
7
7
  end
@@ -12,6 +12,7 @@ require "json"
12
12
  require "securerandom"
13
13
 
14
14
  ENV["APP_ENV"] ||= "production"
15
+ ENV["base_path"] ||= ""
15
16
 
16
17
  # Global params for Schemalass REST API
17
18
  module SchemalessRestApi
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.5.0
4
+ version: 0.6.0
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-14 00:00:00.000000000 Z
11
+ date: 2024-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: prometheus-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: puma
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +136,7 @@ files:
122
136
  - lib/schemaless_rest_api.rb
123
137
  - lib/schemaless_rest_api/entities.rb
124
138
  - lib/schemaless_rest_api/mongo_client.rb
139
+ - lib/schemaless_rest_api/rest.ico
125
140
  - lib/schemaless_rest_api/rest_server.rb
126
141
  - lib/schemaless_rest_api/server_utils.rb
127
142
  - lib/schemaless_rest_api/swagger/index.css