Skip to content

Commit 8bbe9fd

Browse files
authored
Merge pull request #35 from eeditiones/fix/12
next round of refactoring
2 parents 2ac3ada + 4cd2919 commit 8bbe9fd

28 files changed

+679
-1181
lines changed

.existdb.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
]
2020
},
2121
"package": {
22-
"author": "Dom&Jörn",
22+
"author": "TEI-publisher project team",
2323
"target": "tuttle",
24-
"description": "tuttle - github integration.",
25-
"namespace": "http://exist-db.org/apps/tuttle",
26-
"website": "http://exist-db.org/apps/tuttle",
24+
"description": "Synchronize data with git services",
25+
"namespace": "http://e-editiones.org/tuttle",
26+
"website": "https://github.com/eeditiones/tuttle",
2727
"status": "dev",
28-
"title": "tuttle - github integration",
28+
"title": "tuttle - pull data from git APIs",
2929
"license": "GPLv3"
3030
}
3131
}

src/cleanup.xq

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
xquery version "3.1";
2+
3+
declare namespace sm="http://exist-db.org/xquery/securitymanager";
4+
5+
(: TODO: $target is not set in cleanup phase :)
6+
declare variable $configuration-collection := "/db/apps/tuttle/data/";
7+
declare variable $backup-collection := "/db/tuttle-backup/";
8+
declare variable $configuration-filename := "tuttle.xml";
9+
10+
(: backup tuttle configuration :)
11+
if (not(xmldb:collection-available($backup-collection)))
12+
then ((: move/copy to collection :)
13+
util:log("info", "Creating configuration backup collection"),
14+
xmldb:create-collection("/db", "tuttle-backup"),
15+
sm:chmod(xs:anyURI($backup-collection), "rwxr-x---")
16+
)
17+
else ()
18+
,
19+
util:log("info", "Backing up configuration"),
20+
xmldb:move($configuration-collection, $backup-collection, $configuration-filename)

src/controller.xql

+16-53
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ declare variable $exist:controller external;
77
declare variable $exist:prefix external;
88
declare variable $exist:root external;
99

10-
if ($exist:path eq "") then
10+
declare variable $is-get := lower-case(request:get-method()) eq 'get';
11+
12+
if ($is-get and $exist:path eq "") then
1113
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
1214
<redirect url="{request:get-uri()}/"/>
1315
</dispatch>
1416

15-
(: forward root path to index.xql :)
16-
else if ($exist:path eq "/") then
17+
else if ($is-get and $exist:path eq "/") then
1718
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
18-
<redirect url="index.html"/>
19+
<forward url="{$exist:controller}/index.html">
20+
<set-header name="Content-Type" value="text/html"/>
21+
</forward>
1922
</dispatch>
23+
2024
else if ($exist:resource eq 'login') then
2125
let $loggedIn := login:set-user("org.exist.login", (), false())
2226
let $user := request:get-attribute("org.exist.login.user")
@@ -40,56 +44,15 @@ else if ($exist:resource eq 'login') then
4044
)
4145

4246
(: static HTML page for API documentation should be served directly to make sure it is always accessible :)
43-
else if ($exist:path eq "/index.html") then
44-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
45-
<set-header name="Content-Type" value="text/html"/>
46-
</dispatch>
47-
else if ($exist:path eq "/data/tuttle.xml") then
48-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
49-
<forward url="{$exist:controller}/data/tuttle.xml">
50-
<set-header name="Cache-Control" value="max-age=31536000"/>
51-
</forward>
52-
</dispatch>
53-
else if ($exist:path eq "/api.html" or ends-with($exist:resource, "json")) then
54-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
55-
</dispatch>
56-
57-
(: other images are resolved against the data collection and also returned directly :)
58-
else if (matches($exist:resource, "\.(css)$", "s")) then
59-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
60-
</dispatch>
47+
else if ($is-get and $exist:path = ("/api.html", "/api.json")) then
48+
<dispatch xmlns="http://exist.sourceforge.net/NS/exist" />
6149

