This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebsocket
executable file
·88 lines (77 loc) · 2.18 KB
/
websocket
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
#!/usr/bin/env node
const dotenv = require('dotenv');
const EchoServer = require('laravel-echo-server');
/**
* Script that launches the websocket server
*/
/**
* Parsed process arguments
*/
const args = (() => {
const argv = process.argv.slice(2);
let args = {};
let argLast = null;
for (let arg of argv) {
if (arg.substr(0, 2) === "--") {
argLast = arg.substr(2);
args[argLast] = true;
} else if (argLast) {
args[argLast] = arg;
argLast = null;
}
}
return args;
})();
/**
* `.env` file environment variables
*/
const env = (() => {
const result = dotenv.config({path: __dirname + '/.env' + (args.env && args.env !== true ? `.${args.env}` : '')});
if (result.error)
throw result.error;
return result.parsed;
})();
/**
* Return either an environment variable or a default value
* @param {function} func
* @param defaultValue
* @returns {*}
*/
function optional(func, defaultValue) {
const result = func(env);
return result !== undefined ? result : defaultValue;
}
/**
* Either return an environment variable or throw an error if missing
* @param {function} func
* @returns {*}
*/
function required(func) {
const result = func(env);
if (result === undefined)
throw "Missing required environment variable";
return result;
}
/**
* Laravel echo server options
*/
const options = {
database: 'redis',
databaseConfig: {
redis: {
host: optional(e => e.REDIS_HOST, '127.0.0.1'),
port: optional(e => e.REDIS_PORT, 6379),
password: optional(e => e.REDIS_PASSWORD, ''),
}
},
authHost: required(e => e.WEBSOCKET_APP_ENDPOINT || e.APP_URL),
devMode: optional(e => e.APP_DEBUG === "true" || e.APP_DEBUG === true, false),
port: optional(e => e.WEBSOCKET_PORT, 6001),
protocol: optional(e => e.WEBSOCKET_PROTOCOL, "http"),
sslCertPath: optional(e => e.SSL_CERT_PATH, ''),
sslKeyPath: optional(e => e.SSL_KEY_PATH, ''),
sslCertChainPath: optional(e => e.SSL_CERT_CHAIN_PATH, ''),
sslPassphrase: optional(e => e.SSL_PASSPHRASE, ''),
};
// Run the server
EchoServer.run(options);