1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-25 17:54:43 +03:00

refactor: remove web app env variables

This commit is contained in:
Louistiti 2019-05-25 12:07:11 +08:00
parent 23e9070640
commit cdef5f8d15
13 changed files with 59 additions and 52 deletions

View File

@ -4,16 +4,9 @@
# Language currently used # Language currently used
LEON_LANG=en-US LEON_LANG=en-US
# Current env
LEON_NODE_ENV=development
# Server # Server
LEON_SERVER_HOST=http://localhost LEON_HOST=http://localhost
LEON_SERVER_PORT=1337 LEON_PORT=1337
# Web app
LEON_WEBAPP_HOST=http://localhost
LEON_WEBAPP_PORT=1337
# Time zone (current one by default) # Time zone (current one by default)
LEON_TIME_ZONE= LEON_TIME_ZONE=

View File

@ -3,12 +3,11 @@
import Chatbot from './chatbot.es6' import Chatbot from './chatbot.es6'
export default class Client { export default class Client {
constructor (client, host, port, input, res) { constructor (client, serverUrl, input, res) {
this.client = client this.client = client
this.host = host
this.port = port
this._input = input this._input = input
this.socket = io.connect(`${this.host}:${this.port}`) this.serverUrl = serverUrl
this.socket = io.connect(this.serverUrl)
this.history = localStorage.getItem('history') this.history = localStorage.getItem('history')
this.parsedHistory = [] this.parsedHistory = []
this.info = res this.info = res
@ -21,7 +20,7 @@ export default class Client {
} }
} }
init (config) { init () {
this.chatbot.init() this.chatbot.init()
this.socket.on('connect', () => { this.socket.on('connect', () => {
@ -58,7 +57,7 @@ export default class Client {
}) })
this.socket.on('download', (data) => { this.socket.on('download', (data) => {
window.location = `/v1/downloads?package=${data.package}&module=${data.module}` window.location = `${this.serverUrl}/v1/downloads?package=${data.package}&module=${data.module}`
}) })
if (this.history !== null) { if (this.history !== null) {

View File

@ -10,18 +10,19 @@ import { onkeydowndocument, onkeydowninput } from './onkeydown.es6'
const config = { const config = {
app: 'webapp', app: 'webapp',
server_host: process.env.LEON_SERVER_HOST, server_host: process.env.LEON_HOST,
server_port: process.env.LEON_SERVER_PORT, server_port: process.env.LEON_PORT,
min_decibels: -40, // Noise detection sensitivity min_decibels: -40, // Noise detection sensitivity
max_blank_time: 1000 // Maximum time to consider a blank (ms) max_blank_time: 1000 // Maximum time to consider a blank (ms)
} }
const serverUrl = process.env.LEON_NODE_ENV === 'production' ? '' : `${config.server_host}:${config.server_port}`
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const loader = new Loader() const loader = new Loader()
loader.start() loader.start()
request.get('/v1/info') request.get(`${serverUrl}/v1/info`)
.end((err, res) => { .end((err, res) => {
if (err || !res.ok) { if (err || !res.ok) {
console.error(err.response.error.message) console.error(err.response.error.message)
@ -30,8 +31,7 @@ document.addEventListener('DOMContentLoaded', () => {
const mic = document.querySelector('button') const mic = document.querySelector('button')
const v = document.querySelector('#version small') const v = document.querySelector('#version small')
const logger = document.querySelector('#logger small') const logger = document.querySelector('#logger small')
const client = new Client(config.app, config.server_host, const client = new Client(config.app, serverUrl, input, res.body)
config.server_port, input, res.body)
let rec = { } let rec = { }
let chunks = [] let chunks = []
let enabled = false let enabled = false
@ -47,7 +47,7 @@ document.addEventListener('DOMContentLoaded', () => {
} }
logger.innerHTML += sLogger logger.innerHTML += sLogger
client.init(config) client.init()
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => { navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {

View File

@ -10,9 +10,9 @@ const record = require('node-record-lpcm16')
const { Detector, Models } = require('snowboy') const { Detector, Models } = require('snowboy')
const io = require('socket.io-client') const io = require('socket.io-client')
process.env.LEON_SERVER_HOST = process.env.LEON_SERVER_HOST || 'http://localhost' process.env.LEON_HOST = process.env.LEON_HOST || 'http://localhost'
process.env.LEON_SERVER_PORT = process.env.LEON_SERVER_PORT || 1337 process.env.LEON_PORT = process.env.LEON_PORT || 1337
const url = `${process.env.LEON_SERVER_HOST}:${process.env.LEON_SERVER_PORT}` const url = `${process.env.LEON_HOST}:${process.env.LEON_PORT}`
const socket = io(url) const socket = io(url)
socket.on('connect', () => { socket.on('connect', () => {
@ -21,7 +21,7 @@ socket.on('connect', () => {
console.log('Waiting for hotword...') console.log('Waiting for hotword...')
}) })
request.get('/v1/info') request.get(`${url}/v1/info`)
.end((err, res) => { .end((err, res) => {
if (err || !res.ok) { if (err || !res.ok) {
if (!err.response) { if (!err.response) {

View File

@ -813,7 +813,7 @@
}, },
"readable-stream": { "readable-stream": {
"version": "2.3.6", "version": "2.3.6",
"resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
"integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
"requires": { "requires": {
"core-util-is": "~1.0.0", "core-util-is": "~1.0.0",
@ -892,7 +892,7 @@
"resolved": "https://registry.npmjs.org/snowboy/-/snowboy-1.2.0.tgz", "resolved": "https://registry.npmjs.org/snowboy/-/snowboy-1.2.0.tgz",
"integrity": "sha1-BLaTM9Fd5ms1NyxpFqXEdz++zfI=", "integrity": "sha1-BLaTM9Fd5ms1NyxpFqXEdz++zfI=",
"requires": { "requires": {
"node-pre-gyp": "^0.6.30" "node-pre-gyp": "^0.6.39"
} }
}, },
"sntp": { "sntp": {

28
package-lock.json generated
View File

@ -6199,12 +6199,14 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -6219,17 +6221,20 @@
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -6346,7 +6351,8 @@
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -6358,6 +6364,7 @@
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -6372,6 +6379,7 @@
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@ -6379,12 +6387,14 @@
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@ -6403,6 +6413,7 @@
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@ -6483,7 +6494,8 @@
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@ -6495,6 +6507,7 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -6616,6 +6629,7 @@
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",

View File

@ -31,12 +31,12 @@
"setup:offline-hotword": "babel-node scripts/setup-offline/run-setup-hotword.js", "setup:offline-hotword": "babel-node scripts/setup-offline/run-setup-hotword.js",
"preinstall": "node scripts/setup/preinstall.js", "preinstall": "node scripts/setup/preinstall.js",
"postinstall": "babel-node scripts/setup/setup.js", "postinstall": "babel-node scripts/setup/setup.js",
"dev:app": "npm run build:app && babel-node scripts/app/dev-app.js", "dev:app": "babel-node scripts/app/dev-app.js",
"dev:server": "npm run train expressions && nodemon --watch server ./server/src/index.js --ignore server/src/tmp/ --exec babel-node", "dev:server": "npm run train expressions && cross-env LEON_NODE_ENV=development nodemon --watch server ./server/src/index.js --ignore server/src/tmp/ --exec babel-node",
"wake": "cross-env LEON_SERVER_HOST=localhost LEON_SERVER_PORT=1337 node hotword/index.js", "wake": "cross-env LEON_HOST=http://localhost LEON_PORT=1337 node hotword/index.js",
"delete-dist:server": "shx rm -rf ./server/dist", "delete-dist:server": "shx rm -rf ./server/dist",
"build": "npm run lint && npm run build:app && npm run build:server", "build": "npm run lint && npm run build:app && npm run build:server",
"build:app": "babel-node scripts/app/run-build-app.js", "build:app": "cross-env LEON_NODE_ENV=production babel-node scripts/app/run-build-app.js",
"build:server": "npm run delete-dist:server && npm run train expressions && babel ./server/src -d ./server/dist --copy-files && shx mkdir -p server/dist/tmp", "build:server": "npm run delete-dist:server && npm run train expressions && babel ./server/src -d ./server/dist --copy-files && shx mkdir -p server/dist/tmp",
"start": "cross-env LEON_NODE_ENV=production node ./server/dist/index.js", "start": "cross-env LEON_NODE_ENV=production node ./server/dist/index.js",
"train": "babel-node scripts/run-train.js", "train": "babel-node scripts/run-train.js",
@ -79,7 +79,6 @@
"browser-sync": "^2.26.5", "browser-sync": "^2.26.5",
"browserify": "^14.4.0", "browserify": "^14.4.0",
"cli-spinner": "^0.2.8", "cli-spinner": "^0.2.8",
"dev-ip": "^1.0.1",
"envify": "^4.1.0", "envify": "^4.1.0",
"eslint": "^5.16.0", "eslint": "^5.16.0",
"eslint-config-airbnb-base": "^11.2.0", "eslint-config-airbnb-base": "^11.2.0",

View File

@ -1,5 +1,6 @@
import dotenv from 'dotenv' import dotenv from 'dotenv'
import buildApp from './build-app'
import serveApp from './serve-app' import serveApp from './serve-app'
dotenv.config(); dotenv.config();
@ -8,5 +9,8 @@ dotenv.config();
* Main entry for the webapp development * Main entry for the webapp development
*/ */
(async () => { (async () => {
process.env.LEON_NODE_ENV = 'development'
await buildApp()
await serveApp() await serveApp()
})() })()

View File

@ -1,5 +1,4 @@
import browserSync from 'browser-sync' import browserSync from 'browser-sync'
import devip from 'dev-ip'
import buildApp from './build-app' import buildApp from './build-app'
@ -30,10 +29,11 @@ export default () => new Promise((resolve) => {
} }
} }
const [, host] = process.env.LEON_HOST.split('//')
bs.init({ bs.init({
host: devip(), host,
port: 4242, port: 4242,
open: true, open: host !== 'localhost' ? 'external' : true,
server: { server: {
baseDir: 'app/', baseDir: 'app/',
index: 'index.html' index: 'index.html'

View File

@ -80,7 +80,7 @@ class Server {
app.use(`/${apiVersion}/downloads`, downloadRouter) app.use(`/${apiVersion}/downloads`, downloadRouter)
try { try {
await this.listen(process.env.LEON_SERVER_PORT) await this.listen(process.env.LEON_PORT)
resolve() resolve()
} catch (e) { } catch (e) {
log[e.type](e.obj.message) log[e.type](e.obj.message)
@ -100,7 +100,7 @@ class Server {
return return
} }
log.success(`Server is listening on ${port}`) log.success(`Server is available at ${process.env.LEON_HOST}:${port}`)
const io = socketio.listen(this.server) const io = socketio.listen(this.server)
io.on('connection', this.connection) io.on('connection', this.connection)

View File

@ -5,10 +5,8 @@
*/ */
const corsMidd = (req, res, next) => { const corsMidd = (req, res, next) => {
// Allow only a specific client to request to the API (depending of the env) // Allow only a specific client to request to the API (depending of the env)
if (process.env.NODE_ENV === 'production') { if (process.env.LEON_NODE_ENV !== 'production') {
res.header('Access-Control-Allow-Origin', `${process.env.LEON_WEBAPP_HOST}:${process.env.LEON_WEBAPP_PORT}`) res.header('Access-Control-Allow-Origin', `${process.env.LEON_HOST}:4242`)
} else {
res.header('Access-Control-Allow-Origin', `${process.env.LEON_WEBAPP_HOST}:4242`)
} }
// Allow several headers for our requests // Allow several headers for our requests

View File

@ -55,8 +55,8 @@ describe('server', () => {
Server.server.close() Server.server.close()
}) })
await Server.listen(process.env.LEON_SERVER_PORT) await Server.listen(process.env.LEON_PORT)
server.listen(process.env.LEON_SERVER_PORT) server.listen(process.env.LEON_PORT)
}) })
}) })

View File

@ -19,7 +19,7 @@ global.enExpressions = expressions
process.env.LEON_LANG = 'en-US' process.env.LEON_LANG = 'en-US'
process.env.LEON_NODE_ENV = 'testing' process.env.LEON_NODE_ENV = 'testing'
process.env.LEON_SERVER_HOST = 'localhost' process.env.LEON_HOST = 'http://localhost'
process.env.LEON_SERVER_PORT = 1338 process.env.LEON_PORT = 1338
process.env.LEON_TIME_ZONE = global.date.time_zone process.env.LEON_TIME_ZONE = global.date.time_zone
process.env.LEON_TTS = true.toString() process.env.LEON_TTS = true.toString()