sdoc_live 0.1.7 → 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: 1653429aed4ce0456d7df0c26da4d8ccdde41967cfd91ab2824c24c5da7dc975
4
- data.tar.gz: a22e7b188e50c419cf7160dbc2c2b32160320ca46015306cf19e08efde37dc11
3
+ metadata.gz: c308a3b0c63dae1cd0325824aecadac55373be489c0371d7a00710129527533e
4
+ data.tar.gz: c69d278e885d057d746c433431333a10bd34e9595e9b881a99c8ebe38569914f
5
5
  SHA512:
6
- metadata.gz: 4c592cd42c497602bbb49b90e9f1603c407139e8c00e0f093f26bfc412a5c160b1de729ce70c4db14a5efbe89967e51eceda9bca695273f7e252c83e6dfd639a
7
- data.tar.gz: 87c2c8adc1768ed43cba4aad88b425a16279920893ab5e326c63555e5653c1bce416980d868eb1e17d52162277ce80bc009466df465e38743ba266f8c081df79
6
+ metadata.gz: f82f91308407a7a993f5fd58bfaf26490e02ad7ee91edfa7d5065faea40411638c461011820efa61f1939bf671915f2de5297bf5fdad9868a0e7fa45ad56b0e9
7
+ data.tar.gz: 0c0930e0ab7c5196057648904952eadbd8674abd79a2972081b0d0ed698128e01b70c0a4a6ecb4b4490a2061bbdd3d059fe7a458c9c3d49af3cc5e08a5047f82
@@ -6,35 +6,61 @@ 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: "/"
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)
13
+ end
14
+
15
+ end
16
+
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 }" }, []]
33
+ end
34
+
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("/")
21
57
  end
22
58
  end
23
59
 
24
- initializer "sdoc_live.trailing_slash" do
25
- SdocLive::Engine.middleware.use(Class.new do
26
- def initialize(app)
27
- @app = app
28
- end
29
-
30
- def call(env)
31
- if env["PATH_INFO"] == ""
32
- [301, { "location" => "#{ env['SCRIPT_NAME'] }/", "content-type" => "text/html" }, ["Redirecting..."]]
33
- else
34
- @app.call(env)
35
- end
36
- end
37
- end)
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]]
38
64
  end
39
65
 
40
66
  end
@@ -1,5 +1,5 @@
1
1
  module SdocLive
2
2
 
3
- VERSION = "0.1.7"
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.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - 16554289+optimuspwnius@users.noreply.github.com