file_sv 0.2.0 → 0.3.0

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: 7177e3a064f21ddcd10c0e939d9557460e23a260d247cfbed515a94a5b9265a3
4
- data.tar.gz: e5cc7b549e0d6264a2a357a00de1d85ef18b7aafa757c621c0eeb647bc6db332
3
+ metadata.gz: 85dad645586f82345f25060f4866f1ac3a6e520ae9ebe635c26f56e37511e3e7
4
+ data.tar.gz: 86386a9473897c0425781759eac4ceddf96a253ba661d0e387c2e4e21d9c21cb
5
5
  SHA512:
6
- metadata.gz: 61d515119cc7b4d3cefca1f8231c2d85f51394518e448013b4457271f01dddffd370426a5f4077bd5c05f7a446085643524c2968a443b0a1fd8fd86a5b3a6276
7
- data.tar.gz: 96c064f198dbea63b6c5f251f4b2758856759f604cf8b3f3e31ccbb80fc895ee9d8861fe486e605d6a01feaab5f848a46d81501beadd1f1fee975d7bec9de146
6
+ metadata.gz: db63c4da96123bf37a186e49abb2f1e4f45f7721ee50031f08c5ca7fdb4c71ca57f32a2fda2d3059019dc03c99c080cea93722dac7ec0b98a2d5ce841e398d5a
7
+ data.tar.gz: c8efa0355463b2cb4f5cf09380035e08b9b2494ec324c5749bd7b65ec4f978f53c5351c1609ef37007bd8cf3b2ed88422dc1623d33028107a07cc24464684f00
@@ -26,7 +26,7 @@ class HarGenerator
26
26
  parsed = JSON.parse(self.content)
27
27
  pages = parsed['log']['pages']
28
28
  self.entries = parsed['log']['entries']
29
- puts "Found #{self.entries.count} entries in #{self.har_file_location}"
29
+ FileSv.logger.info "Found #{self.entries.count} entries in #{self.har_file_location}"
30
30
  end
31
31
 
32
32
  def generate
@@ -130,19 +130,6 @@ class PlannedEndpoint
130
130
  ERB.new(File.read(serving_file_name)).result(binding)
131
131
  end
132
132
 
133
- # Log when API is called
134
- def log_call(params)
135
- if params.keys.count > 0
136
- show_params = []
137
- params.each do |key, value|
138
- show_params << "#{key}=#{value}"
139
- end
140
- "#{self.method} #{self.path}?#{show_params.join('&')}"
141
- else
142
- "#{self.method} #{self.path}"
143
- end
144
- end
145
-
146
133
  def short_description
147
134
  desc = self.status_code.to_s
148
135
  desc += " (delay #{self.delay} sec)" if self.delay > 0
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # typed: false
4
+
5
+ # Helper methods added to RestServer
6
+ module ServerUtils
7
+ def log(messages)
8
+ if FileSv.log_type == :ougai
9
+ log_structured(messages)
10
+ else
11
+ message = messages.values.join(", ")
12
+ FileSv.logger.info "#{message}, #{request.request_method} #{request.fullpath}, CorrelationId: #{@request_id}"
13
+ end
14
+ end
15
+
16
+ private def log_structured(messages)
17
+ log_msg = {
18
+ method: request.request_method,
19
+ path: request.fullpath,
20
+ correlationId: @request_id
21
+ }
22
+ messages.each do |key, value|
23
+ log_msg[key] = value
24
+ end
25
+ FileSv.logger.info(log_msg)
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileSv
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -8,8 +8,9 @@ require "openssl"
8
8
  # Virtual server hosting virtual service defined through files
9
9
  class VirtualServer < Sinatra::Base
10
10
  set :server, :puma
11
- enable :logging if ENV['debug'] == "true"
11
+ enable :logging if ENV["debug"] == "true"
12
12
  set :bind, "0.0.0.0"
13
+ helpers ServerUtils
13
14
 
14
15
  register Sinatra::DocDsl
15
16
 
@@ -20,6 +21,13 @@ class VirtualServer < Sinatra::Base
20
21
  for more details'
21
22
  end
22
23
 
24
+ before do
25
+ @request_id = SecureRandom.uuid
26
+ headers["X-Correlation-Id"] = @request_id
27
+ headers["Access-Control-Allow-Origin"] = "*"
28
+ log({ msg: "Request" }) if request.fullpath != "/"
29
+ end
30
+
23
31
  get "/favicon.ico" do
24
32
  send_file File.join(__dir__, "file_sv.ico")
25
33
  end
@@ -33,26 +41,26 @@ for more details'
33
41
  end
34
42
 
35
43
  def append_content_type(endpoint)
36
- if (endpoint.file_path.end_with? '.json')
44
+ if endpoint.file_path.end_with? ".json"
37
45
  content_type :json
38
- elsif endpoint.file_path.end_with? '.xml'
46
+ elsif endpoint.file_path.end_with? ".xml"
39
47
  content_type :xml
40
- elsif endpoint.file_path.end_with? '.css'
48
+ elsif endpoint.file_path.end_with? ".css"
41
49
  content_type :css
42
- elsif endpoint.file_path.end_with? '.js'
43
- content_type :js
50
+ elsif endpoint.file_path.end_with? ".js"
51
+ content_type :js
44
52
  end
45
53
  end
46
54
 
47
55
  # Log endpoint. Return content and status code defined by endpoint
48
56
  # @param [PlannedEndpoint] endpoint Planned endpoint to serve
49
- def serve(endpoint, id = nil)
57
+ def serve(endpoint, id = nil)
50
58
  @id = id
51
59
  append_content_type(endpoint)
52
- if ENV['debug'] == "true"
60
+ if ENV["debug"] == "true"
53
61
  message = "Using endpoint based on file #{endpoint.serving_file_name}."
