-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitzpapalotl.js
128 lines (107 loc) · 3.66 KB
/
itzpapalotl.js
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
/*jslint indent: 2, nomen: true, maxlen: 100, white: true, plusplus: true, unparam: true */
/*global require */
////////////////////////////////////////////////////////////////////////////////
/// @brief An example Foxx-Application for ArangoDB
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2013 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
(function() {
"use strict";
const createRouter = require('@arangodb/foxx/router');
const router = createRouter();
module.context.use(router);
// include console module so we can log something (in the server's log)
var console = require("console");
var ArangoError = require("@arangodb").ArangoError;
// we also need this module for custom responses
var actions = require("@arangodb/actions");
// use joi for validation
var joi = require("joi");
// our app is about the following Aztec deities:
var deities = [
"CentzonTotochtin",
"Chalchihuitlicue",
"Chantico",
"Chicomecoatl",
"Cihuacoatl",
"Cinteotl",
"Coatlicue",
"Coyolxauhqui",
"Huehuecoyotl",
"Huitzilopochtli",
"Ilamatecuhtli",
"Itzcoliuhqui",
"Itzpaplotl",
"Mayauel",
"Meztli",
"Mictlantecuhtli",
"Mixcoatl",
"Quetzalcoatl",
"Tezcatlipoca",
"Tialoc",
"Tlauixcalpantecuhtli",
"Tlazolteotl",
"Tonatiuh",
"Tozi",
"XipeTotec",
"Xochiquetzal",
"Xolotl",
"Yacatecuhtli"
];
// install index route (this is the default route mentioned in manifest.json)
// this route will create an HTML overview page
router.get('/index', function (req, res) {
res.set("content-type", "text/html");
var body = "<h1>" + module.context.service.manifest.name + " (" + module.context.service.manifest.version + ")</h1>";
body += "<h2>an example application demoing a few Foxx features</h2>";
deities.forEach(function(deity) {
body += "summon <a href=\"" + encodeURIComponent(deity) + "/summon\">" + deity + "</a><br />";
});
body += "<hr />pick a <a href=\"random\">random</a> Aztec deity";
res.body = body;
})
.summary("prints an overview page");
// install route to return a random deity name in JSON
router.get('/random', function (req, res) {
var idx = Math.round(Math.random() * (deities.length - 1));
res.json({ name: deities[idx] });
})
.summary("returns a random deity name");
// install deity-specific route for summoning
// deity name is passed as part of the URL
router.get('/:deity/summon', function (req, res) {
var deity = req.pathParams.deity;
console.log("request to summon %s", deity);
if (deities.indexOf(deity) === -1) {
// unknown deity
res.throw(404, "The requested deity could not be found");
}
console.log("summoning %s", deity);
res.json({ name: deity, summoned: true });
})
.summary("summons the requested deity")
.pathParam("deity",
joi.string().required()
);
}());