-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
255 lines (220 loc) · 6.62 KB
/
Copy pathapp.rb
File metadata and controls
255 lines (220 loc) · 6.62 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# app.rb
require "sinatra"
require "tmpdir"
require "fileutils"
require "open-uri"
set :server, :puma
set :port, ENV["PORT"] || 3000
# Load install scripts, version files, and other assets at startup
INSTALL_SCRIPT = File.read(File.join(__dir__, "install.sh"))
VERSION = File.read(File.join(__dir__, "VERSION")).strip
ROBOTS = File.read(File.join(__dir__, "robots.txt")).strip
OPS_INSTALL_SCRIPT = begin
File.read(File.join(__dir__, "ops/install.sh"))
rescue
""
end
OPS_VERSION = begin
File.read(File.join(__dir__, "ops/VERSION")).strip
rescue
"0.1.0"
end
ARMOR_INSTALL_SCRIPT = begin
File.read(File.join(__dir__, "armor/install.sh"))
rescue
""
end
ARMOR_VERSION = begin
File.read(File.join(__dir__, "armor/VERSION")).strip
rescue
"0.1.0"
end
EXT_VAULT_INSTALL_SCRIPT = begin
File.read(File.join(__dir__, "ext/vault/install.sh"))
rescue
""
end
EXT_VAULT_VERSION = begin
File.read(File.join(__dir__, "ext/vault/VERSION")).strip
rescue
"0.1.0"
end
helpers do
def handle_download(os, name, arch, version = nil)
arch = arch.gsub(/\.[^\/.]+$/, "").downcase.strip
version ||= VERSION
version = version.sub(/^v/, "") if version.start_with?("v")
binary_name = (os == "windows") ? "#{name}.exe" : name
repo = "#{name}-#{os}-#{arch}"
filename = "#{repo}-#{version}.tgz"
registry_url = "https://registry.npmjs.org/@dotenvx/#{repo}/-/#{filename}"
Dir.mktmpdir do |tmp_dir|
tmp_download_path = File.join(tmp_dir, filename)
tmp_tar_path = File.join(tmp_dir, "output.tgz")
command = <<~SH
curl -sS -L #{registry_url} -o #{tmp_download_path} &&
tar -xzf #{tmp_download_path} -C #{tmp_dir} --strip-components=1 package &&
chmod 755 #{File.join(tmp_dir, binary_name)} &&
tar -czf #{tmp_tar_path} -C #{tmp_dir} #{binary_name}
SH
system(command)
if File.exist?(tmp_tar_path)
send_file tmp_tar_path, filename: filename, type: "application/gzip"
else
halt 500, "500 error"
end
end
end
def format_number(num)
case num
when 1_000_000..Float::INFINITY
"#{(num / 1_000_000.0).round(1)}M"
when 1_000..999_999
"#{(num / 1_000.0).round(1)}k"
else
num.to_s
end
end
def handle_stats(packages)
total_downloads = packages.sum do |pkg|
response = URI.open("https://api.npmjs.org/downloads/point/last-year/#{pkg}")
data = JSON.parse(response.read)
data["downloads"]
rescue
0
end
{
schemaVersion: 1,
label: "downloads",
message: format_number(total_downloads),
color: "brightgreen"
}.to_json
end
def handle_install(install_script, version = nil, directory = nil, os = nil, arch = nil, force = nil)
result = install_script.dup
result.gsub!(/VERSION="[^"]*"/, "VERSION=\"#{version}\"") if version
result.gsub!(/OS="[^"]*"/, "OS=\"#{os}\"") if os
result.gsub!(/ARCH="[^"]*"/, "ARCH=\"#{arch}\"") if arch
result.gsub!(/DIRECTORY="[^"]*"/, "DIRECTORY=\"#{directory}\"") if directory
result.gsub!(/FORCE="[^"]*"/, "FORCE=\"#{force}\"") if force
result
end
end
get "/" do
content_type "text/plain"
handle_install(INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
get "/robots.txt" do
content_type "text/plain"
ROBOTS
end
get "/VERSION" do
content_type "text/plain"
VERSION
end
get "/install.sh" do
content_type "text/plain"
handle_install(INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
# for historical purposes
get "/installer.sh" do
content_type "text/plain"
handle_install(INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
# for ops
get "/ops" do
content_type "text/plain"
handle_install(OPS_INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
get "/ops/" do
content_type "text/plain"
handle_install(OPS_INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
get "/ops/install.sh" do
content_type "text/plain"
handle_install(OPS_INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
get "/ops/VERSION" do
content_type "text/plain"
OPS_VERSION
end
# for armor
get "/armor" do
content_type "text/plain"
handle_install(ARMOR_INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
get "/armor/" do
content_type "text/plain"
handle_install(ARMOR_INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
get "/armor/install.sh" do
content_type "text/plain"
handle_install(ARMOR_INSTALL_SCRIPT, params["version"], params["directory"], params["os"], params["arch"], params["force"])
end
get "/armor/VERSION" do
content_type "text/plain"
ARMOR_VERSION
end
# Ext Vault routes
get "/ext/vault" do
content_type "text/plain"
handle_install(EXT_VAULT_INSTALL_SCRIPT, params["version"], params["directory"])
end
get "/ext/vault/install.sh" do
content_type "text/plain"
handle_install(EXT_VAULT_INSTALL_SCRIPT, params["version"], params["directory"])
end
get "/ext/vault/VERSION" do
content_type "text/plain"
EXT_VAULT_VERSION
end
get "/stats/curl" do
content_type "application/json"
handle_stats([
"@dotenvx/dotenvx-darwin-amd64",
"@dotenvx/dotenvx-darwin-arm64",
"@dotenvx/dotenvx-darwin-x86_64",
"@dotenvx/dotenvx-linux-aarch64",
"@dotenvx/dotenvx-linux-amd64",
"@dotenvx/dotenvx-linux-arm64",
"@dotenvx/dotenvx-linux-x86_64",
"@dotenvx/dotenvx-windows-amd64",
"@dotenvx/dotenvx-windows-x86_64"
])
end
get "/stats/curl/darwin" do
content_type "application/json"
handle_stats([
"@dotenvx/dotenvx-darwin-amd64",
"@dotenvx/dotenvx-darwin-arm64",
"@dotenvx/dotenvx-darwin-x86_64"
])
end
get "/stats/curl/linux" do
content_type "application/json"
handle_stats([
"@dotenvx/dotenvx-linux-aarch64",
"@dotenvx/dotenvx-linux-amd64",
"@dotenvx/dotenvx-linux-arm64",
"@dotenvx/dotenvx-linux-x86_64"
])
end
get "/stats/curl/windows" do
content_type "application/json"
handle_stats([
"@dotenvx/dotenvx-windows-amd64",
"@dotenvx/dotenvx-windows-x86_64"
])
end
get "/darwin/*" do
arch = params["splat"].first
handle_download("darwin", "dotenvx", arch, params["version"])
end
get "/linux/*" do
arch = params["splat"].first
handle_download("linux", "dotenvx", arch, params["version"])
end
get "/windows/*" do
arch = params["splat"].first
handle_download("windows", "dotenvx", arch, params["version"])
end