62-
else if (matches($exist:resource, "\.(js)$", "s")) then
50+
(: serve static resources :)
51+
else if ($is-get and matches($exist:path, "^/(css|js|images)/[^/]+\.(css|js(\.map)?|svg|jpg|png)$")) then
6352
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
64-
</dispatch>
65-
66-
else if (matches($exist:resource, "\.(js.map)$", "s")) then
67-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
68-
</dispatch>
69-
70-
else if (matches($exist:resource, "\.(png)$", "s")) then
71-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
72-
</dispatch>
73-
else if (matches($exist:resource, "\.(svg)$", "s")) then
74-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
75-
</dispatch>
76-
77-
else if (matches($exist:resource, "\.(png|jpg|jpeg|gif|tif|tiff|txt|mei|js)$", "s")) then
78-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
79-
<forward url="{$exist:controller}/data/{$exist:path}">
80-
<set-header name="Cache-Control" value="max-age=31536000"/>
81-
</forward>
82-
</dispatch>
83-
84-
(: use a different Open API router, needs exist-jwt installed! :)
85-
else if (starts-with($exist:path, '/jwt')) then
86-
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
87-
<forward url="{$exist:controller}/modules/custom-router.xq">
88-
<set-header name="Access-Control-Allow-Origin" value="*"/>
89-
<set-header name="Access-Control-Allow-Credentials" value="true"/>
90-
<set-header name="Access-Control-Allow-Methods" value="GET, POST, DELETE, PUT, PATCH, OPTIONS"/>
91-
<set-header name="Access-Control-Allow-Headers" value="Accept, Content-Type, Authorization, X-Auth-Token"/>
92-
<set-header name="Cache-Control" value="no-cache"/>
53+
<forward url="{$exist:controller}/resources/{$exist:path}">
54+
<set-header name="Cache-Control" value="max-age=2419200, must-revalidate, stale-while-revalidate=86400"/>
55+
<!-- <set-header name="Cache-Control" value="max-age=31536000"/> -->
9356
</forward>
9457
</dispatch>
9558

@@ -103,4 +66,4 @@ else
10366
<set-header name="Access-Control-Allow-Headers" value="Accept, Content-Type, Authorization, X-Start"/>
10467
<set-header name="Cache-Control" value="no-cache"/>
10568
</forward>
106-
</dispatch>
69+
</dispatch>

src/data/tuttle-example-config.xml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<tuttle>
2+
<repos>
3+
<collection name="tuttle-sample-data">
4+
<!-- optional: make _one_ repository the default
5+
for endpoints without {collection}
6+
-->
7+
<default>true</default>
8+
9+
<!-- which sercive to connect to -->
10+
<type>github</type>
11+
<baseurl>https://api.github.com/</baseurl>
12+
13+
<!-- necessary to provide for GitHub -->
14+
<owner>eeditiones</owner>
15+
<repo>tuttle-sample-data</repo>
16+
17+
<!-- branch to pull from -->
18+
<ref>next</ref>
19+
20+
<!-- authenticate against remote service,
21+
can also be read from environment variables -->
22+
<token>XXX</token>
23+
24+
<!-- only necessary if you need the service to call tuttle -->
25+
<hookuser>admin</hookuser>
26+
<hookpasswd></hookpasswd>
27+
</collection>
28+
29+
<collection name="tuttle-sample-gitlab">
30+
<!-- which sercive to connect to -->
31+
<type>gitlab</type>
32+
<baseurl>https://gitlab.com/api/v4/</baseurl>
33+
34+
<!-- this is not needed for Gitlab but can be added for human consumption -->
35+
<owner>line-o</owner>
36+
<repo>tuttle-sample-data</repo>
37+
38+
<!-- necessary to provide for Gitlab -->
39+
<project-id>50872175</project-id>
40+
41+
<!-- branch to pull from -->
42+
<ref>main</ref>
43+
44+
<!-- authenticate against remote service,
45+
can also be read from environment variables -->
46+
<token>XXX</token>
47+
48+
<!-- only necessary if you need the service to call tuttle -->
49+
<hookuser>admin</hookuser>
50+
<hookpasswd></hookpasswd>
51+
</collection>
52+
</repos>
53+
54+
<!-- these files will not be extracted from archives and left untouched when cleaning up -->
55+
<ignore>
56+
<file>existdb.json</file>
57+
<file>build.xml</file>
58+
<file>README.md</file>
59+
<file>.gitignore</file>
60+
<file>expath-pkg.xml.tmpl</file>
61+
<file>repo.xml.tmpl</file>
62+
<file>build.properties.xml</file>
63+
</ignore>
64+
65+
<!-- prefix, suffix, lock and apikeys can usually be left as-is -->
66+
<config prefix="/db/apps/" suffix="-stage" lock="git-lock.xml" apikeys="/db/system/auth/tuttle-token.xml">
67+
<!--
68+
The permissions the deployed data gets assigned, if no expath-pkg.xml is found.
69+
The initial setting is safe, but this can and should be adapted to your requirements.
70+
-->
71+
<sm user="admin" group="dba" mode="rw-r-----"/>
72+
</config>
73+
</tuttle>

