88 lines
No EOL
2.6 KiB
Text
88 lines
No EOL
2.6 KiB
Text
module namespace cm = 'app/cm';
|
|
import module namespace ore = 'urn:quodatum:template:oregano' at "lib.xq/oregano.xqm";
|
|
declare variable $cm:opts:=map{"base": file:resolve-path("views/", file:base-dir()),
|
|
"layout": true(),
|
|
"indent": "no"
|
|
};
|
|
declare variable $cm:model:=map{"_ctx": "app"};
|
|
|
|
declare function cm:header($path as xs:string?,$data as xs:base64Binary?,$content-type as xs:string?)
|
|
as element(rest:response)
|
|
{ switch(true())
|
|
case exists($path) return
|
|
web:response-header(
|
|
map { 'media-type': web:content-type($path) },
|
|
|
|
map {
|
|
'Cache-Control': 'max-age=3600,public',
|
|
'Content-Length': file:size($path),
|
|
'ETag': file:last-modified($path)
|
|
}
|
|
)
|
|
|
|
default return
|
|
web:response-header(
|
|
map { 'media-type':$content-type },
|
|
(map:entry('Cache-Control', 'max-age=3600,public'),
|
|
if(exists($data)) then map:entry('Content-Length', bin:length($data))
|
|
)=>map:merge()
|
|
)
|
|
};
|
|
|
|
(:~ http response from file content:)
|
|
declare function cm:response-file($path as xs:string)
|
|
as item()*{
|
|
if(file:exists($path))
|
|
then (
|
|
cm:header($path,(),()),
|
|
file:read-binary($path)
|
|
)else error(xs:QName('cm:not-found'),$path)
|
|
()
|
|
};
|
|
|
|
(:~ http response from data content:)
|
|
declare function cm:response-data($data as xs:base64Binary,$content-type as xs:string)
|
|
as item()*
|
|
{
|
|
cm:header((),$data,$content-type),
|
|
$data
|
|
|
|
};
|
|
|
|
|
|
(:~ http response for htmx :)
|
|
declare function cm:htmx2($template as xs:string,$model as map(*))
|
|
as item()*
|
|
{
|
|
cm:header((),(),"text/html"),
|
|
ore:render($template,$cm:opts,$model)=>ore:serialize($cm:opts)
|
|
};
|
|
|
|
|
|
(:~ read a relative file :)
|
|
declare function cm:read($path as xs:string)
|
|
as xs:string{
|
|
file:resolve-path( $path,file:base-dir())=>file:read-text()
|
|
};
|
|
|
|
(:~ write a message to standard BaseX log passthru item :)
|
|
declare function cm:trace($items as item()*,$msg as xs:string)
|
|
as item()*{
|
|
$items,admin:write-log($msg || ": " || serialize($items,map{"method":"basex"}),"MDUI")
|
|
};
|
|
|
|
declare function cm:status-404()
|
|
as item()*{
|
|
<rest:response>
|
|
<http:response status="404">
|
|
<http:header name="Content-Language" value="en"/>
|
|
<http:header name="Content-Type" value="text/plain; charset=utf-8"/>
|
|
</http:response>
|
|
</rest:response>,
|
|
"The requested resource is not available."
|
|
};
|
|
|
|
declare function cm:status($status as xs:integer,$msg as xs:string)
|
|
as item()*{
|
|
web:response-header((),(), map { 'status': $status, 'message': "OK"}),$msg
|
|
}; |