1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-24 17:23:23 +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
LEON_LANG=en-US
# Current env
LEON_NODE_ENV=development
# Server
LEON_SERVER_HOST=http://localhost
LEON_SERVER_PORT=1337
# Web app
LEON_WEBAPP_HOST=http://localhost
LEON_WEBAPP_PORT=1337
LEON_HOST=http://localhost
LEON_PORT=1337
# Time zone (current one by default)
LEON_TIME_ZONE=

View File

@ -3,12 +3,11 @@
import Chatbot from './chatbot.es6'
export default class Client {
constructor (client, host, port, input, res) {
constructor (client, serverUrl, input, res) {
this.client = client
this.host = host
this.port = port
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.parsedHistory = []
this.info = res
@ -21,7 +20,7 @@ export default class Client {
}
}
init (config) {
init () {
this.chatbot.init()
this.socket.on('connect', () => {
@ -58,7 +57,7 @@ export default class Client {
})
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) {

View File

@ -10,18 +10,19 @@ import { onkeydowndocument, onkeydowninput } from './onkeydown.es6'
const config = {
app: 'webapp',
server_host: process.env.LEON_SERVER_HOST,
server_port: process.env.LEON_SERVER_PORT,
server_host: process.env.LEON_HOST,
server_port: process.env.LEON_PORT,
min_decibels: -40, // Noise detection sensitivity
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', () => {
const loader = new Loader()
loader.start()
request.get('/v1/info')
request.get(`${serverUrl}/v1/info`)
.end((err, res) => {
if (err || !res.ok) {
console.error(err.response.error.message)
@ -30,8 +31,7 @@ document.addEventListener('DOMContentLoaded', () => {
const mic = document.querySelector('button')
const v = document.querySelector('#version small')
const logger = document.querySelector('#logger small')
const client = new Client(config.app, config.server_host,
config.server_port, input, res.body)
const client = new Client(config.app, serverUrl, input, res.body)
let rec = { }
let chunks = []
let enabled = false
@ -47,7 +47,7 @@ document.addEventListener('DOMContentLoaded', () => {
}
logger.innerHTML += sLogger
client.init(config)
client.init()
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
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 io = require('socket.io-client')
process.env.LEON_SERVER_HOST = process.env.LEON_SERVER_HOST || 'http://localhost'
process.env.LEON_SERVER_PORT = process.env.LEON_SERVER_PORT || 1337
const url = `${process.env.LEON_SERVER_HOST}:${process.env.LEON_SERVER_PORT}`
process.env.LEON_HOST = process.env.LEON_HOST || 'http://localhost'
process.env.LEON_PORT = process.env.LEON_PORT || 1337
const url = `${process.env.LEON_HOST}:${process.env.LEON_PORT}`
const socket = io(url)
socket.on('connect', () => {
@ -21,7 +21,7 @@ socket.on('connect', () => {
console.log('Waiting for hotword...')
})
request.get('/v1/info')
request.get(`${url}/v1/info`)
.end((err, res) => {
if (err || !res.ok) {
if (!err.response) {

View File

@ -813,7 +813,7 @@
},
"readable-stream": {
"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==",
"requires": {
"core-util-is": "~1.0.0",
@ -892,7 +892,7 @@
"resolved": "https://registry.npmjs.org/snowboy/-/snowboy-1.2.0.tgz",
"integrity": "sha1-BLaTM9Fd5ms1NyxpFqXEdz++zfI=",
"requires": {
"node-pre-gyp": "^0.6.30"
"node-pre-gyp": "^0.6.39"
}
},
"sntp": {

28
package-lock.json generated
View File

@ -6199,12 +6199,14 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@ -6219,17 +6221,20 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"core-util-is": {
"version": "1.0.2",
@ -6346,7 +6351,8 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"ini": {
"version": "1.3.5",
@ -6358,6 +6364,7 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@ -6372,6 +6379,7 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@ -6379,12 +6387,14 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
@ -6403,6 +6413,7 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@ -6483,7 +6494,8 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"object-assign": {
"version": "4.1.1",
@ -6495,6 +6507,7 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@ -6616,6 +6629,7 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^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",
"preinstall": "node scripts/setup/preinstall.js",
"postinstall": "babel-node scripts/setup/setup.js",
"dev:app": "npm run build: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",
"wake": "cross-env LEON_SERVER_HOST=localhost LEON_SERVER_PORT=1337 node hotword/index.js",
"dev:app": "babel-node scripts/app/dev-app.js",
"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_HOST=http://localhost LEON_PORT=1337 node hotword/index.js",
"delete-dist:server": "shx rm -rf ./server/dist",
"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",
"start": "cross-env LEON_NODE_ENV=production node ./server/dist/index.js",
"train": "babel-node scripts/run-train.js",
@ -79,7 +79,6 @@
"browser-sync": "^2.26.5",
"browserify": "^14.4.0",
"cli-spinner": "^0.2.8",
"dev-ip": "^1.0.1",
"envify": "^4.1.0",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^11.2.0",

View File

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

View File

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

View File

@ -80,7 +80,7 @@ class Server {
app.use(`/${apiVersion}/downloads`, downloadRouter)
try {
await this.listen(process.env.LEON_SERVER_PORT)
await this.listen(process.env.LEON_PORT)
resolve()
} catch (e) {
log[e.type](e.obj.message)
@ -100,7 +100,7 @@ class Server {
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)
io.on('connection', this.connection)

View File

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

View File

@ -55,8 +55,8 @@ describe('server', () => {
Server.server.close()
})
await Server.listen(process.env.LEON_SERVER_PORT)
server.listen(process.env.LEON_SERVER_PORT)
await Server.listen(process.env.LEON_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_NODE_ENV = 'testing'
process.env.LEON_SERVER_HOST = 'localhost'
process.env.LEON_SERVER_PORT = 1338
process.env.LEON_HOST = 'http://localhost'
process.env.LEON_PORT = 1338
process.env.LEON_TIME_ZONE = global.date.time_zone
process.env.LEON_TTS = true.toString()