-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontroller.xql
68 lines (59 loc) · 2.97 KB
/
controller.xql
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
xquery version "3.1";
declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $exist:prefix external;
declare variable $exist:root external;
declare variable $local:isget := request:get-method() = ("GET","get");
util:log("debug", map {
"$exist:path": $exist:path,
"$exist:resource": $exist:resource,
"$exist:controller": $exist:controller,
"$exist:prefix": $exist:prefix,
"$exist:root": $exist:root,
"$local:isget": $local:isget
}),
if ($local:isget and $exist:path eq "") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{request:get-uri()}/"/>
</dispatch>
(: forward root path to index.xql :)
else if ($local:isget and $exist:path eq "/") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="api.html"/>
</dispatch>
(: static HTML page for API documentation should be served directly to make sure it is always accessible :)
else if (
($local:isget and $exist:path eq "/api.html") or
($local:isget and matches($exist:path, "^/[^/]+\.json$", "s"))
) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist" />
(: other static resources are resolved against the resources collection and also returned directly :)
else if ($local:isget and matches($exist:path, "^/static/.+$", "s")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/{replace($exist:path, "static", "resources")}">
<set-header name="Cache-Control" value="max-age=31536000"/>
</forward>
</dispatch>
(: use a different Open API router, needs exist-jwt installed! :)
else if (starts-with($exist:path, '/jwt')) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/custom-router.xq">
<set-header name="Access-Control-Allow-Origin" value="*"/>
<set-header name="Access-Control-Allow-Credentials" value="true"/>
<set-header name="Access-Control-Allow-Methods" value="GET, POST, DELETE, PUT, PATCH, OPTIONS"/>
<set-header name="Access-Control-Allow-Headers" value="Accept, Content-Type, Authorization, X-Auth-Token"/>
<set-header name="Cache-Control" value="no-cache"/>
</forward>
</dispatch>
(: all other requests are passed on the Open API router :)
else
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/api.xql">
<set-header name="Access-Control-Allow-Origin" value="*"/>
<set-header name="Access-Control-Allow-Credentials" value="true"/>
<set-header name="Access-Control-Allow-Methods" value="GET, POST, DELETE, PUT, PATCH, OPTIONS"/>
<set-header name="Access-Control-Allow-Headers" value="Accept, Content-Type, Authorization, X-Start"/>
<set-header name="Cache-Control" value="no-cache"/>
</forward>
</dispatch>