-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (121 loc) · 3.07 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Pulls in the required parameters
**/
const express = require('express');
const commandLineArgs = require('command-line-args');
/**
* Command line properties
**/
const optionDefinitions = [
{ name: 'count', alias: 'c', type: Number },
{ name: 'port', alias: 'p', type: Number },
]
/**
* Parse out the command line arguments
**/
const args = commandLineArgs(optionDefinitions);
/**
* The amount of pages to show
**/
const AMOUNT_RESERVED = 1;
const AMOUNT_OF_PAGES = parseInt(args.count || process.env.PAGES || 100) || 100;
const AMOUNT_OF_DIRS = Math.round(AMOUNT_OF_PAGES / 500) || 1;
const DELTA_PAGES = AMOUNT_OF_PAGES - AMOUNT_RESERVED - AMOUNT_OF_DIRS;
/**
* Loop and build a array of pages we can use
**/
var PAGES = [];
/**
* Do the actual loop
**/
for(var i = 0; i < DELTA_PAGES; i++) {
// set the pages
var LISTING_TO_USE = Math.ceil( Math.random() * AMOUNT_OF_DIRS );
// add the page
PAGES.push({
dir: LISTING_TO_USE,
index: 1 * i // clone to avoid ref issues
});
}
/**
* PORT TO LISTEN ON
**/
const PORT = parseInt(args.port || process.env.PORT || 8080)
/**
* Setup express with pug to render templates
**/
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'pug')
/**
*
**/
app.use(function(req, res, next) {
// total amount of requested pages
res.locals.pagecount = DELTA_PAGES;
// array of pages that we can render
res.locals.pages = PAGES;
// amount of directories to show
res.locals.dirs = AMOUNT_OF_DIRS;
// move on
next();
});
app.get('/', function(req, res) {
// else just render a nice message. With a link to home :)
res.render('home', {})
});
/**
* Handles showing a individual page
**/
app.get('/p/:pageid', function(req, res) {
// check if the page id exists
var pageid = parseInt(req.params.pageid);
// check if number
if(pageid === NaN ||
pageid === undefined ||
pageid === null) {
// yes stop it
return res.status(404).render('notfound');
}
// check sanity check just for fun
if(pageid > DELTA_PAGES ||
pageid == 0) {
// yes stop it
return res.status(404).render('notfound');
}
// else just render a nice message. With a link to home :)
res.render('page', {
pageid: pageid
})
});
/**
* Handles showing the directory
**/
app.get('/d/:dirid', function(req, res) {
// check if the page id exists
var dirid = parseInt(req.params.dirid);
// check if number
if(dirid === NaN ||
dirid === undefined ||
dirid === null) {
// yes stop it
return res.status(404).render('notfound');
}
// check sanity check just for fun
if(dirid > AMOUNT_OF_DIRS ||
dirid == 0) {
// yes stop it
return res.status(404).render('notfound');
}
// loop it
res.render('directory', {
dirid: dirid
})
});
/**
* Start listening
**/
app.listen(PORT, ()=>{
// output logging info, just to be cool
console.log('Server with', AMOUNT_OF_PAGES, 'pages running on port', PORT)
});