boto-rb-gateway 0.18.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +42 -0
- data/lib/boto/gateway/assets/panel.css +314 -0
- data/lib/boto/gateway/env_rows.rb +52 -0
- data/lib/boto/gateway/health.rb +55 -0
- data/lib/boto/gateway/html_view.rb +33 -0
- data/lib/boto/gateway/json_view.rb +37 -0
- data/lib/boto/gateway/masking.rb +40 -0
- data/lib/boto/gateway/mounts.rb +67 -0
- data/lib/boto/gateway/page.rb +29 -0
- data/lib/boto/gateway/panel.html.erb +136 -0
- data/lib/boto/gateway/panel.rb +65 -0
- data/lib/boto/gateway/service_info.rb +54 -0
- data/lib/boto/gateway/version.rb +7 -0
- data/lib/boto/gateway.rb +15 -0
- data/lib/boto/plugins/gateway.rb +76 -0
- data/lib/boto/templates/gateway/files/config/app.rb.erb +7 -0
- data/lib/boto/templates/gateway/files/test/config/gateway_test.rb.erb +10 -0
- data/lib/boto/templates/gateway/files/test/test_helper.rb.erb +9 -0
- data/lib/boto/templates/gateway/template.rb +9 -0
- metadata +74 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d2c90f42c17971ec3a21cb4986c2dee37765a780bd1c9fd44566a9878205a5e0
|
|
4
|
+
data.tar.gz: 64466343c02d31b31a2ef497dc56bc39eb3b0d7c6fb399e3a6e1481b2da6788d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1bd6ae74e431e7d268360ce79a4500530bf8e41d15d4efd18114bf56f0dd2c48601d42d347d400dc73dbab936fb6bb6558c047889848dd440641034e9e9e28a9
|
|
7
|
+
data.tar.gz: e67d5775f2d88d877ec99603ef37cea13354f03306061ccde15e15ce5a8eb30bbed07576c106c57de8c21b0910ee1b70161072bac4050e3c663207735cf9a178
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Evo Tech Builder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# boto-rb-gateway
|
|
2
|
+
|
|
3
|
+
Single entry point for a Boto solution in development: the `gateway` template
|
|
4
|
+
mounts the solution's HTTP services in one process, each under `/<project>`,
|
|
5
|
+
and serves the `/_boto` panel.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
# Gemfile of the solution
|
|
11
|
+
gem "boto-rb-gateway"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bundle install
|
|
16
|
+
boto add gateway borda --refs catalogo,users
|
|
17
|
+
boto run
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Plugin
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
# apps/borda/config/app.rb
|
|
24
|
+
Boto.configure do |app|
|
|
25
|
+
app.use :health
|
|
26
|
+
app.use :gateway, prefixes: { "users" => "/contas" }, panel: true
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- `prefixes:` overrides the default `/<project>` prefix of a service.
|
|
31
|
+
- `panel:` turns `/_boto` on or off (default: on in development only).
|
|
32
|
+
- The gateway refuses to boot in `production`; there, route with your ingress.
|
|
33
|
+
|
|
34
|
+
## Panel
|
|
35
|
+
|
|
36
|
+
`GET /_boto` lists each service with its prefix, port, health (probed in the
|
|
37
|
+
same process), routes, OpenAPI and Swagger links and its env variables, with
|
|
38
|
+
sensitive values masked. `GET /_boto/services.json` has the same data.
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT.
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/* Panel styles, served once from /_boto/panel.css. Colours follow the approved design. */
|
|
2
|
+
|
|
3
|
+
:root {
|
|
4
|
+
--ink: #1f1f24;
|
|
5
|
+
--muted: #52525b;
|
|
6
|
+
--line: #e4e4e7;
|
|
7
|
+
--line-soft: #f0f0f1;
|
|
8
|
+
--surface: #ffffff;
|
|
9
|
+
--page: #f6f6f7;
|
|
10
|
+
--mono: ui-monospace, "SF Mono", Menlo, monospace;
|
|
11
|
+
--sans: "Avenir Next", Avenir, "Segoe UI Variable Display", "Segoe UI", system-ui, sans-serif;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
* { box-sizing: border-box; }
|
|
15
|
+
|
|
16
|
+
body {
|
|
17
|
+
margin: 0;
|
|
18
|
+
background: var(--page);
|
|
19
|
+
color: var(--ink);
|
|
20
|
+
font-family: var(--sans);
|
|
21
|
+
font-size: 15px;
|
|
22
|
+
line-height: 1.5;
|
|
23
|
+
min-height: 100vh;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
a { color: var(--ink); }
|
|
27
|
+
a:hover { color: #cc342d; }
|
|
28
|
+
|
|
29
|
+
h1, h2 { margin: 0; }
|
|
30
|
+
|
|
31
|
+
.muted { color: var(--muted); font-size: 13px; }
|
|
32
|
+
|
|
33
|
+
/* Top bar */
|
|
34
|
+
|
|
35
|
+
.topbar { background: var(--ink); color: #ffffff; }
|
|
36
|
+
|
|
37
|
+
.topbar__inner,
|
|
38
|
+
.topbar__brand,
|
|
39
|
+
.topbar__nav,
|
|
40
|
+
.service__head,
|
|
41
|
+
.service__identity,
|
|
42
|
+
.service__status,
|
|
43
|
+
.section-head,
|
|
44
|
+
.card__summary,
|
|
45
|
+
.footer {
|
|
46
|
+
display: flex;
|
|
47
|
+
flex-wrap: wrap;
|
|
48
|
+
align-items: center;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.topbar__inner {
|
|
52
|
+
justify-content: space-between;
|
|
53
|
+
gap: 12px;
|
|
54
|
+
max-width: 1120px;
|
|
55
|
+
margin: 0 auto;
|
|
56
|
+
padding: 14px 24px;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.topbar__brand { gap: 12px; }
|
|
60
|
+
|
|
61
|
+
.topbar__nav {
|
|
62
|
+
gap: 20px;
|
|
63
|
+
font-size: 14px;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.topbar a { color: #ffffff; }
|
|
67
|
+
|
|
68
|
+
.brand { font-size: 20px; font-weight: 700; letter-spacing: -0.3px; }
|
|
69
|
+
|
|
70
|
+
.gateway-name { font-size: 16px; font-weight: 600; }
|
|
71
|
+
|
|
72
|
+
.topbar .muted { color: #a1a1aa; }
|
|
73
|
+
|
|
74
|
+
.env-badge {
|
|
75
|
+
font-size: 12px;
|
|
76
|
+
font-weight: 600;
|
|
77
|
+
padding: 2px 10px;
|
|
78
|
+
border-radius: 999px;
|
|
79
|
+
background: #3f3f46;
|
|
80
|
+
color: #fde68a;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.host {
|
|
84
|
+
font-family: var(--mono);
|
|
85
|
+
color: #d4d4d8;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* Layout */
|
|
89
|
+
|
|
90
|
+
.panel {
|
|
91
|
+
display: flex;
|
|
92
|
+
flex-direction: column;
|
|
93
|
+
gap: 28px;
|
|
94
|
+
max-width: 1120px;
|
|
95
|
+
margin: 0 auto;
|
|
96
|
+
padding: 32px 24px 48px;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.summary {
|
|
100
|
+
display: grid;
|
|
101
|
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
102
|
+
gap: 16px;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
.stat {
|
|
106
|
+
background: var(--surface);
|
|
107
|
+
border: 1px solid var(--line);
|
|
108
|
+
border-radius: 12px;
|
|
109
|
+
padding: 16px 20px;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.stat__label { font-size: 13px; color: var(--muted); }
|
|
113
|
+
|
|
114
|
+
.stat__value { font-size: 28px; font-weight: 700; }
|
|
115
|
+
|
|
116
|
+
.stat__value--ready { color: #166534; }
|
|
117
|
+
|
|
118
|
+
.stat__value--failing { color: #b42318; }
|
|
119
|
+
|
|
120
|
+
.card,
|
|
121
|
+
.service {
|
|
122
|
+
background: var(--surface);
|
|
123
|
+
border: 1px solid var(--line);
|
|
124
|
+
border-radius: 14px;
|
|
125
|
+
overflow: hidden;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.card__summary,
|
|
129
|
+
.service__env-summary {
|
|
130
|
+
cursor: pointer;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.card__summary {
|
|
134
|
+
gap: 12px;
|
|
135
|
+
padding: 16px 22px;
|
|
136
|
+
align-items: baseline;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.card__title { display: inline; font-size: 19px; font-weight: 700; }
|
|
140
|
+
|
|
141
|
+
.services { display: flex; flex-direction: column; gap: 20px; }
|
|
142
|
+
|
|
143
|
+
.section-head {
|
|
144
|
+
justify-content: space-between;
|
|
145
|
+
align-items: baseline;
|
|
146
|
+
gap: 8px;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.section-title { font-size: 24px; font-weight: 700; letter-spacing: -0.3px; }
|
|
150
|
+
|
|
151
|
+
/* Services */
|
|
152
|
+
|
|
153
|
+
.service__head {
|
|
154
|
+
justify-content: space-between;
|
|
155
|
+
gap: 12px;
|
|
156
|
+
padding: 18px 22px;
|
|
157
|
+
border-bottom: 1px solid var(--line-soft);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.service__identity { gap: 12px; }
|
|
161
|
+
|
|
162
|
+
.service__name { font-size: 19px; font-weight: 700; }
|
|
163
|
+
|
|
164
|
+
.service__status {
|
|
165
|
+
gap: 16px;
|
|
166
|
+
font-size: 14px;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.prefix {
|
|
170
|
+
font-family: var(--mono);
|
|
171
|
+
font-size: 14px;
|
|
172
|
+
padding: 2px 8px;
|
|
173
|
+
border-radius: 6px;
|
|
174
|
+
background: #fdf2f1;
|
|
175
|
+
color: #9f2a24;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
.port-link { font-family: var(--mono); font-size: 14px; }
|
|
179
|
+
|
|
180
|
+
.link-strong { font-weight: 600; }
|
|
181
|
+
|
|
182
|
+
.health {
|
|
183
|
+
display: inline-flex;
|
|
184
|
+
align-items: center;
|
|
185
|
+
gap: 6px;
|
|
186
|
+
font-weight: 600;
|
|
187
|
+
color: var(--muted);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.health--ready { color: #166534; }
|
|
191
|
+
|
|
192
|
+
.health--failing { color: #b42318; }
|
|
193
|
+
|
|
194
|
+
.health-detail {
|
|
195
|
+
margin: 14px 22px 0;
|
|
196
|
+
padding: 10px 14px;
|
|
197
|
+
border-radius: 8px;
|
|
198
|
+
background: #fef3f2;
|
|
199
|
+
border: 1px solid #fecdca;
|
|
200
|
+
color: #912018;
|
|
201
|
+
font-family: var(--mono);
|
|
202
|
+
font-size: 14px;
|
|
203
|
+
word-break: break-word;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/* Tables */
|
|
207
|
+
|
|
208
|
+
.table-wrap {
|
|
209
|
+
overflow-x: auto;
|
|
210
|
+
padding: 0 22px 16px;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
.routes,
|
|
214
|
+
.env-table {
|
|
215
|
+
width: 100%;
|
|
216
|
+
border-collapse: collapse;
|
|
217
|
+
font-size: 14px;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
.routes caption {
|
|
221
|
+
text-align: left;
|
|
222
|
+
padding: 8px 0;
|
|
223
|
+
font-size: 13px;
|
|
224
|
+
color: var(--muted);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
.env-table thead,
|
|
228
|
+
.routes thead {
|
|
229
|
+
color: var(--muted);
|
|
230
|
+
font-size: 12px;
|
|
231
|
+
text-transform: uppercase;
|
|
232
|
+
letter-spacing: 0.6px;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
.routes th,
|
|
236
|
+
.env-table th {
|
|
237
|
+
text-align: left;
|
|
238
|
+
font-weight: 600;
|
|
239
|
+
padding: 6px 12px 6px 0;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
.routes td,
|
|
243
|
+
.env-table td {
|
|
244
|
+
padding: 9px 12px 9px 0;
|
|
245
|
+
border-top: 1px solid var(--line-soft);
|
|
246
|
+
white-space: nowrap;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
.env-name { font-family: var(--mono); font-weight: 600; }
|
|
250
|
+
|
|
251
|
+
.env-value { font-family: var(--mono); }
|
|
252
|
+
|
|
253
|
+
.env-source { font-family: var(--mono); color: var(--muted); }
|
|
254
|
+
|
|
255
|
+
.route-path { font-family: var(--mono); }
|
|
256
|
+
|
|
257
|
+
.prefix-dim { color: #71717a; }
|
|
258
|
+
|
|
259
|
+
.endpoint { font-family: var(--mono); color: var(--muted); }
|
|
260
|
+
|
|
261
|
+
.masked-badge {
|
|
262
|
+
margin-left: 8px;
|
|
263
|
+
font-family: var(--sans);
|
|
264
|
+
font-size: 11px;
|
|
265
|
+
font-weight: 600;
|
|
266
|
+
padding: 1px 8px;
|
|
267
|
+
border-radius: 999px;
|
|
268
|
+
background: #f4f4f5;
|
|
269
|
+
color: var(--muted);
|
|
270
|
+
border: 1px solid var(--line);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/* HTTP verbs */
|
|
274
|
+
|
|
275
|
+
.verb {
|
|
276
|
+
display: inline-block;
|
|
277
|
+
min-width: 58px;
|
|
278
|
+
text-align: center;
|
|
279
|
+
font-family: var(--mono);
|
|
280
|
+
font-size: 12px;
|
|
281
|
+
font-weight: 700;
|
|
282
|
+
padding: 2px 8px;
|
|
283
|
+
border-radius: 6px;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
.verb--get { background: #e0ecff; color: #1e40af; }
|
|
287
|
+
|
|
288
|
+
.verb--post { background: #dcfce7; color: #166534; }
|
|
289
|
+
|
|
290
|
+
.verb--put,
|
|
291
|
+
.verb--patch { background: #ffedd5; color: #9a3412; }
|
|
292
|
+
|
|
293
|
+
.verb--delete { background: #fee2e2; color: #991b1b; }
|
|
294
|
+
|
|
295
|
+
.verb--query { background: #ede9fe; color: #5b21b6; }
|
|
296
|
+
|
|
297
|
+
/* Environment disclosure inside each service */
|
|
298
|
+
|
|
299
|
+
.service__env { border-top: 1px solid var(--line-soft); }
|
|
300
|
+
|
|
301
|
+
.service__env-summary {
|
|
302
|
+
padding: 12px 22px;
|
|
303
|
+
font-size: 14px;
|
|
304
|
+
font-weight: 600;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
.footer {
|
|
308
|
+
justify-content: space-between;
|
|
309
|
+
gap: 8px;
|
|
310
|
+
padding-top: 16px;
|
|
311
|
+
border-top: 1px solid var(--line);
|
|
312
|
+
font-size: 13px;
|
|
313
|
+
color: var(--muted);
|
|
314
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Gateway
|
|
5
|
+
# The variables the panel lists: the solution's root files first, then what each project adds.
|
|
6
|
+
module EnvRows
|
|
7
|
+
Row = Data.define(:name, :value, :source, :masked)
|
|
8
|
+
|
|
9
|
+
PORT = "PORT"
|
|
10
|
+
PROCESS_SOURCE = "process"
|
|
11
|
+
BOTO_RUN_SOURCE = "boto run"
|
|
12
|
+
NO_ENV = Boto::Env.new({})
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def solution(env)
|
|
17
|
+
env = normalize(env)
|
|
18
|
+
rows(env, solution_keys(env))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Keys whose source is a file in the solution root: no "/" in the path and not a process-level source.
|
|
22
|
+
def solution_keys(env)
|
|
23
|
+
env = normalize(env)
|
|
24
|
+
env.file_keys.select { |key| root_file?(env.source(key)) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def project(env, solution_keys)
|
|
28
|
+
env = normalize(env)
|
|
29
|
+
rows(env, [PORT] | (env.file_keys - solution_keys))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# An app without an env (no .env files loaded) lists nothing.
|
|
33
|
+
def normalize(env) = env || NO_ENV
|
|
34
|
+
|
|
35
|
+
def root_file?(source)
|
|
36
|
+
source && !source.include?("/") && ![PROCESS_SOURCE, BOTO_RUN_SOURCE].include?(source)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def rows(env, keys)
|
|
40
|
+
keys.filter_map { |key| row(env, key) }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def row(env, key)
|
|
44
|
+
value = env[key]
|
|
45
|
+
return if value.nil?
|
|
46
|
+
|
|
47
|
+
shown, masked = Masking.apply(key, value)
|
|
48
|
+
Row.new(name: key, value: shown, source: env.source(key), masked: masked)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/mock_request"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Gateway
|
|
7
|
+
# Runs a service's own readiness probe in this process, so the panel shows what /health/ready says.
|
|
8
|
+
module Health
|
|
9
|
+
Result = Data.define(:status, :detail)
|
|
10
|
+
|
|
11
|
+
PROBE_PATH = "/health/ready"
|
|
12
|
+
HEALTH_PLUGIN = :health
|
|
13
|
+
OK = 200
|
|
14
|
+
DETAIL_LIMIT = 200
|
|
15
|
+
READY = :ready
|
|
16
|
+
FAILING = :failing
|
|
17
|
+
NONE = :none
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
def check(app, host: nil)
|
|
22
|
+
return Result.new(status: NONE, detail: nil) unless app.plugins.any? { |plugin| health_plugin?(plugin) }
|
|
23
|
+
|
|
24
|
+
probe(app, host)
|
|
25
|
+
rescue StandardError => error
|
|
26
|
+
failing("#{error.class}: #{error.message}")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def probe(app, host)
|
|
30
|
+
env = Rack::MockRequest.env_for(PROBE_PATH)
|
|
31
|
+
env["HTTP_HOST"] = host if host
|
|
32
|
+
status, _headers, body = app.call(env)
|
|
33
|
+
return Result.new(status: READY, detail: nil) if status == OK
|
|
34
|
+
|
|
35
|
+
failing(read_body(body))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def health_plugin?(plugin)
|
|
39
|
+
plugin.class.plugin_name == HEALTH_PLUGIN
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def failing(detail)
|
|
43
|
+
Result.new(status: FAILING, detail: detail)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def read_body(body)
|
|
47
|
+
text = +""
|
|
48
|
+
body.each { |chunk| text << chunk }
|
|
49
|
+
text[0, DETAIL_LIMIT]
|
|
50
|
+
ensure
|
|
51
|
+
body.close if body.respond_to?(:close)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "erb"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Gateway
|
|
7
|
+
# Renders the panel page from panel.html.erb. Every value goes through h (ERB::Util.html_escape).
|
|
8
|
+
class HtmlView
|
|
9
|
+
include ERB::Util
|
|
10
|
+
|
|
11
|
+
HEALTH_LABELS = { ready: "Ready", failing: "Not ready", none: "No health check" }.freeze
|
|
12
|
+
|
|
13
|
+
# Read as UTF-8 whatever the locale is: the page contains non-ASCII characters.
|
|
14
|
+
def self.template = ERB.new(File.read(Panel::TEMPLATE_FILE, encoding: Encoding::UTF_8), trim_mode: "-")
|
|
15
|
+
|
|
16
|
+
def initialize(template)
|
|
17
|
+
@template = template
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def render(page)
|
|
21
|
+
@template.result(binding)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def plural(count, word) = "#{count} #{count == 1 ? word : "#{word}s"}"
|
|
27
|
+
|
|
28
|
+
def health_label(status) = HEALTH_LABELS.fetch(status)
|
|
29
|
+
|
|
30
|
+
def verb_class(verb) = "verb verb--#{verb.downcase}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Gateway
|
|
7
|
+
# The panel's data as JSON, for /_boto/services.json. Same values as the HTML page.
|
|
8
|
+
module JsonView
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def render(page)
|
|
12
|
+
JSON.generate(document(page))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def document(page)
|
|
16
|
+
{ gateway: page.gateway, env: page.env_name, solution_env: page.solution_env.map { |row| row_hash(row) },
|
|
17
|
+
services: page.services.map { |service| service_hash(service, page) } }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def service_hash(service, page)
|
|
21
|
+
{ name: service.name, prefix: service.prefix, port: service.port,
|
|
22
|
+
health: { status: service.health.status.to_s, detail: service.health.detail },
|
|
23
|
+
routes: service.routes.map { |route| route_hash(route) },
|
|
24
|
+
openapi: page.link(service.openapi), swagger: service.swagger && page.link(service.swagger),
|
|
25
|
+
env: service.env.map { |row| row_hash(row) } }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def route_hash(route)
|
|
29
|
+
{ method: route.verb, path: route.path, endpoint: route.endpoint }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def row_hash(row)
|
|
33
|
+
{ name: row.name, value: row.value, source: row.source, masked: row.masked }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Gateway
|
|
7
|
+
# Hides secrets before the panel or its JSON shows a variable. Pure: it returns the value to show and a flag.
|
|
8
|
+
module Masking
|
|
9
|
+
MASK = "••••••••"
|
|
10
|
+
SENSITIVE_WORDS = %w[PASSWORD SECRET TOKEN KEY DSN CREDENTIAL].freeze
|
|
11
|
+
SENSITIVE_NAME = Regexp.new(SENSITIVE_WORDS.join("|"), Regexp::IGNORECASE).freeze
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# Returns [shown_value, masked]. The whole value goes for sensitive names; a URL keeps everything but its password.
|
|
16
|
+
def apply(name, value)
|
|
17
|
+
return [MASK, true] if SENSITIVE_NAME.match?(name)
|
|
18
|
+
|
|
19
|
+
url = mask_url_password(value)
|
|
20
|
+
return [url, true] if url
|
|
21
|
+
|
|
22
|
+
[value, false]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def mask_url_password(value)
|
|
26
|
+
password = url_password(value)
|
|
27
|
+
return if password.nil? || password.empty?
|
|
28
|
+
|
|
29
|
+
value.sub(":#{password}@") { ":#{MASK}@" }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# A value that is not a URI (spaces, stray %) has no password to hide.
|
|
33
|
+
def url_password(value)
|
|
34
|
+
URI.parse(value).password
|
|
35
|
+
rescue URI::InvalidURIError
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Gateway
|
|
5
|
+
# Decides the prefix of each referenced service and checks the result, once at boot.
|
|
6
|
+
class Mounts
|
|
7
|
+
ROOT = "/"
|
|
8
|
+
RESERVED_PREFIX = "/_boto"
|
|
9
|
+
ERROR_PREFIX = "gateway: "
|
|
10
|
+
|
|
11
|
+
# [service name, prefix, app] for every service, in the order the app received them.
|
|
12
|
+
attr_reader :entries
|
|
13
|
+
|
|
14
|
+
def initialize(services:, prefixes: {})
|
|
15
|
+
prefixes = prefixes.transform_keys(&:to_s) # app.use :gateway, prefixes: { users: "/contas" } works too
|
|
16
|
+
check_names!(services, prefixes)
|
|
17
|
+
@entries = services.map { |name, app| [name, prefix_for(name, prefixes), app] }.freeze
|
|
18
|
+
check_unique_prefixes!
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# The Hash that Rack::URLMap takes: prefix => app.
|
|
22
|
+
def apps_by_prefix
|
|
23
|
+
@entries.to_h { |_name, prefix, app| [prefix, app] }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def check_names!(services, prefixes)
|
|
29
|
+
unknown = prefixes.keys - services.keys
|
|
30
|
+
return if unknown.empty?
|
|
31
|
+
|
|
32
|
+
raise ArgumentError, "#{ERROR_PREFIX}prefixes names #{unknown.map(&:inspect).join(", ")}, which is not a ref"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def prefix_for(name, prefixes)
|
|
36
|
+
prefix = prefixes.fetch(name) { "#{ROOT}#{name}" }
|
|
37
|
+
reason = prefix_problem(prefix)
|
|
38
|
+
raise ArgumentError, "#{ERROR_PREFIX}prefix of #{name.inspect} #{reason}, got #{prefix.inspect}" if reason
|
|
39
|
+
|
|
40
|
+
prefix
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def prefix_problem(prefix)
|
|
44
|
+
return "must be a String" unless prefix.is_a?(String)
|
|
45
|
+
return "must start with \"/\"" unless prefix.start_with?(ROOT)
|
|
46
|
+
return "cannot be \"/\"" if prefix == ROOT
|
|
47
|
+
return "cannot end with \"/\"" if prefix.end_with?(ROOT)
|
|
48
|
+
return "cannot start with \"/_boto\", which is reserved for the panel" if prefix.start_with?(RESERVED_PREFIX)
|
|
49
|
+
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def check_unique_prefixes!
|
|
54
|
+
seen = {}
|
|
55
|
+
@entries.each do |name, prefix, _app|
|
|
56
|
+
raise ArgumentError, duplicate_message(name, prefix) if seen.key?(prefix)
|
|
57
|
+
|
|
58
|
+
seen[prefix] = name
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def duplicate_message(name, prefix)
|
|
63
|
+
"#{ERROR_PREFIX}prefix #{prefix.inspect} of #{name.inspect} is used by another service"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Gateway
|
|
5
|
+
# Everything the panel shows, built on each request so the health checks run at page load.
|
|
6
|
+
# base is the SCRIPT_NAME of the request, so links work behind a mount point.
|
|
7
|
+
Page = Data.define(:gateway, :env_name, :host, :base, :solution_env, :services) do
|
|
8
|
+
def self.build(gateway_app:, mounts:, env_name:, host:, base:)
|
|
9
|
+
solution_keys = EnvRows.solution_keys(gateway_app.env)
|
|
10
|
+
new(gateway: project_name(gateway_app.title), env_name: env_name, host: host, base: base,
|
|
11
|
+
solution_env: EnvRows.solution(gateway_app.env),
|
|
12
|
+
services: mounts.entries.map { |name, prefix, app| ServiceInfo.build(name, prefix, app, solution_keys, host: host) })
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# The namespace is the project name in CamelCase (Borda, LojaBorda); the panel shows the project name.
|
|
16
|
+
def self.project_name(namespace)
|
|
17
|
+
namespace.split("::").last.gsub(/([a-z\d])([A-Z])/, '\\1_\\2').downcase
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def link(path) = "#{base}#{path}"
|
|
21
|
+
|
|
22
|
+
def ready_count = services.count(&:ready?)
|
|
23
|
+
|
|
24
|
+
def failing_count = services.count(&:failing?)
|
|
25
|
+
|
|
26
|
+
def route_count = services.sum { |service| service.routes.size }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>Boto gateway panel</title>
|
|
7
|
+
<link rel="stylesheet" href="<%= h(page.link(Panel::CSS_PATH)) %>">
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<header class="topbar">
|
|
11
|
+
<div class="topbar__inner">
|
|
12
|
+
<div class="topbar__brand">
|
|
13
|
+
<svg width="28" height="28" viewBox="0 0 64 64" aria-hidden="true"><polygon points="16,10 48,10 60,24 4,24" fill="#e0473f"></polygon><polygon points="4,24 60,24 32,56" fill="#cc342d"></polygon><path d="M16 10 L24 24 L32 56 L40 24 L48 10" stroke="#ffffff" stroke-opacity="0.45" stroke-width="1.5" stroke-linecap="round" fill="none"></path></svg>
|
|
14
|
+
<span class="brand">Boto</span>
|
|
15
|
+
<span class="muted">/</span>
|
|
16
|
+
<span class="gateway-name">gateway <%= h(page.gateway) %></span>
|
|
17
|
+
<span class="env-badge"><%= h(page.env_name) %></span>
|
|
18
|
+
</div>
|
|
19
|
+
<nav class="topbar__nav" aria-label="Panel links">
|
|
20
|
+
<span class="host"><%= h(page.host) %></span>
|
|
21
|
+
<a href="<%= h(page.link(Panel::JSON_PATH)) %>">services.json</a>
|
|
22
|
+
</nav>
|
|
23
|
+
</div>
|
|
24
|
+
</header>
|
|
25
|
+
|
|
26
|
+
<main class="panel">
|
|
27
|
+
<section class="summary" aria-label="Summary">
|
|
28
|
+
<div class="stat"><div class="stat__label">Services</div><div class="stat__value"><%= page.services.size %></div></div>
|
|
29
|
+
<div class="stat"><div class="stat__label">Ready</div><div class="stat__value stat__value--ready"><%= page.ready_count %></div></div>
|
|
30
|
+
<div class="stat"><div class="stat__label">Not ready</div><div class="stat__value stat__value--failing"><%= page.failing_count %></div></div>
|
|
31
|
+
<div class="stat"><div class="stat__label">Routes</div><div class="stat__value"><%= page.route_count %></div></div>
|
|
32
|
+
</section>
|
|
33
|
+
|
|
34
|
+
<section class="card" aria-labelledby="solution-env-title">
|
|
35
|
+
<details open>
|
|
36
|
+
<summary class="card__summary">
|
|
37
|
+
<h2 id="solution-env-title" class="card__title">Solution environment</h2>
|
|
38
|
+
<span class="muted"><%= plural(page.solution_env.size, "variable") %> · loaded into ENV, shared by every project</span>
|
|
39
|
+
</summary>
|
|
40
|
+
<div class="table-wrap">
|
|
41
|
+
<table class="env-table">
|
|
42
|
+
<thead>
|
|
43
|
+
<tr><th scope="col">Name</th><th scope="col">Value</th><th scope="col">Source</th></tr>
|
|
44
|
+
</thead>
|
|
45
|
+
<tbody>
|
|
46
|
+
<% page.solution_env.each do |row| %>
|
|
47
|
+
<tr>
|
|
48
|
+
<td class="env-name"><%= h(row.name) %></td>
|
|
49
|
+
<td class="env-value"><span><%= h(row.value) %></span><% if row.masked %> <span class="masked-badge">masked</span><% end %></td>
|
|
50
|
+
<td class="env-source"><%= h(row.source) %></td>
|
|
51
|
+
</tr>
|
|
52
|
+
<% end %>
|
|
53
|
+
</tbody>
|
|
54
|
+
</table>
|
|
55
|
+
</div>
|
|
56
|
+
</details>
|
|
57
|
+
</section>
|
|
58
|
+
|
|
59
|
+
<section class="services" aria-labelledby="services-title">
|
|
60
|
+
<div class="section-head">
|
|
61
|
+
<h1 id="services-title" class="section-title">Services</h1>
|
|
62
|
+
<span class="muted">Health checked in-process at page load</span>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
<% page.services.each_with_index do |service, index| %>
|
|
66
|
+
<article class="service">
|
|
67
|
+
<div class="service__head">
|
|
68
|
+
<div class="service__identity">
|
|
69
|
+
<h2 class="service__name"><%= h(service.name) %></h2>
|
|
70
|
+
<code class="prefix"><%= h(service.prefix) %></code>
|
|
71
|
+
<% if service.port %>
|
|
72
|
+
<a class="port-link" href="<%= h("http://localhost:#{service.port}") %>">localhost:<%= h(service.port) %></a>
|
|
73
|
+
<span class="muted"><%= service.random_port ? "random port" : "fixed PORT" %></span>
|
|
74
|
+
<% end %>
|
|
75
|
+
</div>
|
|
76
|
+
<div class="service__status">
|
|
77
|
+
<span class="health health--<%= h(service.health.status) %>"><% case service.health.status %><% when :ready %><svg width="14" height="14" viewBox="0 0 16 16" aria-hidden="true"><circle cx="8" cy="8" r="7" fill="#16a34a"></circle><path d="M5 8.2 L7.1 10.2 L11 6" stroke="#ffffff" stroke-width="1.8" fill="none" stroke-linecap="round" stroke-linejoin="round"></path></svg><% when :failing %><svg width="14" height="14" viewBox="0 0 16 16" aria-hidden="true"><circle cx="8" cy="8" r="7" fill="#dc2626"></circle><path d="M5.5 5.5 L10.5 10.5 M10.5 5.5 L5.5 10.5" stroke="#ffffff" stroke-width="1.8" stroke-linecap="round"></path></svg><% else %><svg width="14" height="14" viewBox="0 0 16 16" aria-hidden="true"><circle cx="8" cy="8" r="6.2" stroke="#71717a" stroke-width="1.6" fill="none"></circle><path d="M5.5 8 H10.5" stroke="#71717a" stroke-width="1.6" stroke-linecap="round"></path></svg><% end %><%= health_label(service.health.status) %></span>
|
|
78
|
+
<a class="link-strong" href="<%= h(page.link(service.openapi)) %>">OpenAPI</a>
|
|
79
|
+
<% if service.swagger %>
|
|
80
|
+
<a class="link-strong" href="<%= h(page.link(service.swagger)) %>">Swagger</a>
|
|
81
|
+
<% end %>
|
|
82
|
+
</div>
|
|
83
|
+
</div>
|
|
84
|
+
|
|
85
|
+
<% if service.failing? %>
|
|
86
|
+
<div class="health-detail"><%= h(service.health.detail) %></div>
|
|
87
|
+
<% end %>
|
|
88
|
+
|
|
89
|
+
<div class="table-wrap">
|
|
90
|
+
<table class="routes">
|
|
91
|
+
<caption><%= plural(service.routes.size, "route") %></caption>
|
|
92
|
+
<thead>
|
|
93
|
+
<tr><th scope="col">Method</th><th scope="col">Path</th><th scope="col">Endpoint</th></tr>
|
|
94
|
+
</thead>
|
|
95
|
+
<tbody>
|
|
96
|
+
<% service.routes.each do |route| %>
|
|
97
|
+
<tr>
|
|
98
|
+
<td><span class="<%= verb_class(route.verb) %>"><%= h(route.verb) %></span></td>
|
|
99
|
+
<td class="route-path"><span class="prefix-dim"><%= h(service.prefix) %></span><%= h(route.path) %></td>
|
|
100
|
+
<td class="endpoint"><%= h(route.endpoint) %></td>
|
|
101
|
+
</tr>
|
|
102
|
+
<% end %>
|
|
103
|
+
</tbody>
|
|
104
|
+
</table>
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
<details class="service__env"<% if index.zero? %> open<% end %>>
|
|
108
|
+
<summary class="service__env-summary">Environment <span class="muted">· <%= plural(service.env.size, "variable") %> · app.env, this project only</span></summary>
|
|
109
|
+
<div class="table-wrap">
|
|
110
|
+
<table class="env-table">
|
|
111
|
+
<thead>
|
|
112
|
+
<tr><th scope="col">Name</th><th scope="col">Value</th><th scope="col">Source</th></tr>
|
|
113
|
+
</thead>
|
|
114
|
+
<tbody>
|
|
115
|
+
<% service.env.each do |row| %>
|
|
116
|
+
<tr>
|
|
117
|
+
<td class="env-name"><%= h(row.name) %></td>
|
|
118
|
+
<td class="env-value"><span><%= h(row.value) %></span><% if row.masked %> <span class="masked-badge">masked</span><% end %></td>
|
|
119
|
+
<td class="env-source"><%= h(row.source) %></td>
|
|
120
|
+
</tr>
|
|
121
|
+
<% end %>
|
|
122
|
+
</tbody>
|
|
123
|
+
</table>
|
|
124
|
+
</div>
|
|
125
|
+
</details>
|
|
126
|
+
</article>
|
|
127
|
+
<% end %>
|
|
128
|
+
</section>
|
|
129
|
+
|
|
130
|
+
<footer class="footer">
|
|
131
|
+
<span>boto-rb-gateway <%= h(Boto::Gateway::VERSION) %></span>
|
|
132
|
+
<span>This panel is on in development only</span>
|
|
133
|
+
</footer>
|
|
134
|
+
</main>
|
|
135
|
+
</body>
|
|
136
|
+
</html>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Gateway
|
|
5
|
+
# Rack app for the panel under /_boto. Read-only: GET and HEAD, the page is built on each request.
|
|
6
|
+
class Panel
|
|
7
|
+
PATH = "/_boto"
|
|
8
|
+
HTML_PATH = PATH
|
|
9
|
+
JSON_PATH = "/_boto/services.json"
|
|
10
|
+
CSS_PATH = "/_boto/panel.css"
|
|
11
|
+
STYLESHEET_FILE = File.join(__dir__, "assets", "panel.css")
|
|
12
|
+
TEMPLATE_FILE = File.join(__dir__, "panel.html.erb")
|
|
13
|
+
READ_METHODS = %w[GET HEAD].freeze
|
|
14
|
+
ALLOW = "GET, HEAD"
|
|
15
|
+
HTML_TYPE = "text/html; charset=utf-8"
|
|
16
|
+
JSON_TYPE = "application/json; charset=utf-8"
|
|
17
|
+
CSS_TYPE = "text/css; charset=utf-8"
|
|
18
|
+
TEXT_TYPE = "text/plain; charset=utf-8"
|
|
19
|
+
NO_STORE = "no-store"
|
|
20
|
+
|
|
21
|
+
def initialize(gateway:, mounts:, env_name:, stylesheet: File.read(STYLESHEET_FILE, encoding: Encoding::UTF_8),
|
|
22
|
+
html: HtmlView.new(HtmlView.template))
|
|
23
|
+
@gateway = gateway
|
|
24
|
+
@mounts = mounts
|
|
25
|
+
@env_name = env_name
|
|
26
|
+
@stylesheet = stylesheet.freeze
|
|
27
|
+
@html = html
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def call(env)
|
|
31
|
+
return method_not_allowed unless READ_METHODS.include?(env["REQUEST_METHOD"])
|
|
32
|
+
|
|
33
|
+
case env["PATH_INFO"]
|
|
34
|
+
when HTML_PATH then html(env)
|
|
35
|
+
when JSON_PATH then json(env)
|
|
36
|
+
when CSS_PATH then [200, { "content-type" => CSS_TYPE }, [@stylesheet]]
|
|
37
|
+
else text(404, "Not Found")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def html(env)
|
|
44
|
+
[200, { "content-type" => HTML_TYPE, "cache-control" => NO_STORE }, [@html.render(page(env))]]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def json(env)
|
|
48
|
+
[200, { "content-type" => JSON_TYPE, "cache-control" => NO_STORE }, [JsonView.render(page(env))]]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def page(env)
|
|
52
|
+
Page.build(gateway_app: @gateway, mounts: @mounts, env_name: @env_name,
|
|
53
|
+
host: env["HTTP_HOST"], base: env["SCRIPT_NAME"].to_s)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def method_not_allowed
|
|
57
|
+
[405, { "content-type" => TEXT_TYPE, "allow" => ALLOW }, ["Method Not Allowed"]]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def text(status, body)
|
|
61
|
+
[status, { "content-type" => TEXT_TYPE }, [body]]
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Gateway
|
|
5
|
+
# What the panel shows of one service. Route paths have no prefix; the prefix is added to the links.
|
|
6
|
+
ServiceInfo = Data.define(:name, :prefix, :port, :random_port, :health, :routes, :openapi, :swagger, :env)
|
|
7
|
+
|
|
8
|
+
class ServiceInfo
|
|
9
|
+
Route = Data.define(:verb, :path, :endpoint)
|
|
10
|
+
|
|
11
|
+
OPENAPI_PATH = Boto::App::OPENAPI_PATH
|
|
12
|
+
OPENAPI_PLUGIN = :openapi
|
|
13
|
+
SWAGGER_PLUGIN = :swagger_ui
|
|
14
|
+
|
|
15
|
+
# host: the panel request's host, so a service with :hosts accepts the probe as it accepts the panel.
|
|
16
|
+
def self.build(name, prefix, app, solution_keys, host: nil)
|
|
17
|
+
new(name: name, prefix: prefix, port: app.port, random_port: random_port?(app),
|
|
18
|
+
health: Health.check(app, host: host), routes: routes_of(app),
|
|
19
|
+
openapi: "#{prefix}#{openapi_path(app)}", swagger: swagger_link(app, prefix),
|
|
20
|
+
env: EnvRows.project(app.env, solution_keys))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.random_port?(app)
|
|
24
|
+
app.env&.source(EnvRows::PORT) == EnvRows::BOTO_RUN_SOURCE
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.routes_of(app)
|
|
28
|
+
app.endpoints.map do |endpoint|
|
|
29
|
+
verb, path = endpoint.route
|
|
30
|
+
Route.new(verb: verb, path: path, endpoint: endpoint.name)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The OpenAPI plugin keeps its own path; without it the core serves the default in development.
|
|
35
|
+
def self.openapi_path(app)
|
|
36
|
+
plugin = plugin_named(app, OPENAPI_PLUGIN)
|
|
37
|
+
plugin ? plugin.path : OPENAPI_PATH
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.swagger_link(app, prefix)
|
|
41
|
+
plugin = plugin_named(app, SWAGGER_PLUGIN)
|
|
42
|
+
"#{prefix}#{plugin.path}" if plugin
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.plugin_named(app, name)
|
|
46
|
+
app.plugins.find { |plugin| plugin.class.plugin_name == name }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def ready? = health.status == Health::READY
|
|
50
|
+
|
|
51
|
+
def failing? = health.status == Health::FAILING
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/boto/gateway.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "boto"
|
|
4
|
+
require "zeitwerk"
|
|
5
|
+
|
|
6
|
+
loader = Zeitwerk::Loader.for_gem_extension(Boto)
|
|
7
|
+
# Template declarations run through Boto::Template.define when the registry loads; they are not constants.
|
|
8
|
+
loader.ignore("#{__dir__}/templates")
|
|
9
|
+
loader.setup
|
|
10
|
+
|
|
11
|
+
module Boto
|
|
12
|
+
# Namespace of the gem; its plugins and templates are loaded from lib/boto/.
|
|
13
|
+
module Gateway
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/urlmap"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Plugins
|
|
7
|
+
# Mounts the referenced HTTP services under their prefixes in this process, so one port serves the solution.
|
|
8
|
+
# Runs before routing: a path no prefix claims falls through to the gateway's own endpoints.
|
|
9
|
+
class Gateway < Boto::Plugin
|
|
10
|
+
scope :global
|
|
11
|
+
|
|
12
|
+
PRODUCTION = "production"
|
|
13
|
+
NOT_IN_PRODUCTION = "gateway runs in development and test only for now; in production, route with your ingress"
|
|
14
|
+
CASCADE_HEADER = "x-cascade"
|
|
15
|
+
CASCADE_PASS = "pass"
|
|
16
|
+
BOOLEANS = [true, false].freeze
|
|
17
|
+
|
|
18
|
+
attr_reader :app, :mounts
|
|
19
|
+
|
|
20
|
+
def attach(app)
|
|
21
|
+
raise ArgumentError, NOT_IN_PRODUCTION if env_name == PRODUCTION
|
|
22
|
+
|
|
23
|
+
@app = app
|
|
24
|
+
@mounts = Boto::Gateway::Mounts.new(services: app.services, prefixes: @prefixes)
|
|
25
|
+
@router = Rack::URLMap.new(@mounts.apps_by_prefix)
|
|
26
|
+
@panel = Boto::Gateway::Panel.new(gateway: app, mounts: @mounts, env_name: env_name) if panel?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Panel defaults to development only; an explicit true or false wins.
|
|
30
|
+
def panel?
|
|
31
|
+
@panel.nil? ? development? : @panel
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def call(env, app)
|
|
35
|
+
return @panel.call(env) if panel_request?(env)
|
|
36
|
+
|
|
37
|
+
response = @router.call(env)
|
|
38
|
+
return response unless cascade?(response)
|
|
39
|
+
|
|
40
|
+
app.call(env)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
# Checked before the URLMap so a service can never claim the panel namespace.
|
|
46
|
+
def panel_request?(env)
|
|
47
|
+
return false unless @panel
|
|
48
|
+
|
|
49
|
+
path = env["PATH_INFO"].to_s
|
|
50
|
+
path == Boto::Gateway::Panel::PATH || path.start_with?("#{Boto::Gateway::Panel::PATH}/")
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def configure(prefixes: {}, panel: nil)
|
|
54
|
+
@prefixes = validate_prefixes(prefixes)
|
|
55
|
+
@panel = validate_panel(panel)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def validate_prefixes(prefixes)
|
|
59
|
+
return prefixes if prefixes.is_a?(Hash)
|
|
60
|
+
|
|
61
|
+
raise ArgumentError, "gateway: prefixes must be a Hash of service name to prefix, got #{prefixes.inspect}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def validate_panel(panel)
|
|
65
|
+
return panel if panel.nil? || BOOLEANS.include?(panel)
|
|
66
|
+
|
|
67
|
+
raise ArgumentError, "gateway: panel must be true, false or nil, got #{panel.inspect}"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# URLMap answers 404 with x-cascade: pass when no prefix matches; that is the only case that goes to the gateway.
|
|
71
|
+
def cascade?(response)
|
|
72
|
+
response[1][CASCADE_HEADER] == CASCADE_PASS
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class <%= namespace %>GatewayTest < Minitest::Test
|
|
6
|
+
def test_live_responds_ok_without_token
|
|
7
|
+
response = Rack::MockRequest.new(APP).get("/health/live")
|
|
8
|
+
assert_equal 200, response.status
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "minitest/autorun"
|
|
4
|
+
require "rack/mock"
|
|
5
|
+
require "boto"
|
|
6
|
+
|
|
7
|
+
SOLUTION_ROOT = File.expand_path("../../..", __dir__)
|
|
8
|
+
BOOT = Boto::Boot.new(Boto::Botofile.load(File.join(SOLUTION_ROOT, "Botofile")), root: SOLUTION_ROOT)
|
|
9
|
+
APP = BOOT.app("<%= name %>", env_name: "test")
|
metadata
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: boto-rb-gateway
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.18.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Evo Tech Builder
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: boto-rb
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - '='
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.18.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - '='
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.18.0
|
|
26
|
+
executables: []
|
|
27
|
+
extensions: []
|
|
28
|
+
extra_rdoc_files: []
|
|
29
|
+
files:
|
|
30
|
+
- LICENSE
|
|
31
|
+
- README.md
|
|
32
|
+
- lib/boto/gateway.rb
|
|
33
|
+
- lib/boto/gateway/assets/panel.css
|
|
34
|
+
- lib/boto/gateway/env_rows.rb
|
|
35
|
+
- lib/boto/gateway/health.rb
|
|
36
|
+
- lib/boto/gateway/html_view.rb
|
|
37
|
+
- lib/boto/gateway/json_view.rb
|
|
38
|
+
- lib/boto/gateway/masking.rb
|
|
39
|
+
- lib/boto/gateway/mounts.rb
|
|
40
|
+
- lib/boto/gateway/page.rb
|
|
41
|
+
- lib/boto/gateway/panel.html.erb
|
|
42
|
+
- lib/boto/gateway/panel.rb
|
|
43
|
+
- lib/boto/gateway/service_info.rb
|
|
44
|
+
- lib/boto/gateway/version.rb
|
|
45
|
+
- lib/boto/plugins/gateway.rb
|
|
46
|
+
- lib/boto/templates/gateway/files/config/app.rb.erb
|
|
47
|
+
- lib/boto/templates/gateway/files/test/config/gateway_test.rb.erb
|
|
48
|
+
- lib/boto/templates/gateway/files/test/test_helper.rb.erb
|
|
49
|
+
- lib/boto/templates/gateway/template.rb
|
|
50
|
+
homepage: https://github.com/EvoTechBuilder/boto-rb
|
|
51
|
+
licenses:
|
|
52
|
+
- MIT
|
|
53
|
+
metadata:
|
|
54
|
+
rubygems_mfa_required: 'true'
|
|
55
|
+
source_code_uri: https://github.com/EvoTechBuilder/boto-rb
|
|
56
|
+
changelog_uri: https://github.com/EvoTechBuilder/boto-rb/blob/main/CHANGELOG.md
|
|
57
|
+
rdoc_options: []
|
|
58
|
+
require_paths:
|
|
59
|
+
- lib
|
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '3.4'
|
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
70
|
+
requirements: []
|
|
71
|
+
rubygems_version: 4.0.20
|
|
72
|
+
specification_version: 4
|
|
73
|
+
summary: Single entry point for a Boto solution in development, with the /_boto panel.
|
|
74
|
+
test_files: []
|