forked from azazdeaz/rasa-nlu-trainer
-
Notifications
You must be signed in to change notification settings - Fork 185
/
server.js
executable file
·202 lines (178 loc) · 4.5 KB
/
server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#! /usr/bin/env node
'use strict';
// @flow
"use strict"
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json({ limit: '50mb' }))
const findit = require('findit')
const getPort = require('get-port')
const open = require('open')
const updateNotifier = require('update-notifier')
const pkg = require('./package.json')
updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24 // one day
}).notify()
const fs = require('fs')
const argv = require('yargs')
.usage('This is my awesome program\n\nUsage: $0 [options]')
.help('help').alias('help', 'h')
.options({
source: {
alias: 's',
description: '<filename> A json file in native rasa-nlu format',
requiresArg: true,
},
port: {
alias: 'p',
description: '<port> Port to listen on',
requiresArg: true,
},
development: {
alias: 'd',
}
})
.default({
source: null,
port: null,
development: false,
})
.argv
const sourceFile = {
path: '',
data: {},
isLoaded: false,
}
function readData(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (error, raw) => {
var json = {};
if (error) {
return reject(`Can't read file "${path}"\n${error}`)
}
try {
json = JSON.parse(raw)
}
catch (error) {
return reject(`Can't parse json file "${path}"\n${error}`)
}
if (!json.rasa_nlu_data) {
return reject('"rasa_nlu_data" is undefined')
}
resolve(json)
})
})
}
if (argv.source) {
readData(argv.source)
.then(data => {
sourceFile.data = data,
sourceFile.path = argv.source
sourceFile.isLoaded = true
serve()
})
.catch(error => {
throw error
})
}
else {
console.log('searching for the training examples...')
var isSearchingOver = false;
var inReading = 0;
function checkDone() {
if (isSearchingOver && inReading === 0) {
if (!sourceFile.isLoaded) {
throw new Error(`Can't find training file, please try to specify it with the --source option`)
}
else {
serve()
}
}
}
const finder = findit(process.cwd())
finder.on('directory', function (dir, stat, stop) {
var base = path.basename(dir);
if (base === '.git' || base === 'node_modules') stop()
})
finder.on('file', function (file) {
if (file.substr(-5) === '.json' && !sourceFile.isLoaded) {
inReading++
readData(file)
.then(data => {
if (!sourceFile.isLoaded) { // an other file could have been loaded in the meantime
sourceFile.data = data,
sourceFile.path = file
sourceFile.isLoaded = true
console.log(`found ${file}`)
}
})
.catch(() => {})
.then(() => {
inReading--
checkDone()
})
}
})
finder.on('end', function () {
isSearchingOver = true
checkDone()
})
}
function serve() {
// app.use(express.static('./build'))
app.use(express.static(path.join(__dirname, './build')))
if (process.env.NODE_ENV !== 'production') {
//the dev server is running on an other port
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
}
if (!argv.development) {
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, './build', 'index.html'))
})
}
app.post('/data', function (req, res) {
res.json({
data: sourceFile.data,
path: sourceFile.path,
})
})
app.post('/save', function (req, res) {
const data = req.body
if (!data || !data.rasa_nlu_data) {
res.json({error: 'file is invalid'})
}
fs.writeFile(sourceFile.path, JSON.stringify(data, null, 2), (error) => {
if (error) {
return res.json({error})
}
readData(sourceFile.path)
.then(json => sourceFile.data = json)
.catch(error => console.error(error))
.then(() => res.json({ok: true}))
})
})
if (argv.port) {
listen(argv.port)
}
else {
getPort().then(port => listen(port))
}
function listen(port) {
app.listen(port)
if (!argv.development) {
const url = `http://localhost:${port}/`
console.log(`server listening at ${url}`)
open(url)
}
else {
console.log('dev server listening at', port)
}
}
}