sdoc_live 0.1.8 → 0.1.9

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: 2c58eaf82f18b4feb7c431156e8ba8be018e22eb57b8b62d69e0e0c503e2085c
4
- data.tar.gz: 372965feda50bdc4bc08b196d3acbbc43af6fe2b7fb4461b622bbb4f80c0befa
3
+ metadata.gz: c308a3b0c63dae1cd0325824aecadac55373be489c0371d7a00710129527533e
4
+ data.tar.gz: c69d278e885d057d746c433431333a10bd34e9595e9b881a99c8ebe38569914f
5
5
  SHA512:
6
- metadata.gz: 224cc2e187603628bb1e76664ef87ff8f6c33640d34512d38e7f262b1e96490a3e05dc07ed2cc08f78e372adf8a016bae6c55199f2a9de3c57ebbcc1ea2e5bfb
7
- data.tar.gz: dc1222d122c5992e1e9505e752305a28b164b0d55dff3bbdc3f3b19aae4d630c76d6940be15fd45201426e1c548172bea7722b61cf6f3f95eb4ffec2ef02c2f2
6
+ metadata.gz: f82f91308407a7a993f5fd58bfaf26490e02ad7ee91edfa7d5065faea40411638c461011820efa61f1939bf671915f2de5297bf5fdad9868a0e7fa45ad56b0e9
7
+ data.tar.gz: 0c0930e0ab7c5196057648904952eadbd8674abd79a2972081b0d0ed698128e01b70c0a4a6ecb4b4490a2061bbdd3d059fe7a458c9c3d49af3cc5e08a5047f82
@@ -6,47 +6,63 @@ module SdocLive
6
6
  load File.expand_path("../tasks/sdoc_live.rake", __dir__)
7
7
  end
8
8
 
9
- initializer "sdoc_live.static" do |app|
10
- doc_root = (SdocLive.configuration.output_dir || app.root.join("tmp", "doc")).to_s
11
-
12
- SdocLive::Engine.routes.draw do
13
- doc_app = ::Rack::Static.new(
14
- ->(_) { [404, { "content-type" => "text/plain" }, ["Not Found"]] },
15
- urls: ["/"],
16
- root: doc_root,
17
- index: "index.html"
18
- )
19
-
20
- mount doc_app, at: "/"
21
- end
9
+ initializer "sdoc_live.static" do
10
+ doc_root = (SdocLive.configuration.output_dir || Rails.root.join("tmp", "doc")).to_s
11
+
12
+ config.middleware.use(SdocLive::StaticFiles, doc_root)
22
13
  end
23
14
 
24
- initializer "sdoc_live.trailing_slash", after: :add_routing_paths do |app|
25
- mount_path = nil
15
+ end
26
16
 
27
- app.routes.routes.each do |route|
28
- if route.app.respond_to?(:app) && route.app.app == SdocLive::Engine
29
- mount_path = route.path.spec.to_s.gsub("(.:format)", "").chomp("/")
30
- break
31
- end
17
+ class StaticFiles
18
+
19
+ def initialize(app, doc_root)
20
+ @app = app
21
+ @doc_root = doc_root
22
+ end
23
+
24
+ def call(env)
25
+ mount_path = find_mount_path(env)
26
+ return @app.call(env) unless mount_path
27
+
28
+ path_info = env["PATH_INFO"]
29
+
30
+ if path_info == mount_path
31
+ query = env["QUERY_STRING"].to_s.empty? ? "" : "?#{ env['QUERY_STRING'] }"
32
+ return [301, { "location" => "#{ mount_path }/#{ query }" }, []]
32
33
  end
33
34
 
34
- if mount_path && !mount_path.empty?
35
- app.middleware.use(Class.new do
36
- define_method(:initialize) { |a| @app = a }
37
-
38
- define_method(:call) do |env|
39
- if env["PATH_INFO"] == mount_path && !env["PATH_INFO"].end_with?("/")
40
- query = env["QUERY_STRING"].to_s.empty? ? "" : "?#{ env['QUERY_STRING'] }"
41
- [301, { "location" => "#{ mount_path }/#{ query }", "content-type" => "text/html" }, ["Redirecting..."]]
42
- else
43
- @app.call(env)
44
- end
45
- end
46
- end)
35
+ return @app.call(env) unless path_info.start_with?("#{ mount_path }/")
36
+
37
+ relative_path = path_info.delete_prefix(mount_path)
38
+ relative_path = "/index.html" if relative_path == "/"
39
+
40
+ file_path = File.join(@doc_root, relative_path)
41
+ file_path = File.join(file_path, "index.html") if File.directory?(file_path)
42
+
43
+ if File.file?(file_path)
44
+ serve_file(file_path)
45
+ else
46
+ @app.call(env)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def find_mount_path(env)
53
+ @mount_path ||= begin
54
+ routes = Rails.application.routes.routes
55
+ route = routes.detect { |r| r.app.respond_to?(:app) && r.app.app == SdocLive::Engine }
56
+ route&.path&.spec&.to_s&.gsub("(.:format)", "")&.chomp("/")
47
57
  end
48
58
  end
49
59
 
60
+ def serve_file(path)
61
+ content_type = Rack::Mime.mime_type(File.extname(path), "application/octet-stream")
62
+ body = File.read(path, mode: "rb")
63
+ [200, { "content-type" => content_type, "content-length" => body.bytesize.to_s }, [body]]
64
+ end
65
+
50
66
  end
51
67
 
52
68
  end
@@ -1,5 +1,5 @@
1
1
  module SdocLive
2
2
 
3
- VERSION = "0.1.8"
3
+ VERSION = "0.1.9"
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdoc_live
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - 16554289+optimuspwnius@users.noreply.github.com