file_sv 0.2.1 → 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: baf2b31a16ce8df2b4d5090be3fb176064c86930518b0c0879eee59fcd95216c
4
- data.tar.gz: 78b452772bc26da778f339ab205962ccfb9c8d993d3aab7aff3a31a18cf70103
3
+ metadata.gz: 85dad645586f82345f25060f4866f1ac3a6e520ae9ebe635c26f56e37511e3e7
4
+ data.tar.gz: 86386a9473897c0425781759eac4ceddf96a253ba661d0e387c2e4e21d9c21cb
5
5
  SHA512:
6
- metadata.gz: 930fd5e3ebf4ea16112f353805188367b476aa788bf147d576f6a2dbf8c616849ed951710b471004b006d162ebaf6897ca625fa4ad422c41cfdb7c822cc45698
7
- data.tar.gz: aac656a5c5a10c5fc3e56e86978da3b9b60e44a420ae84df7c4dfdc7a17b0005be772d84f5711fc1bb587da5c2dc2e6b77ce7a9b51bbf400f569bc4bf6266b00
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
@@ -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.1"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -10,6 +10,7 @@ class VirtualServer < Sinatra::Base
10
10
  set :server, :puma
11
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
@@ -52,7 +60,7 @@ for more details'
52
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 "#{request.request_method} #{request.fullpath}"
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
@@ -78,7 +86,7 @@ for more details'
78
86
  endpoint = endpoints.sample
79
87
  @params = params
80
88
  unless ENV["ignore_path"] && ("/#{ENV["ignore_path"]}" == endpoint_base.path)
81
- puts "#{request.request_method} #{request.fullpath}"
89
+ log({ msg: "Response", status: response.status, endpoint: endpoint.serving_file_name }) if request.fullpath != "/"
82
90
  end
83
91
  serve endpoint
84
92
  end
@@ -86,7 +94,6 @@ for more details'
86
94
  end
87
95
  # options endpoint for CORS
88
96
  options endpoint_path do
89
- puts "options: #{endpoint_path}"
90
97
  response["Allow"] = "*"
91
98
  response["Access-Control-Allow-Origin"] = "*"
92
99
  response["Access-Control-Allow-Methods"] = "*"
data/lib/file_sv.rb CHANGED
@@ -1,11 +1,14 @@
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"
9
12
 
10
13
  ENV["APP_ENV"] ||= "production"
11
14
 
@@ -17,6 +20,17 @@ module FileSv
17
20
  # Error related to incorrect format of filename
18
21
  class FileNameError < Error; end
19
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
+
20
34
  class << self
21
35
  # @return [Hash] Mapping of REST method names to setting classes
22
36
  def rest_methods
@@ -25,6 +39,12 @@ module FileSv
25
39
  delete: DeleteSettings, put: PutSettings
26
40
  }
27
41
  end
42
+
43
+ # @return [Logger] Logger
44
+ attr_accessor :logger
45
+
46
+ # @return Logger type
47
+ attr_accessor :log_type
28
48
  end
29
49
  end
30
50
 
@@ -33,17 +53,17 @@ CONFIG_FILE = "file_sv.yaml"
33
53
  # Set values in global settings based on config
34
54
  def load_default_config(file_path)
35
55
  unless File.exist? file_path
36
- 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"
37
57
  return
38
58
  end
39
- puts "Loading config from #{file_path}" if ENV["debug"] == "true"
59
+ FileSv.logger.debug "Loading config from #{file_path}" if ENV["debug"] == "true"
40
60
 
41
61
  config = YAML.load_file file_path
42
62
  return unless config # Handle empty YAML file
43
63
 
44
- puts "Config #{config}" if ENV["debug"] == "true"
64
+ FileSv.logger.debug "Config #{config}" if ENV["debug"] == "true"
45
65
  config["global"]&.each do |key, value|
46
- puts "Setting #{key} to #{value}" if ENV["debug"] == "true"
66
+ FileSv.logger.debug "Setting #{key} to #{value}" if ENV["debug"] == "true"
47
67
  GlobalSettings.send("#{key}=", value)
48
68
  end
49
69
 
@@ -62,7 +82,7 @@ def set_based_on_env_vars
62
82
  GlobalSettings.instance_variables.each do |setting|
63
83
  setting_name = setting.to_s[1..]
64
84
  if ENV[setting_name]
65
- puts "Setting #{setting_name} to #{ENV[setting_name]}"
85
+ FileSv.logger.debug "Setting #{setting_name} to #{ENV[setting_name]}"
66
86
  GlobalSettings.send("#{setting_name}=", ENV[setting_name])
67
87
  end
68
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.1
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-06-14 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