wurk 1.16.0 → 1.17.0
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/README.md +3 -1
- data/app/controllers/concerns/wurk/same_origin_guard.rb +8 -39
- data/app/controllers/concerns/wurk/sse_streaming.rb +3 -38
- data/app/controllers/wurk/api_controller.rb +8 -642
- data/app/controllers/wurk/dashboard_controller.rb +5 -156
- data/app/controllers/wurk/extensions_controller.rb +5 -43
- data/app/controllers/wurk/profiles_controller.rb +4 -117
- data/lib/wurk/api/serializers.rb +2 -2
- data/lib/wurk/engine.rb +3 -111
- data/lib/wurk/version.rb +1 -1
- data/lib/wurk/web/asset_mount.rb +119 -0
- data/lib/wurk/web/dashboard/api_actions.rb +657 -0
- data/lib/wurk/web/dashboard/controller.rb +146 -0
- data/lib/wurk/web/dashboard/endpoints.rb +92 -0
- data/lib/wurk/web/dashboard/extension_actions.rb +53 -0
- data/lib/wurk/web/dashboard/profile_actions.rb +130 -0
- data/lib/wurk/web/dashboard/routes.rb +134 -0
- data/lib/wurk/web/dashboard/shell.rb +181 -0
- data/lib/wurk/web/dashboard/stats_tick.rb +49 -0
- data/lib/wurk/web/dashboard/stream_concurrency_guard.rb +97 -0
- data/lib/wurk/web/rack_app.rb +27 -29
- data/lib/wurk/web/same_origin.rb +43 -0
- data/vendor/assets/dashboard/wurk-manifest.json +2 -2
- metadata +14 -4
- data/app/controllers/concerns/wurk/stream_concurrency_guard.rb +0 -94
- /data/{app/controllers/wurk/api → lib/wurk/web/dashboard}/pagination.rb +0 -0
- /data/{app/controllers/wurk/api → lib/wurk/web/dashboard}/serializers.rb +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'rack'
|
|
5
|
+
|
|
6
|
+
module Wurk
|
|
7
|
+
class Web
|
|
8
|
+
module Dashboard
|
|
9
|
+
# The slice of the ActionController interface the dashboard's shared
|
|
10
|
+
# actions use (Dashboard::ApiActions, ProfileActions, ExtensionActions),
|
|
11
|
+
# over plain Rack, so `run Sidekiq::Web` serves the same dashboard as the
|
|
12
|
+
# engine without Rails (CLAUDE.md: standalone mode never loads the
|
|
13
|
+
# engine). The engine's controllers include the same action modules and
|
|
14
|
+
# get these methods from ActionController instead.
|
|
15
|
+
class Controller
|
|
16
|
+
# `params[:name]` and `params['name']` alike, as ActionController's.
|
|
17
|
+
class Params < ::Hash
|
|
18
|
+
def [](key) = super(key.to_s)
|
|
19
|
+
def key?(key) = super(key.to_s)
|
|
20
|
+
def fetch(key, *, &) = super(key.to_s, *, &)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The request readers the actions call, under ActionDispatch's names.
|
|
24
|
+
class Request < ::Rack::Request
|
|
25
|
+
def headers = Headers.new(env)
|
|
26
|
+
def get_header(name) = env[name]
|
|
27
|
+
def content_security_policy_nonce = nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# `request.headers['Sec-Fetch-Site']` reads `HTTP_SEC_FETCH_SITE`.
|
|
31
|
+
Headers = Struct.new(:env) do
|
|
32
|
+
def [](name) = env["HTTP_#{name.upcase.tr('-', '_')}"]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
Response = Struct.new(:headers)
|
|
36
|
+
|
|
37
|
+
JSON_TYPE = 'application/json; charset=utf-8'
|
|
38
|
+
|
|
39
|
+
class << self
|
|
40
|
+
def before_action(name)
|
|
41
|
+
before_actions << name
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def before_actions
|
|
45
|
+
@before_actions ||= superclass.respond_to?(:before_actions) ? superclass.before_actions.dup : []
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# A Rack triple for `action`, with `path_params` from the route.
|
|
49
|
+
def call(env, action, path_params = {})
|
|
50
|
+
new(env, path_params).dispatch(action)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
attr_reader :request, :response
|
|
55
|
+
|
|
56
|
+
def initialize(env, path_params)
|
|
57
|
+
@request = Request.new(env)
|
|
58
|
+
@response = Response.new({})
|
|
59
|
+
@path_params = path_params
|
|
60
|
+
@result = nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def params
|
|
64
|
+
@params ||= Params.new.merge!(request_params.transform_keys(&:to_s))
|
|
65
|
+
.merge!(@path_params.transform_keys(&:to_s))
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def dispatch(action)
|
|
69
|
+
self.class.before_actions.each do |name|
|
|
70
|
+
send(name)
|
|
71
|
+
return @result if @result
|
|
72
|
+
end
|
|
73
|
+
public_send(action)
|
|
74
|
+
@result || head(:no_content)
|
|
75
|
+
rescue *::Wurk::Configuration::REDIS_ERROR_CLASSES => e
|
|
76
|
+
::Wurk.logger.warn("wurk web: redis unavailable (#{e.class}: #{e.message})")
|
|
77
|
+
@result = nil
|
|
78
|
+
render json: { error: 'redis_unavailable' }, status: :service_unavailable
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def logger = ::Wurk.logger
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def render(json: nil, html: nil, body: nil, status: 200, content_type: nil, layout: nil) # rubocop:disable Lint/UnusedMethodArgument -- ActionController's keyword
|
|
86
|
+
if json
|
|
87
|
+
finish(status, content_type || JSON_TYPE, Dashboard.json(json))
|
|
88
|
+
else
|
|
89
|
+
finish(status, content_type || 'text/html; charset=utf-8', (html || body).to_s)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def head(status)
|
|
94
|
+
finish(status, nil, '')
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def redirect_to(url, allow_other_host: false) # rubocop:disable Lint/UnusedMethodArgument -- ActionController's keyword
|
|
98
|
+
response.headers['location'] = url.to_s
|
|
99
|
+
finish(302, nil, '')
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def send_data(data, type:, disposition: 'inline')
|
|
103
|
+
response.headers['content-disposition'] = disposition
|
|
104
|
+
finish(200, type, data.to_s)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def send_file(path, disposition: 'inline')
|
|
108
|
+
send_data(::File.binread(path), type: ::Rack::Mime.mime_type(::File.extname(path.to_s)),
|
|
109
|
+
disposition: disposition)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def finish(status, content_type, body)
|
|
113
|
+
headers = response.headers.transform_keys(&:downcase)
|
|
114
|
+
headers['content-type'] = content_type if content_type
|
|
115
|
+
@result = [::Rack::Utils.status_code(status), headers, [body]]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Rack parses the query string and a form body; the SPA posts JSON.
|
|
119
|
+
def request_params
|
|
120
|
+
form = request.params
|
|
121
|
+
return form unless request.media_type == 'application/json'
|
|
122
|
+
|
|
123
|
+
parsed = ::JSON.parse(request.body&.read.to_s.then { |raw| raw.empty? ? '{}' : raw })
|
|
124
|
+
parsed.is_a?(::Hash) ? form.merge(parsed) : form
|
|
125
|
+
rescue ::JSON::ParserError
|
|
126
|
+
form
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# JSON as ActiveSupport's encoder writes it for the payloads the
|
|
131
|
+
# dashboard renders: a non-finite float is null rather than an
|
|
132
|
+
# unparseable `NaN`, and a Time is ISO 8601.
|
|
133
|
+
def self.json(value) = ::JSON.generate(encodable(value))
|
|
134
|
+
|
|
135
|
+
def self.encodable(value)
|
|
136
|
+
case value
|
|
137
|
+
when ::Hash then value.to_h { |k, v| [k.to_s, encodable(v)] }
|
|
138
|
+
when ::Array then value.map { |v| encodable(v) }
|
|
139
|
+
when ::Float then value.finite? ? value : nil
|
|
140
|
+
when ::Time then value.iso8601(3)
|
|
141
|
+
else value
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'controller'
|
|
4
|
+
require_relative '../pool_scope'
|
|
5
|
+
require_relative 'api_actions'
|
|
6
|
+
require_relative 'extension_actions'
|
|
7
|
+
require_relative 'profile_actions'
|
|
8
|
+
require_relative 'shell'
|
|
9
|
+
require_relative 'stats_tick'
|
|
10
|
+
require_relative 'stream_concurrency_guard'
|
|
11
|
+
|
|
12
|
+
module Wurk
|
|
13
|
+
class Web
|
|
14
|
+
module Dashboard
|
|
15
|
+
# The standalone mount's controllers: the engine's actions over
|
|
16
|
+
# Dashboard::Controller instead of ActionController.
|
|
17
|
+
class ShellEndpoint < Controller
|
|
18
|
+
def index
|
|
19
|
+
render html: Shell.new(request, asset_base: "#{request.script_name.chomp('/')}#{AssetMount::PREFIX}").html
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class ApiEndpoint < Controller
|
|
24
|
+
include ApiActions
|
|
25
|
+
|
|
26
|
+
# The engine streams through ActionController::Live; here the body is
|
|
27
|
+
# the stream, written as the server iterates it. The slot is held from
|
|
28
|
+
# here until the server closes the body. Past the cap: 503, as the
|
|
29
|
+
# engine answers.
|
|
30
|
+
def stream
|
|
31
|
+
unless StreamConcurrencyGuard.acquire
|
|
32
|
+
response.headers['Retry-After'] = StreamConcurrencyGuard::RETRY_AFTER_SECONDS.to_s
|
|
33
|
+
return head(:service_unavailable)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
tick, max_dur = stream_window
|
|
37
|
+
finish(200, 'text/event-stream', '')
|
|
38
|
+
@result[1].merge!('cache-control' => 'no-cache', 'x-accel-buffering' => 'no')
|
|
39
|
+
@result[2] = StatsStream.new(tick, max_dur)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
class ProfilesEndpoint < Controller
|
|
44
|
+
include ProfileActions
|
|
45
|
+
|
|
46
|
+
before_action :require_listed_profile
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class ExtensionsEndpoint < ShellEndpoint
|
|
50
|
+
include ExtensionActions
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# `/api/stream`'s body: one `event: stats` per tick until the deadline,
|
|
54
|
+
# in the bytes ActionController::Live::SSE writes for the engine.
|
|
55
|
+
class StatsStream
|
|
56
|
+
RETRY_MS = (ApiActions::STREAM_TICK_SECONDS * 1000).to_i
|
|
57
|
+
|
|
58
|
+
def initialize(tick, max_duration)
|
|
59
|
+
@tick = tick
|
|
60
|
+
@max_duration = max_duration
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The server iterates the body after the Rack app returned, outside
|
|
64
|
+
# the web pool scope it set, so each read re-enters it.
|
|
65
|
+
def each
|
|
66
|
+
deadline = monotime + @max_duration
|
|
67
|
+
loop do
|
|
68
|
+
yield event('stats', PoolScope.scope { StatsTick.payload })
|
|
69
|
+
break if monotime >= deadline
|
|
70
|
+
|
|
71
|
+
sleep @tick
|
|
72
|
+
end
|
|
73
|
+
rescue *::Wurk::Configuration::REDIS_ERROR_CLASSES => e
|
|
74
|
+
::Wurk.logger.warn("wurk web: stream redis unavailable (#{e.class}: #{e.message})")
|
|
75
|
+
yield event('error', { error: 'redis_unavailable' })
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def close
|
|
79
|
+
StreamConcurrencyGuard.release
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def event(name, payload)
|
|
85
|
+
"retry: #{RETRY_MS}\nevent: #{name}\ndata: #{Dashboard.json(payload)}\n\n"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def monotime = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../extension'
|
|
4
|
+
|
|
5
|
+
module Wurk
|
|
6
|
+
class Web
|
|
7
|
+
module Dashboard
|
|
8
|
+
# Third-party Web extensions (#187) inside the dashboard:
|
|
9
|
+
# `ext/:name/*subpath` runs the registered extension's matched route and
|
|
10
|
+
# returns its rendered HTML for the SPA's Extension page to embed;
|
|
11
|
+
# `ext-assets/:name/*file` serves files from the extension's
|
|
12
|
+
# `asset_paths`. An /ext URL with no extension registered under that name
|
|
13
|
+
# is the SPA's own client route, so it gets the shell (the includer's
|
|
14
|
+
# `index`). Shared by the engine's ExtensionsController and the
|
|
15
|
+
# standalone Rack mount.
|
|
16
|
+
module ExtensionActions
|
|
17
|
+
def show
|
|
18
|
+
result = Web::Extension::Renderer.call(
|
|
19
|
+
name: params[:name], method: request.request_method,
|
|
20
|
+
subpath: "/#{params[:subpath]}", env: request.env, mount: request.script_name
|
|
21
|
+
)
|
|
22
|
+
return index unless result # not an extension → SPA client route; let the SolidJS router handle it
|
|
23
|
+
|
|
24
|
+
respond_with(result)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def asset
|
|
28
|
+
file, cache_for = Web::Extension::Renderer.asset_file(params[:name], params[:file])
|
|
29
|
+
return head :not_found unless file
|
|
30
|
+
|
|
31
|
+
response.headers['Cache-Control'] = "public, max-age=#{cache_for}"
|
|
32
|
+
send_file file, disposition: 'inline'
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def respond_with((status, headers, body))
|
|
38
|
+
return redirect_to(headers['Location'], allow_other_host: false) if status == 302 && headers['Location']
|
|
39
|
+
|
|
40
|
+
content_type = 'text/html; charset=utf-8'
|
|
41
|
+
headers.each do |key, value|
|
|
42
|
+
next content_type = value if key.casecmp?('content-type')
|
|
43
|
+
|
|
44
|
+
response.headers[key] = value
|
|
45
|
+
end
|
|
46
|
+
# Extension output is host-registered server code, same trust model as
|
|
47
|
+
# Sidekiq::Web rendering its extensions — not user input.
|
|
48
|
+
render body: body, content_type: content_type, status: status
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
require 'uri'
|
|
6
|
+
|
|
7
|
+
module Wurk
|
|
8
|
+
class Web
|
|
9
|
+
module Dashboard
|
|
10
|
+
# Profiles pane non-JSON endpoints (spec §25.4):
|
|
11
|
+
#
|
|
12
|
+
# GET /profiles/:key/data → the stored gzipped gecko JSON, streamed with a
|
|
13
|
+
# gzip Content-Encoding (Firefox profiler pulls
|
|
14
|
+
# this when given a `from-url` source).
|
|
15
|
+
# POST /profiles/:key → upload the profile to the Firefox profiler
|
|
16
|
+
# store and 302 to its public view URL.
|
|
17
|
+
# GET /profiles/:key → 302 to an upload already made (by either UI),
|
|
18
|
+
# 405 when there is none yet.
|
|
19
|
+
#
|
|
20
|
+
# Sidekiq uploads on the GET. Sending job data to a third party is a side
|
|
21
|
+
# effect, so here only a POST does it: SameOriginGuard covers it like every
|
|
22
|
+
# other mutation, and read-only mode refuses it (#101 W5).
|
|
23
|
+
#
|
|
24
|
+
# `:key` is "<token>-<jid>". The JSON list lives at /api/profiles.
|
|
25
|
+
#
|
|
26
|
+
# `:key` is request input that is otherwise used verbatim as a Redis key, so
|
|
27
|
+
# every action first requires it to be a member of the `profiles` ZSET
|
|
28
|
+
# (where both Wurk and Sidekiq register each stored profile). Without that,
|
|
29
|
+
# a non-HASH key WRONGTYPEs into a false "redis_unavailable" 503, and any
|
|
30
|
+
# HASH with a `data` field could be shipped to the profiler and have a `sid`
|
|
31
|
+
# written into it. #
|
|
32
|
+
# Shared by the engine's ProfilesController and the standalone Rack mount;
|
|
33
|
+
# each includer runs `require_listed_profile` before every action.
|
|
34
|
+
module ProfileActions
|
|
35
|
+
def data
|
|
36
|
+
blob = profile_blob(params[:key])
|
|
37
|
+
return head(:not_found) unless blob
|
|
38
|
+
|
|
39
|
+
response.headers['Content-Encoding'] = 'gzip'
|
|
40
|
+
send_data blob, type: 'application/json', disposition: 'inline'
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def show
|
|
44
|
+
return head(:forbidden) if ::Wurk::Web.config.read_only?
|
|
45
|
+
|
|
46
|
+
sid = cached_sid(params[:key])
|
|
47
|
+
return redirect_to_profile(sid) if sid
|
|
48
|
+
|
|
49
|
+
response.headers['Allow'] = 'POST'
|
|
50
|
+
head :method_not_allowed
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Read-only mode 403s this POST in Wurk::Web::Authorization, before it
|
|
54
|
+
# gets here.
|
|
55
|
+
def upload
|
|
56
|
+
sid = profile_sid(params[:key])
|
|
57
|
+
return head(:not_found) if sid == :missing
|
|
58
|
+
return head(:bad_gateway) unless sid
|
|
59
|
+
|
|
60
|
+
redirect_to_profile(sid)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def redirect_to_profile(sid)
|
|
66
|
+
redirect_to(format(::Wurk::Web.config.profile_view_url, sid), allow_other_host: true)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The member's score is its expiry, the same instant the HASH's TTL lapses;
|
|
70
|
+
# ProfileSet purges expired members lazily, so a past score is gone too.
|
|
71
|
+
def require_listed_profile
|
|
72
|
+
expires_at = ::Wurk.redis { |c| c.call('ZSCORE', ::Wurk::Keys::PROFILES, params[:key].to_s) }
|
|
73
|
+
head(:not_found) unless expires_at && Float(expires_at) > ::Time.now.to_f
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def cached_sid(key) = ::Wurk.redis { |c| c.call('HGET', key, 'sid') }
|
|
77
|
+
|
|
78
|
+
def profile_blob(key)
|
|
79
|
+
::Wurk::ProfileRecord.data_for(key)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The profile-store id, cached in the HASH's `sid` field after the first
|
|
83
|
+
# upload exactly as Sidekiq's Web UI does, so either UI reuses the other's
|
|
84
|
+
# upload. :missing when the profile is gone, nil when the upload failed.
|
|
85
|
+
def profile_sid(key)
|
|
86
|
+
sid = cached_sid(key)
|
|
87
|
+
return sid if sid
|
|
88
|
+
|
|
89
|
+
blob = profile_blob(key)
|
|
90
|
+
return :missing unless blob
|
|
91
|
+
|
|
92
|
+
sid = upload_to_profiler(blob)
|
|
93
|
+
::Wurk.redis { |c| c.call('HSET', key, 'sid', sid) } if sid
|
|
94
|
+
sid
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# POSTs the gzipped profile to the Firefox profiler's compressed-store,
|
|
98
|
+
# which answers with a JWT whose payload carries the `profileToken` the
|
|
99
|
+
# view URL needs. Returns that token, or nil on failure.
|
|
100
|
+
def upload_to_profiler(gzipped)
|
|
101
|
+
uri = URI.parse(::Wurk::Web.config.profile_store_url)
|
|
102
|
+
res = post_gzip(uri, gzipped)
|
|
103
|
+
res.is_a?(Net::HTTPSuccess) ? profile_token(res.body.to_s) : nil
|
|
104
|
+
rescue StandardError => e
|
|
105
|
+
Wurk.configuration.handle_exception(e, context: 'Wurk dashboard profile upload')
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def profile_token(jwt)
|
|
110
|
+
payload = jwt.strip.split('.')[1].to_s.tr('-_', '+/')
|
|
111
|
+
::JSON.parse(payload.unpack1('m'))['profileToken']
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def post_gzip(uri, body)
|
|
115
|
+
req = Net::HTTP::Post.new(uri)
|
|
116
|
+
req['Content-Encoding'] = 'gzip'
|
|
117
|
+
req['Content-Type'] = 'application/json'
|
|
118
|
+
req['Accept'] = 'application/vnd.firefox-profiler+json;version=1.0'
|
|
119
|
+
req.body = body
|
|
120
|
+
# Explicit timeouts so a slow/unreachable profiler can't tie up the Rails
|
|
121
|
+
# request thread for Ruby's ~60s defaults.
|
|
122
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https',
|
|
123
|
+
open_timeout: 5, read_timeout: 15) do |http|
|
|
124
|
+
http.request(req)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rack'
|
|
4
|
+
require_relative 'endpoints'
|
|
5
|
+
require_relative '../asset_mount'
|
|
6
|
+
|
|
7
|
+
module Wurk
|
|
8
|
+
class Web
|
|
9
|
+
module Dashboard
|
|
10
|
+
# The dashboard under the standalone Rack mount (`run Sidekiq::Web`): the
|
|
11
|
+
# same paths the engine draws in config/routes.rb, mapped to the
|
|
12
|
+
# standalone endpoints. test/unit/web_dashboard_routes_test.rb holds this
|
|
13
|
+
# table and the engine's routes to the same list.
|
|
14
|
+
module Routes
|
|
15
|
+
API = [
|
|
16
|
+
['GET', '/api/meta', :meta], ['GET', '/api/stats', :stats], ['GET', '/api/queues', :queues],
|
|
17
|
+
['GET', '/api/queues/:name', :queue], ['POST', '/api/queues/:name/clear', :clear_queue],
|
|
18
|
+
['POST', '/api/queues/:name/delete', :delete_queue_job], ['POST', '/api/queues/:name/pause', :pause_queue],
|
|
19
|
+
['POST', '/api/queues/:name/unpause', :unpause_queue],
|
|
20
|
+
['GET', '/api/retries', :retries], ['POST', '/api/retries', :retries_bulk],
|
|
21
|
+
['POST', '/api/retries/all/:cmd', :retries_all], ['POST', '/api/retries/:key', :retry_job],
|
|
22
|
+
['GET', '/api/retries/:key', :retry_entry],
|
|
23
|
+
['GET', '/api/scheduled', :scheduled], ['POST', '/api/scheduled', :scheduled_bulk],
|
|
24
|
+
['POST', '/api/scheduled/all/:cmd', :scheduled_all], ['POST', '/api/scheduled/:key', :scheduled_job],
|
|
25
|
+
['GET', '/api/scheduled/:key', :scheduled_entry],
|
|
26
|
+
['GET', '/api/dead', :dead], ['POST', '/api/dead', :dead_bulk], ['POST', '/api/dead/all/:cmd', :dead_all],
|
|
27
|
+
['POST', '/api/dead/:key', :dead_job], ['GET', '/api/dead/:key', :dead_entry],
|
|
28
|
+
['GET', '/api/processes', :processes], ['GET', '/api/workers', :workers],
|
|
29
|
+
['POST', '/api/busy/quiet', :quiet_process], ['POST', '/api/busy/stop', :stop_process],
|
|
30
|
+
['GET', '/api/flows', :flows], ['GET', '/api/flows/:fid', :flow],
|
|
31
|
+
['POST', '/api/flows/:fid/abandon', :abandon_flow],
|
|
32
|
+
['GET', '/api/batches', :batches], ['GET', '/api/batches/:bid', :batch],
|
|
33
|
+
['GET', '/api/limiters', :limiters], ['POST', '/api/limiters/:name/reset', :reset_limiter],
|
|
34
|
+
['GET', '/api/cron', :cron], ['POST', '/api/cron/:lid/pause', :pause_cron],
|
|
35
|
+
['POST', '/api/cron/:lid/unpause', :unpause_cron], ['POST', '/api/cron/:lid/enqueue', :enqueue_cron],
|
|
36
|
+
['GET', '/api/cron/:lid/history', :cron_history],
|
|
37
|
+
['GET', '/api/metrics', :metrics], ['GET', '/api/metrics/:klass', :metrics_for_job],
|
|
38
|
+
['GET', '/api/history/snapshots', :history_snapshots], ['GET', '/api/history/:bucket', :history],
|
|
39
|
+
['GET', '/api/queue-history/:bucket', :queue_history], ['GET', '/api/search', :search],
|
|
40
|
+
['GET', '/api/profiles', :profiles], ['GET', '/api/stream', :stream]
|
|
41
|
+
].map { |verb, path, action| [verb, path, ApiEndpoint, action] }
|
|
42
|
+
|
|
43
|
+
PROFILES = [
|
|
44
|
+
['GET', '/profiles/:key/data', ProfilesEndpoint, :data],
|
|
45
|
+
['GET', '/profiles/:key', ProfilesEndpoint, :show],
|
|
46
|
+
['POST', '/profiles/:key', ProfilesEndpoint, :upload]
|
|
47
|
+
].freeze
|
|
48
|
+
|
|
49
|
+
TABLE = (API + PROFILES).map do |verb, path, endpoint, action|
|
|
50
|
+
[verb, path.split('/').reject(&:empty?), endpoint, action].freeze
|
|
51
|
+
end.freeze
|
|
52
|
+
|
|
53
|
+
EXTENSION_VERBS = %w[GET POST PUT PATCH DELETE].freeze
|
|
54
|
+
# A path ending in a static-file extension is never a client route, so
|
|
55
|
+
# a stale asset or source-map URL 404s rather than answering 200 HTML.
|
|
56
|
+
STATIC_FILE = /\.(?:js|mjs|css|map|json|png|jpe?g|gif|svg|ico|webp|woff2?|ttf|txt)\z/i
|
|
57
|
+
|
|
58
|
+
class << self
|
|
59
|
+
# A Rack triple, or nil when the path is not the dashboard's.
|
|
60
|
+
def call(env)
|
|
61
|
+
verb = env['REQUEST_METHOD'] == 'HEAD' ? 'GET' : env['REQUEST_METHOD']
|
|
62
|
+
path = env['PATH_INFO'].to_s
|
|
63
|
+
return asset(env) if path.start_with?("#{AssetMount::PREFIX}/")
|
|
64
|
+
|
|
65
|
+
endpoint, action, params = match(verb, segments(path))
|
|
66
|
+
return endpoint.call(env, action, params) if endpoint
|
|
67
|
+
|
|
68
|
+
extension(env, verb, path) || shell(env, verb, path)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def match(verb, parts)
|
|
72
|
+
TABLE.each do |route_verb, pattern, endpoint, action|
|
|
73
|
+
next unless route_verb == verb && pattern.size == parts.size
|
|
74
|
+
|
|
75
|
+
params = bind(pattern, parts)
|
|
76
|
+
return [endpoint, action, params] if params
|
|
77
|
+
end
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def segments(path) = path.split('/').reject(&:empty?).map { |seg| ::Rack::Utils.unescape_path(seg) }
|
|
84
|
+
|
|
85
|
+
def bind(pattern, parts)
|
|
86
|
+
pattern.each_with_index.with_object({}) do |(seg, i), params|
|
|
87
|
+
if seg.start_with?(':') then params[seg[1..]] = parts[i]
|
|
88
|
+
elsif seg != parts[i] then return nil
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def asset(env)
|
|
94
|
+
@assets ||= AssetMount.new(->(_env) { [404, {}, []] })
|
|
95
|
+
response = @assets.call(env)
|
|
96
|
+
response[0] == 404 ? nil : response
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# `ext/:name(/*subpath)` and `ext-assets/:name/*file`, as the engine
|
|
100
|
+
# draws them.
|
|
101
|
+
def extension(env, verb, path)
|
|
102
|
+
parts = segments(path)
|
|
103
|
+
return unless parts.size >= 2
|
|
104
|
+
|
|
105
|
+
case parts.first
|
|
106
|
+
when 'ext'
|
|
107
|
+
return unless EXTENSION_VERBS.include?(verb)
|
|
108
|
+
|
|
109
|
+
params = { 'name' => parts[1], 'subpath' => parts[2..].join('/') }
|
|
110
|
+
ExtensionsEndpoint.call(env, :show, params)
|
|
111
|
+
when 'ext-assets'
|
|
112
|
+
return unless verb == 'GET' && parts.size >= 3
|
|
113
|
+
|
|
114
|
+
ExtensionsEndpoint.call(env, :asset, { 'name' => parts[1], 'file' => parts[2..].join('/') })
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Every other GET a browser makes is a client route of the SPA, but
|
|
119
|
+
# an unknown `/api/...` path is a missing endpoint, not a page.
|
|
120
|
+
def shell(env, verb, path)
|
|
121
|
+
return unless verb == 'GET' && !path.match?(STATIC_FILE) && html?(env['HTTP_ACCEPT'])
|
|
122
|
+
return if path == '/api' || path.start_with?('/api/')
|
|
123
|
+
|
|
124
|
+
ShellEndpoint.call(env, :index)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def html?(accept)
|
|
128
|
+
accept.to_s.empty? || accept.include?('text/html') || accept.include?('*/*')
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|