This repository was archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.ru
82 lines (66 loc) · 1.8 KB
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true
require 'rubygems'
require 'bundler/setup'
require 'sinatra/base'
require 'rack/cache'
require 'rack/cache/key'
require 'rack/deflater'
require 'pathname'
class QuerylessCacheKey < Rack::Cache::Key
private
# Internal: This API does not consider query string parameters, so ignore
# them when generating cache keys.
#
def query_string
nil
end
end
class App < Sinatra::Base
CONTENT_TYPE = 'text/plain'
HEADERS = {
'Access-Control-Allow-Origin' => '*',
'Content-Type' => "#{CONTENT_TYPE}; charset=utf-8"
}.freeze
CACHEABLE_HEADERS = HEADERS.merge({
'Cache-Control' => 'public, max-age=31536000'
})
PATH = Pathname.new(__FILE__).join('../generated').freeze
STATUS = 'alive'
def app_path(*tag)
PATH.join(*tag)
end
def generated_apps
PATH.children.map(&:basename)
end
def known_versions
generated_apps.map { |name| Gem::Version.new(name.to_s[1..-1]) }.sort
end
before do
halt 406, HEADERS, '' unless request.accept?(CONTENT_TYPE)
end
get '/status' do
return 200, HEADERS, STATUS
end
get '/versions' do
return 200, HEADERS, known_versions.join("\n")
end
get %r{/(v[^/]+)/(v[^/]+)(?:/(.+))?} do |source, target, path|
file_path = path || ''
source_app_path = app_path(source)
source_path = app_path(source, file_path)
target_app_path = app_path(target)
target_path = app_path(target, file_path)
if source_app_path.exist? && target_app_path.exist? && source_path.exist? || target_path.exist?
diff = `diff -Nr -U 1000 -x '*.png' #{source_path} #{target_path}`
return 200, CACHEABLE_HEADERS, diff
else
halt 404, HEADERS, ''
end
end
not_found do
halt 404, HEADERS, ''
end
end
use Rack::Cache, { :cache_key => QuerylessCacheKey }
use Rack::Deflater
run App