This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
85 lines (75 loc) · 1.87 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
if ( process.argv.includes( "-devenv" ) )
require( "dotenv" ).config( { path: "./dev.env" } )
else
require( "dotenv" ).config()
const fs = require( "fs" )
const path = require( "path" )
let trustProxy = !!( process.env.TRUST_PROXY )
if( trustProxy && process.env.TRUST_PROXY_LIST_PATH )
{
let addressList = fs.readFileSync( process.env.TRUST_PROXY_LIST_PATH ).toString()
trustProxy = addressList.split( "\n" ).map( a => a.trim() ).filter( a => !a.startsWith( "#" ) && a != "" )
}
let fastify = require( "fastify" )
if ( process.env.USE_HTTPS )
{
fastify = fastify( {
logger: process.env.USE_FASTIFY_LOGGER || false,
https: {
key: fs.readFileSync( process.env.SSL_KEY_PATH ),
cert: fs.readFileSync( process.env.SSL_CERT_PATH )
},
trustProxy
} )
}
else
{
fastify = fastify( {
logger: process.env.USE_FASTIFY_LOGGER || false,
trustProxy
} )
}
fastify.register( require( "fastify-multipart" ) )
const ROUTE_PATHS = [ "client", "server", "account" ]
if( process.env.USE_RATELIMIT )
{
fastify.register( require( "fastify-rate-limit" ),
{
global: false,
errorResponseBuilder: function( req, context )
{
return {
code: 429,
error: "Too Many Requests",
message: `Request limit reached. You can send ${context.max} requests every ${context.after}. Timeout expiry: ${context.ttl}ms.`,
date: Date.now(),
expiresIn: context.ttl // milliseconds
}
}
}
)
}
for ( let routePath of ROUTE_PATHS )
{
for ( let file of fs.readdirSync( routePath ) )
{
if ( file.endsWith( ".js" ) )
{
console.log( `Registering routes from file ${path.join( routePath, file )}` )
fastify.register( require( path.join( __dirname, routePath, file ) ) )
}
}
}
async function start()
{
try
{
await fastify.listen( process.env.LISTEN_PORT || 80, process.env.LISTEN_IP || "0.0.0.0" )
}
catch ( ex )
{
console.error( ex )
process.exit( 1 )
}
}
start()