54
62
  message += " Using param '#{@id}'" if id
55
- puts message
63
+ FileSv.logger.debug message
56
64
  end
57
65
  sleep endpoint.delay if endpoint.delay > 0
58
66
  [endpoint.status_code, output_for(endpoint, binding)]
@@ -69,7 +77,7 @@ for more details'
69
77
  response["Access-Control-Allow-Origin"] = "*"
70
78
  endpoint = endpoints.sample
71
79
  @params = params
72
- puts endpoint.log_call(@params)
80
+ log({ msg: "Response", status: response.status, endpoint: endpoint.serving_file_name }) if request.fullpath != "/"
73
81
  serve endpoint, id
74
82
  end
75
83
  else
@@ -77,14 +85,15 @@ for more details'
77
85
  response["Access-Control-Allow-Origin"] = "*"
78
86
  endpoint = endpoints.sample
79
87
  @params = params
80
- puts endpoint.log_call(@params) unless ENV['ignore_path'] && ("/#{ENV['ignore_path']}" == endpoint_base.path)
88
+ unless ENV["ignore_path"] && ("/#{ENV["ignore_path"]}" == endpoint_base.path)
89
+ log({ msg: "Response", status: response.status, endpoint: endpoint.serving_file_name }) if request.fullpath != "/"
90
+ end
81
91
  serve endpoint
82
92
  end
83
93
  end
84
94
  end
85
95
  # options endpoint for CORS
86
96
  options endpoint_path do
87
- puts "options: #{endpoint_path}"
88
97
  response["Allow"] = "*"
89
98
  response["Access-Control-Allow-Origin"] = "*"
90
99
  response["Access-Control-Allow-Methods"] = "*"
data/lib/file_sv.rb CHANGED
@@ -1,11 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "yaml"
4
+ require "logger"
5
+ require "ougai"
4
6
  require_relative "file_sv/version"
5
7
  require_relative "file_sv/global_settings"
6
8
  require_relative "file_sv/sv_plan"
7
9
  require_relative "file_sv/service_loader"
8
10
  require_relative "file_sv/planned_endpoint"
11
+ require_relative "file_sv/server_utils"
12
+
13
+ ENV["APP_ENV"] ||= "production"
9
14
 
10
15
  # Create Service Virtualization from a simple file system
11
16
  module FileSv
@@ -15,6 +20,17 @@ module FileSv
15
20
  # Error related to incorrect format of filename
16
21
  class FileNameError < Error; end
17
22
 
23
+ if ENV["Log"] == "structured"
24
+ @logger = Ougai::Logger.new($stdout)
25
+ @log_type = :ougai
26
+ else
27
+ @logger = Logger.new($stdout)
28
+ @log_type = :basic
29
+ end
30
+ LOGLEVELS = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN].freeze
31
+ log_level ||= LOGLEVELS.index ENV.fetch("LOG_LEVEL","INFO")
32
+ @logger.level = log_level
33
+
18
34
  class << self
19
35
  # @return [Hash] Mapping of REST method names to setting classes
20
36
  def rest_methods
@@ -23,6 +39,12 @@ module FileSv
23
39
  delete: DeleteSettings, put: PutSettings
24
40
  }
25
41
  end
42
+
43
+ # @return [Logger] Logger
44
+ attr_accessor :logger
45
+
46
+ # @return Logger type
47
+ attr_accessor :log_type
26
48
  end
27
49
  end
28
50
 
@@ -31,17 +53,17 @@ CONFIG_FILE = "file_sv.yaml"
31
53
  # Set values in global settings based on config
32
54
  def load_default_config(file_path)
33
55
  unless File.exist? file_path
34
- puts "No config found at #{file_path}" if ENV['debug'] == "true"
56
+ FileSv.logger.debug "No config found at #{file_path}" if ENV["debug"] == "true"
35
57
  return
36
58
  end
37
- puts "Loading config from #{file_path}" if ENV['debug'] == "true"
59
+ FileSv.logger.debug "Loading config from #{file_path}" if ENV["debug"] == "true"
38
60
 
39
61
  config = YAML.load_file file_path
40
62
  return unless config # Handle empty YAML file
41
63
 
42
- puts "Config #{config}" if ENV['debug'] == "true"
64
+ FileSv.logger.debug "Config #{config}" if ENV["debug"] == "true"
43
65
  config["global"]&.each do |key, value|
44
- puts "Setting #{key} to #{value}" if ENV['debug'] == "true"
66
+ FileSv.logger.debug "Setting #{key} to #{value}" if ENV["debug"] == "true"
45
67
  GlobalSettings.send("#{key}=", value)
46
68
  end
47
69
 
@@ -60,7 +82,7 @@ def set_based_on_env_vars
60
82
  GlobalSettings.instance_variables.each do |setting|
61
83
  setting_name = setting.to_s[1..]
62
84
  if ENV[setting_name]
63
- puts "Setting #{setting_name} to #{ENV[setting_name]}"
85
+ FileSv.logger.debug "Setting #{setting_name} to #{ENV[setting_name]}"
64
86
  GlobalSettings.send("#{setting_name}=", ENV[setting_name])
65
87
  end
66
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_sv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-05-20 00:00:00.000000000 Z
11
+ date: 2024-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -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: rackup
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -112,6 +126,7 @@ files:
112
126
  - lib/file_sv/har_generator.rb
113
127
  - lib/file_sv/planned_endpoint.rb
114
128
  - lib/file_sv/render_file.rb
129
+ - lib/file_sv/server_utils.rb
115
130
  - lib/file_sv/service_loader.rb
116
131
  - lib/file_sv/sv_plan.rb
117
132
  - lib/file_sv/version.rb