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 +4 -4
- data/lib/sdoc_live/engine.rb +52 -26
- data/lib/sdoc_live/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c308a3b0c63dae1cd0325824aecadac55373be489c0371d7a00710129527533e
|
|
4
|
+
data.tar.gz: c69d278e885d057d746c433431333a10bd34e9595e9b881a99c8ebe38569914f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f82f91308407a7a993f5fd58bfaf26490e02ad7ee91edfa7d5065faea40411638c461011820efa61f1939bf671915f2de5297bf5fdad9868a0e7fa45ad56b0e9
|
|
7
|
+
data.tar.gz: 0c0930e0ab7c5196057648904952eadbd8674abd79a2972081b0d0ed698128e01b70c0a4a6ecb4b4490a2061bbdd3d059fe7a458c9c3d49af3cc5e08a5047f82
|
data/lib/sdoc_live/engine.rb
CHANGED
|
@@ -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
|
|
10
|
-
doc_root = (SdocLive.configuration.output_dir ||
|
|
11
|
-
|
|
12
|
-
SdocLive::
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
data/lib/sdoc_live/version.rb
CHANGED