src/data/tuttle.xml

-42
This file was deleted.

src/demo.css

-68
This file was deleted.

src/expath-pkg.xml

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<package
3-
xmlns="http://expath.org/ns/pkg"
4-
name="http://e-editiones.org/tuttle"
5-
abbrev="tuttle" version="1.2.0" spec="1.0">
6-
<title>Tuttle - Git for eXist-db</title>
7-
<dependency processor="http://exist-db.org" semver-min="5.5.1"/>
8-
<dependency package="http://e-editiones.org/roaster" semver-min="1.0.0"/>
9-
<dependency package="http://exist-db.org/apps/shared" semver-min="0.9.1"/>
10-
<dependency package="http://expath.org/ns/crypto" semver-min="5.0.0"/>
2+
<package xmlns="http://expath.org/ns/pkg" spec="1.0"
3+
name="http://e-editiones.org/tuttle" abbrev="tuttle" version="2.0.0">
4+
<title>Tuttle - Git for eXist-db</title>
5+
<dependency processor="http://exist-db.org" semver-min="5.5.1" semver-max="6" />
6+
<dependency package="http://e-editiones.org/roaster" semver-min="1.0.0"/>
7+
<dependency package="http://expath.org/ns/crypto" semver-min="5.0.0"/>
118
</package>

src/finish.xq

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
xquery version "3.1";
2+
3+
declare namespace sm="http://exist-db.org/xquery/securitymanager";
4+
5+
(: the target collection into which the app is deployed :)
6+
declare variable $target external;
7+
8+
declare variable $configuration-collection := $target || "/data/";
9+
declare variable $backup-collection := "/db/tuttle-backup/";
10+
declare variable $configuration-filename := "tuttle.xml";
11+
12+
(: look for backed up tuttle configuration :)
13+
if (doc-available($backup-collection || $configuration-filename))
14+
then ((: move/copy to collection :)
15+
util:log("info", "Restoring tuttle configuration from backup."),
16+
xmldb:move($backup-collection, $configuration-collection, $configuration-filename),
17+
xmldb:remove($backup-collection)
18+
)
19+
else ((: copy example configuration when no backup was found :)
20+
util:log("info", "No previous tuttle configuration found."),
21+
xmldb:copy-resource(
22+
$configuration-collection, "tuttle-example-config.xml",
23+
$configuration-collection, $configuration-filename
24+
)
25+
)
26+
,
27+
(: tighten security for configuration file :)
28+
sm:chmod(xs:anyURI($configuration-collection || $configuration-filename), "rw-r-----")
29+
,
30+
(: set gid for API :)
31+
sm:chmod(xs:anyURI($target || "/modules/api.xql"), "rwxr-sr-x")
32+

0 commit comments

Comments
 (0)