-
Notifications
You must be signed in to change notification settings - Fork 164
/
config.js
105 lines (89 loc) · 3.49 KB
/
config.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
'use strict'; // eslint-disable-line strict
require('dotenv').config();
const Kodi = require('./kodi-connection/node.js');
let kodiConfig = [];
let globalConfig = {};
try {
// Try to import the kodi hosts. If not found, we'll assume that env variables are available.
let configFile = process.env.GOOGLE_HOME_KODI_CONFIG || './kodi-hosts.config.js';
let config = require(configFile); // eslint-disable-line global-require
kodiConfig = config.kodiConfig;
globalConfig = config.globalConfig;
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
const checkMandatoryEnv = (variables) => {
let missing = variables.filter((v) => !process.env[v]);
if (missing.length > 0) {
console.log('Missing mandatory configuration:', missing.join(', '));
console.log(
`Check your .env-File (when hosting with Glitch)`,
`or your environment variables (when hosting with Docker)`,
`or the kodi-hosts.config.js file.`
);
process.exit(1);
}
};
const Init = function() {
this.kodiHosts = [];
this.globalConf = {};
if (kodiConfig.length !== 0) {
// We've found one or more kodi configurations.
this.kodiHosts = kodiConfig.map((config) => {
return {
id: config.id,
host: new Kodi(
config.kodiProtocol || 'http',
config.kodiIp,
config.kodiPort,
config.kodiUser,
config.kodiPassword)
};
});
console.log(`Loaded ${this.kodiHosts.length} Kodi hosts.`);
} else {
checkMandatoryEnv(['KODI_IP', 'KODI_PORT']);
this.kodiHosts.push({
id: 'kodi',
host: new Kodi(
process.env.KODI_PROTOCOL || 'http',
process.env.KODI_IP,
process.env.KODI_PORT,
process.env.KODI_USER,
process.env.KODI_PASSWORD)
});
}
if (Object.keys(globalConfig).length > 0) {
console.log(`Loaded config from kodi-hosts.config.js, ${JSON.stringify(globalConfig, null, 2)}`);
this.globalConf = globalConfig;
} else {
checkMandatoryEnv(['AUTH_TOKEN', 'PORT']);
this.globalConf.authToken = process.env.AUTH_TOKEN;
this.globalConf.listenerPort = process.env.PORT;
this.globalConf.youtubeKey = process.env.YOUTUBE_KEY || 'AIzaSyBYKxhPJHYUnzYcdOAv14Gmq-43_W9_79w';
this.globalConf.brokerAccentInsensitiveMatch = process.env.BROKER_ACCENT_INSENSITIVE_MATCH === 'true';
this.globalConf.brokerLanguageCacheEnable = process.env.BROKER_LANGUAGE_CACHE_ENABLE === 'false';
console.log('Loaded config from .env (environment)');
}
this.getHost = (kodiId) => {
if (kodiId) {
return this.kodiHosts
.find((kodiHost) => kodiHost.id === kodiId)
.host;
}
return this.kodiHosts[0].host;
};
/*
* Check the request to determine to which kodi instance we want to route the actions to.
* The request object is anylyzed. And the Kodi object is attached to the request, for further use.
*/
this.routeKodiInstance = (request) => {
// For now we are only attaching the first host in the list.
// Next will be to determin a way of passing a host, through IFTTT.
request.kodi = this.getHost(request.body.kodiid);
request.config = this.globalConf;
};
};
module.exports = Init;