-
Notifications
You must be signed in to change notification settings - Fork 1
/
oJobHTTPd.yaml
469 lines (438 loc) · 17.1 KB
/
oJobHTTPd.yaml
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# Copyright 2023 Nuno Aguiar
jobs:
# Prepares a HTTP server
#
# ------------------------
- name : HTTP Start Server
typeArgs:
noLog: false
shortcut:
name : httpdStart
keyArg: port
args :
hs : hs
keystore: keystore
pass : pass
host : host
cp : cp
mapLibs : mapLibs
help :
text : Provides and starts a HTTP server.
expects:
- name: port
desc: (Number) The port where the server should be made available (default is 8091)
- name: hs
desc: (HTTPServer) A HTTPServer to use (is not provided one will be created)
- name: keystore
desc: (String) Optionally to create a https server provide a keystore file
- name: pass
desc: (String) The password for the keystore of a https server
- name: host
desc: (String) Optionally the interface where the server should be listening (default is all)
- name: cp
desc: (String) Optionally providing the classpath URL to include the keystore file
- name: mapLibs
desc: (boolean) Map also internal libs (e.g. jquery, css, etc...)
exec : |
ow.loadServer()
var ks
if (isUnDef(global.__ojobHttp)) global.__ojobHttp = {}
if (isUnDef(global.__ojobRoutes)) global.__ojobRoutes = {}
if (isUnDef(args.port)) args.port = 8091
if (isUnDef(args.cp) && isDef(args.keystore) && !isNull(java.io.File(args.keystore).getParent())) args.cp = java.io.File(args.keystore).getParent().toString()
if (isDef(args.cp)) af.externalAddClasspath(args.cp)
if (isDef(args.keystore)) ks = args.keystore
args.mapLibs = _$(args.mapLibs).isBoolean().default(false)
if (isUnDef(global.__ojobHttp[args.port])) global.__ojobHttp[args.port] = (isDef(args.hs) ? hs : ow.server.httpd.start(args.port, args.host, ks, args.pass))
if (args.mapLibs) {
global.__ojobRoutes[args.port] = ow.server.httpd.mapRoutesWithLibs(global.__ojobHttp[args.port], {})
} else {
global.__ojobRoutes[args.port] = {}
}
# Applies a HTTP service definition
#
# -------------------------
- name : HTTP Apply Service
exec : |
if (isUnDef(args.port)) args.port = 8091
ow.server.httpd.route(global.__ojobHttp[args.port], global.__ojobRoutes[args.port], function(r) { return hs.reply("", "", 401, {}); })
log("READY [" + args.port + "]")
# Stopping the HTTP server
#
# ----------------------
- name: HTTP Stop Server
type: shutdown
typeArgs:
noLog: false
shortcut:
name : httpdStop
keyArg: port
args : {}
help:
text : Sets a shutdown job for the server. If using sequential do ensure that the job 'HTTP Start Server' gets executed first.
expects:
- name: port
desc: (Number) The port where the server was made available (default is 8091)
deps:
- HTTP Start Server
exec: |
if (isUnDef(args.port)) args.port = 8091
ow.server.httpd.stop(global.__ojobHttp[args.port])
delete global.__ojobHttp[args.port]
delete global.__ojobRoutes[args.port]
log("STOPPED [" + args.port + "]")
# Defines a HTTP service
#
# -------------------
- name : HTTP Service
deps :
- HTTP Start Server
to : HTTP Apply Service
typeArgs:
noLog: false
shortcut:
name : httpdService
keyArg: port
args :
uri : uri
execURI : execURI
audit : audit
auditTemplate: auditTemplate
error : error
help :
text: Provides a HTTP service definition. If using sequential jobs do ensure that the job 'HTTP Start Server' gets executed first.
expects:
- name: port
desc: (Number) The port where the server was made available (default is 8091)
- name: uri
desc: (String) The URI where the HTTP service will be available.
- name: execURI
desc: (Function) The URI execution function. Receives a request map and a server object.
- name: audit
desc: (Boolean) Turns request audit logging on (default true)
- name: auditTemplate
desc: (String) Provide an audit logging template based on the request argument.
- name: error
desc: (Boolean) If true will return the error in case of exception.
exec : |
args.port = _$(args.port).isNumber().default(8091)
args.uri = _$(args.uri).isString().default("/")
args.audit = _$(args.audit).default(true)
args.auditTemplate = _$(args.auditTemplate).default("AUDIT HTTP | {{method}} {{uri}} {{reply.status}} {{{reply.mimetype}}} ({{header.remote-addr}}; {{header.user-agent}})")
var fn = __
if (args.audit) {
fn = (req, reply) => {
var data = merge(req, {
reply: {
status : reply.status,
mimetype: reply.mimetype
}
})
try {
tlog(args.auditTemplate, data)
} catch(e) {
logErr("Error on auditing access: " + String(e))
}
}
}
global.__ojobRoutes[args.port][args.uri] = function(r) {
try {
var res = (new Function("var request = arguments[0]; var server = global.__ojobHttp[" + args.port + "]; var port = " + String(args.port) + "; var uri = '" + args.uri + "'; " + args.execURI))(r)
if (!isMap(res)) res = ow.server.httpd.reply(res, void 0, void 0, {})
fn(r, res)
return res
} catch(e) {
if (args.error) {
return { data: String(e), mimetype: "text/plain", status: 500, header: {} }
}
}
}
# -------------------
- name : HTTP Default
deps :
- HTTP Start Server
to : HTTP Service
typeArgs:
noLog: false
shortcut:
name : httpdDefault
keyArg: port
args :
uri : uri
code : code
audit : audit
auditTemplate: auditTemplate
error : error
help :
text : Provides a HTTP default handler.
expects:
- name: port
desc: (Number) The port where the server was made available (default is 8091)
- name: uri
desc: (String) The URI where the HTTP default response will be available (defaults to /)
- name: code
desc: (Number) The default HTTP status code to reply (default is 401)
- name: audit
desc: (Boolean) Turns request audit logging on (default true)
- name: auditTemplate
desc: (String) Provide an audit logging template based on the request argument.
- name: error
desc: (Boolean) If true will return the error in case of exception.
exec : |
args.code = _$(args.code, "code").isNumber().default(401)
args.uri = _$(args.uri, "uri").isString().default("/")
args.execURI = "return ow.server.httpd.reply(\"\", " + args.code + ", __, {});"
# ------------------
- name : HTTP Upload
deps :
- HTTP Start Server
to : HTTP Service
typeArgs:
noLog: false
shortcut:
name : httpdUpload
keyArg: port
args :
uri : uri
path : path
execURI : execURI
audit : audit
auditTemplate: auditTemplate
error : error
help :
text : Provides a HTTP upload handler.
expects:
- name: port
desc: (Number) The port where the server was made available (default is 8091)
- name: uri
desc: (String) The URI where the HTTP default response will be available (defaults to /upload)
- name: path
desc: (String) The local path where uploaded files will be saved
- name: execURI
desc: (Function) The URI execution function. Receives a request map and a server object. If doesn't return any value it will proceed to save the file on the corresponding path after code execution.
- name: audit
desc: (Boolean) Turns request audit logging on (default true)
- name: auditTemplate
desc: (String) Provide an audit logging template based on the request argument.
- name: error
desc: (Boolean) If true will return the error in case of exception.
args :
eURI: |
if (isDef(request.params) && isDef(request.params.data) && request.method == "POST") {
log("Received file '" + request.params.data + "'...")
io.cp(request.files.data, '{{{path}}}/' + request.params.data)
return "ok"
} else {
return server.replyOKHTML("<html><body><form action=\"" + request.uri + "\" enctype=\"multipart/form-data\" method=\"POST\"><input type=\"file\" name=\"data\" value=\"data\" id=\"data\"/><input type=\"submit\" value=\"submit\"/></form></body></html>")
}
exec: |
args.path = _$(args.path, "path").default(".")
args.uri = _$(args.uri).isString().default("/upload")
io.mkdir(args.path)
args.execURI = (isDef(args.execURI) ? args.execURI : "") + "\n" + templify(args.eURI, { path: args.path })
# -------------------
- name : HTTP Healthz
deps :
- HTTP Start Server
to : HTTP Service
typeArgs:
noLog: false
shortcut:
name : httpdHealthz
keyArg: port
args :
uri : uri
path : path
healthz : healthz
openmetrics: openmetrics
help :
text: Adds a healthz URI
expects:
- name: port
desc: The port of the HTTP Start Server (default to 8091)
- name: uri
desc: The uri to use (default /healthz)
- name: healthz
desc: A custom function to return the /healthz result (defaults to ow.metrics.getAll with status 200 or "error" with status 500 if an exception is thrown)
- name: openmetrics
desc: If openmetrics = true the output will be in openmetrics/prometheus instead of json
exec: |
ow.loadMetrics()
args.port = _$(args.port).isNumber().default(8091)
args.uri = _$(args.uri).isString().default("/healthz")
var s1 = " try { return ow.server.httpd.reply(ow.metrics.getAll()); } catch(e) { return ow.server.httpd.reply(\"error\", 500, void 0, {}); } "
var s2 = " try { return ow.metrics.fromObj2OpenMetrics(ow.metrics.getAll()); } catch(e) { return ow.server.httpd.reply(\"error\", 500, void 0, {}); } "
args.healthz = _$(args.healthz).isString().default(args.openmetrics ? s2 : s1)
args.execURI = args.healthz
# ------------------
- name : HTTP Metrics
deps :
- HTTP Start Server
to : HTTP Service
typeArgs:
noLog: false
shortcut:
name : httpdMetrics
keyArg: port
args :
uri : uri
metrics: metrics
prefix : prefix
helpmap: helpmap
help :
text: Adds a metrics URI with a OpenMetrics/Prometheus output
expects:
- name: port
desc: The port of the HTTP Start Server (default to 8091)
- name: uri
desc: The uri to use (default /metrics)
- name: metrics
desc: A custom function to return the /metrics result (defaults to ow.metrics.getAll with status 200 or "error" with status 500 if an exception is thrown).
- name: prefix
desc: OpenMetrics/Prometheus metrics' name prefix
- name: helpmap
desc: OpenMetrics/Prometheus metrics' help map (see help ow.metrics.fromObj2OpenMetrics)
exec : |
ow.loadMetrics()
args.port = _$(args.port).isNumber().default(8091)
args.uri = _$(args.uri).isString().default("/metrics")
args.metrics = _$(args.metrics).isString().default("try { var rrr = ow.server.httpd.reply(ow.metrics.fromObj2OpenMetrics((isDef(request.params.s) ? ow.metrics.getSome(request.params.s.split(\",\")) : ow.metrics.getAll()), " + stringify(args.prefix) + ", now(), " + stringify(args.helpmap,__,'') + ")); rrr.data = String(rrr.data); return rrr; } catch(e) { logErr(e); return { data: \"error\", status: 500, mimetype: \"text/plain\" }; } ")
args.execURI = args.metrics
# ----------------------
- name : HTTP Add Metric
typeArgs:
noLog: false
shortcut:
name : httpdAddMetric
keyArg: metric
args :
fn: fn
help :
text : Add a metric to the current daemon execution
expects:
- name: metric
desc: Metric name
- name: fn
desc: Metric function code returning a map
exec: |
_$(args.metric, "metric").$_()
_$(args.fn, "fn").$_()
ow.loadMetrics()
ow.metrics.add(args.metric, new Function(args.fn))
# Defines a classic HTTP file browse
#
# ---------------------------------
- name : HTTP Classical File Browse
deps :
- HTTP Start Server
typeArgs:
noLog: false
shortcut:
name : httpdClassicFileBrowse
keyArg: port
args :
uri : uri
path: path
help :
text : Provides a classical HTTP file browse definition.
expects:
- name: port
desc: (Number) The port where the server was made available (default is 8091)
- name: uri
desc: (String) The URI where the HTTP File Browse will be available.
- name: path
desc: (String) The canonical path (real path) to the folder that contains the file to be browsed.
exec: |
args.port = _$(args.port).isNumber().default(8091)
args.uri = _$(args.uri).isString().default("/")
_$(args.path).isString().$_("You need to provide a path.")
global.__ojobHttp[args.port].addFileBrowse(args.uri, args.path)
# Defines a HTTP file browse
#
# -----------------------
- name : HTTP File Browse
deps :
- HTTP Start Server
to : HTTP Service
typeArgs:
noLog: false
shortcut:
name : httpdFileBrowse
keyArg: port
args :
uri : uri
path : path
browse: browse
help :
text : Provides a HTTP file browse definition.
expects:
- name: port
desc: (Number) The port where the server was made available (default is 8091)
- name: uri
desc: (String) The URI where the HTTP File Browse will be available.
- name: path
desc: (String) The canonical path (real path) to the folder that contains the file to be browsed.
- name: browse
desc: (String) If "false" no browsing interface is included.
args :
execURI : |
var shouldBrowse = global.__MHTTPD[String(port) + uri].BROWSE
var path = global.__MHTTPD[String(port) + uri].PATH
var tmplb = global.__MHTTPD[String(port) + uri].TMPLB
var tmplh = global.__MHTTPD[String(port) + uri].TMPLH
var content = ""
var puri = String(request.uri).replace(new RegExp("^" + uri), (uri == "/" ? "" : "/")).replace(/\/+/g, "/")
var furi = String((new java.io.File(new java.io.File(String((new java.io.File(path + "/" + puri)).getCanonicalFile()).replace(/\\+/g, "/")))))
var finfo
try {
finfo = io.fileInfo(furi)
} catch(e) {
return server.replyOKHTML("")
}
if (finfo.isDirectory) {
if (shouldBrowse) {
content += tmplh({ puri: (puri == "" ? "/" : puri) })
content += "| Filename | Last modified | Size | Size in bytes |\n"
content += "|----------|:-------------:|-----:|--------------:|\n"
if (puri != "/" && puri != "")
content += "| __[..](" + uri + puri.replace(/[^\/]+\/$/, "") + ")__ | | |\n"
var theList = $from(io.listFiles(furi).files).sort("-isDirectory", "filename").select((f) => {
return merge(f, {
fLastModified: ow.format.fromDate(new Date(f.lastModified), 'yyyy-MM-dd HH:mm:ss'),
fSize : ow.format.toBytesAbbreviation(f.size),
puri : puri,
uri : uri
});
});
content += tmplb(theList)
}
} else {
if (request.uri.endsWith(".md")) {
return ow.server.httpd.replyFileMD(server, path, uri, request.uri)
} else {
if (request.uri.endsWith(".adoc") && isDef(getOPackPath("Asciidoc"))) {
loadLib("asciidoc.js")
return ow.server.httpd.replyAsciidoc(server, path, uri, request.uri)
} else {
return ow.server.httpd.replyFile(server, path, uri, request.uri)
}
}
}
if (shouldBrowse)
return server.replyOKHTML(ow.template.parseMD2HTML(content, true))
else
return server.replyOKHTML("")
exec : |
ow.loadTemplate(); ow.loadFormat()
ow.template.addHelper("encodeURI", encodeURI)
var temph = _$(args.templateHead).isString().default("## {{puri}}<img align=\"right\" src=\"/fonts/openaf_small.png\">\n")
var templ = _$(args.template).isString().default("{{#each this}}| {{#if isDirectory}}__[{{filename}}/]({{encodeURI uri}}{{encodeURI puri}}{{encodeURI filename}}/)__ | {{fLastModified}} | | {{else}} [{{filename}}]({{encodeURI uri}}{{encodeURI puri}}{{encodeURI filename}}) | {{fLastModified}} {{/if}} | {{fSize}} | {{size}} | \n{{/each}}")
args.uri = _$(args.uri).isString().default("/")
args.path = _$(args.path).isString().default(".")
global.__MHTTPD = _$(global.__MHTTPD).isMap().default({})
global.__MHTTPD[String(args.port) + args.uri] = {
BROWSE: _$(args.browse).isString().default("true") === 'true',
PATH : String( (new java.io.File( args.path )).getCanonicalPath() ),
TMPLB : ow.template.execCompiled(ow.template.compile(templ)),
TMPLH : ow.template.execCompiled(ow.template.compile(temph))
}