forked from RamonGebben/figlefy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (102 loc) · 2.81 KB
/
app.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
'use strict';
const Hapi = require('hapi'),
figlet = require('figlet'),
pkg = require('./package.json'),
Joi = require('joi'),
fs = require('fs');
let history = require('./tracking.json');
const port = process.env.PORT || 3000;
const gitpodHost = "127.0.0.1";
const herokuHost = "0.0.0.0";
const server = new Hapi.Server();
server.connection({
host: herokuHost,
port: port,
routes: { cors: true }
});
String.prototype.titleize = function() {
return this.split(/[.?*+^$[\]\\(){}_\s|-]/g).map(function( s ){ return s.capitalize(); }).join(' ');
}
String.prototype.capitalize = function() {
return this.substr( 0, 1 ).toUpperCase() + this.substr(1);
}
server.register([require('vision'), require('inert'), { register: require('lout') }], (err) => {});
server.route({
method: 'GET',
path: '/{param*}',
config: {
plugins: {
lout: false
}
},
handler: {
directory: {
path: './public'
}
}
});
server.route({
method: 'GET',
path:'/fonts',
config: {
description: 'Get a list of possible fonts',
notes: 'Returns an Array of fonts',
tags: ['api']
},
handler: (request, reply) => {
return reply(pkg.fontList);
}
});
server.route({
method: 'GET',
path:'/figlefy',
config: {
description: 'Welcome page',
notes: 'API Root',
tags: ['api']
},
handler: (request, reply) => {
return reply({
'welcome': 'Welcome to figlefy API',
'docs': '/docs'
});
}
});
server.route({
method: 'GET',
path:'/figlefy/{string}/{font?}',
config: {
description: 'Get your text figlefied',
notes: 'Returns a logable string',
tags: ['api'],
validate: {
params: {
string: Joi.string()
.required()
.description('The text you want figlefied.'),
font: Joi.string()
.description('The font you want to use.'),
}
}
},
handler: (request, reply) => {
let font = (request.params.font) ? request.params.font.titleize() : 'standard'.titleize(),
ascii = figlet.textSync(request.params.string, {
font: font,
horizontalLayout: 'default',
verticalLayout: 'default'
}),
trackingObj = {
string: request.params.string,
font: font,
date: new Date()
};
history.push(trackingObj);
fs.writeFileSync('./tracking.json', JSON.stringify(history));
return reply(ascii);
}
});
server.start((err) => {
if (err) { throw err; }
console.log('Server running at:', server.info.uri);
});