-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mjs
87 lines (69 loc) · 2.13 KB
/
app.mjs
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
/* jshint esversion: 6 */
/* jslint esversion: 6 */
'use strict';
import SwaggerExpress from 'swagger-express-mw';
import SwaggerUi from 'swagger-tools/middleware/swagger-ui';
import os from 'os';
import express from 'express';
import path from 'path';
import util from 'util';
import _ from 'lodash';
import { Card, CardDeck } from './Cards';
let app = express();
let hostname = ("http://" + os.hostname() + ":"); // "http://127.0.0.1:";
let port = process.env.PORT || 10010;
console.log(`Starting server ${hostname}${port} ...`);
// const __dirname = path.dirname(new URL(import.meta.url).pathname);
let config = { appRoot: "." };
SwaggerExpress.create(config, function (err, swaggerExpress) {
if (err) { throw err; }
// Add swagger-ui (This must be before swaggerExpress.register)
let ui = SwaggerUi(swaggerExpress.runner.swagger);
app.use(ui);
// install middleware
swaggerExpress.register(app);
app.listen(port);
app.use(express.static('www'));
let someText = "boo!";
app.get('/boo', (req, res) => res.send(someText));
swaggerExpress.runner.swagger.paths['/dealer'];
});
const OP_GENERATE = 0;
const OP_CUT = 1;
const OP_SHUFFLE = 2;
const OP_POP = 3;
function carddeck(req, res) {
// variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name}
let id = req.swagger.params.id.value || 0;
let op = req.swagger.params.op.value || 0;
let pos = req.swagger.params.pos.value || 0;
console.log(id, op, pos);
let result = {};
let deck = new CardDeck(id);
let popItems = [];
switch (op) {
case OP_GENERATE:
break;
case OP_CUT:
deck.cutAt(pos);
break;
case OP_SHUFFLE:
deck.shuffle();
break;
case OP_POP:
if (pos<=1)
pos = 1;
popItems = deck.pop(pos);
break;
default:
console.log(`Unsupported op=$[op}`);
break;
}
result = { 'id': deck.hashCode, 'op': op, 'glyphs': deck.glyphs };
// populate pop items only for the pop op
if (popItems.length)
result.pop = popItems;
res.setHeader('Content-Type', 'application/json');
res.json(result);
}
_['carddeck'] = carddeck;