-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.funct.mjs
52 lines (43 loc) · 1.45 KB
/
serve.funct.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
/**
* This file is ** NOT ** used in production on the Funct registry
* Any code you change here ** WILL NOT ** run when hosted with Funct
*
* However, it used for local development and allows you to ship your Funct
* service to any host which relies on `package.json["scripts"]["start"]`.
*/
// Third-party imports
import InstantAPI from '@instant.dev/api';
import dotenv from 'dotenv';
// Native imports
import cluster from 'cluster';
import os from 'os';
// Shorthand references
const Daemon = InstantAPI.Daemon;
const Gateway = InstantAPI.Daemon.Gateway;
const EncryptionTools = InstantAPI.EncryptionTools;
// Constants
const ENVIRONMENT = process.env.NODE_ENV || 'development';
const PORT = process.env.PORT || 8100;
if (cluster.isPrimary) {
// Multi-process daemon
const daemon = new Daemon(
ENVIRONMENT !== 'development'
? os.cpus().length
: 1,
'Funct'
);
daemon.start(PORT);
} else {
// Individual webserver startup
const gateway = new Gateway({
name: 'Funct.Gateway',
debug: ENVIRONMENT !== 'production'
});
// Optional: Enable Sentry or another error reporting tool
// gateway.setErrorHandler(err => Sentry.captureException(err));
const et = new EncryptionTools();
dotenv.config(); // load env vars
et.decryptProcessEnv(process.env); // decrypt env vars, if necessary
gateway.load(process.cwd()); // load routes from filesystem
gateway.listen(PORT); // start server
}