-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·101 lines (85 loc) · 3.03 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
const fastify = require('fastify');
const dotenv = require('dotenv');
const path = require('path');
// Load environment variables from .env file
dotenv.config();
// Initialize the server
const server = fastify();
if (process.env.REDIS_ENABLED == "true") {
const { createClient } = require('redis');
// Initalize the Redis client
const redis = createClient(`redis://${process.env.REDIS_USER}:${process.env.REDIS_PASS}@${process.env.REDIS_URL}:${process.env.REDIS_PORT}`);
// 🌠jank🌠
redis.on('error', (err) => console.log('Redis error: ' + err));
}
// Import @fastify/static for the sendFile function
server.register(require('@fastify/static'), {
root: path.join(__dirname, "web"),
serve: false
});
// Also to serve /public/
server.register(require('@fastify/static'), {
root: path.join(__dirname, "public"),
prefix: "/public/",
decorateReply: false
});
// Add a register for sites that need to be served under ejs (projects/blog)
server.register(require('@fastify/view'), {
engine: {
ejs: require('ejs')
},
root: path.join(__dirname, "web", "templates"),
production: process.env.PRODUCTION
});
// ** Define routes ** \\
// Because we're on L42 and we have yet to do anything proper.
// Import the links redirect
server.register(require('./routers/links.js'), { prefix: '/links' });
server.register(require('./routers/links.js'), { prefix: '/l' });
// Import the blog router
server.register(require('./routers/blog'), { prefix: '/blog' });
server.register(require('./routers/blog'), { prefix: '/b' });
// Import the projects router
server.register(require('./routers/projects'), { prefix: '/projects' });
server.register(require('./routers/projects'), { prefix: '/p' });
// Add middleware to block ByteSpider
server.addHook('preHandler', async (request, reply) => {
if (request.headers['user-agent'].includes('ByteSpider')) {
return reply.status(403).send('Forbidden.');
}
}
);
// Main routes
server.register(require('./routers/octashibe'), { prefix: '/' });
// Robots.txt
server.get('/robots.txt', async (request, reply) => {
return reply.sendFile('robots.txt');
});
// Start the server
const start = async () => {
try {
if (!process.env.PORT) {
console.warn('No PORT environment variable found. Defaulting to 5000.');
}
if (process.env.REDIS_ENABLED == "true") {
console.log('Redis enabled. Using ' + process.env.REDIS_URL);
server.decorate('redis', redis);
} else {
console.log('Redis disabled. Set REDIS_ENABLED to true to enable.');
console.warn('Blog and project pages will not work or init.')
}
await server.listen({port: process.env.PORT || 5000});
console.log(`Server is running on ${server.server.address().port}`);
} catch (err) {
console.error(err);
process.exit(1);
}
};
// On Ctrl+C, close the Redis connection if it exists
process.on('SIGINT', () => {
if (process.env.REDIS_ENABLED == "true") {
redis.quit();
}
process.exit();
});
start();