|
1 |
| -const createError = require('http-errors'); |
2 |
| -const express = require('express'); |
3 |
| -const path = require('path'); |
4 |
| -const compression = require('compression'); |
5 |
| -const cookieParser = require('cookie-parser'); |
6 |
| -const logger = require('morgan'); |
7 |
| -const cors = require('cors'); |
8 |
| -const fs = require('fs'); |
9 |
| -const { v4: uuidv4 } = require('uuid'); |
10 |
| -const compute = require('compute-rhino3d'); |
11 |
| -require('dotenv').config(); |
| 1 | +const createError = require('http-errors') |
| 2 | +const express = require('express') |
| 3 | +const path = require('path') |
| 4 | +const compression = require('compression') |
| 5 | +const cookieParser = require('cookie-parser') |
| 6 | +const logger = require('morgan') |
| 7 | +const cors = require('cors') |
| 8 | +const fs = require('fs') |
| 9 | +const { v4: uuidv4 } = require('uuid') |
| 10 | +const compute = require('compute-rhino3d') |
| 11 | + |
| 12 | +// dotenv only required for development. |
| 13 | +// Heroku adds config vars in production. |
| 14 | +// If not running on Heroku, you might want to add this back in with your own .env file. |
| 15 | +// require('dotenv').config() |
12 | 16 |
|
13 | 17 | // routers
|
14 |
| -const indexRouter = require('./routes/index'); |
15 |
| -const definitionRouter = require('./routes/definition'); |
| 18 | +const indexRouter = require('./routes/index') |
| 19 | +const definitionRouter = require('./routes/definition') |
16 | 20 |
|
17 |
| -console.log('appserver version: ' + process.env.npm_package_version); |
18 |
| - |
19 |
| -const app = express(); |
| 21 | +const app = express() |
20 | 22 |
|
21 | 23 | // get cmd line args
|
22 | 24 | // get arguments after first two elements in process.argv
|
23 |
| -let args = process.argv.splice(2); |
| 25 | +let args = process.argv.splice(2) |
24 | 26 |
|
25 |
| -let defArgId = args.indexOf('--definitions'); |
26 |
| -let urlArgId = args.indexOf('--computeUrl'); |
| 27 | +let defArgId = args.indexOf('--definitions') |
| 28 | +let urlArgId = args.indexOf('--computeUrl') |
27 | 29 |
|
28 | 30 | // set arguments or accept defaults
|
29 | 31 | if(defArgId > -1)
|
30 |
| - app.set('definitionsDir', path.normalize(args[defArgId+1])); |
| 32 | + app.set('definitionsDir', path.normalize(args[defArgId+1])) |
31 | 33 | else
|
32 |
| - app.set(app.set('definitionsDir', path.join(__dirname, 'files/'))); |
| 34 | + app.set(app.set('definitionsDir', path.join(__dirname, 'files/'))) |
33 | 35 |
|
34 | 36 | if(urlArgId > -1)
|
35 |
| - app.set('computeUrl', args[urlArgId+1]); |
| 37 | + app.set('computeUrl', args[urlArgId+1]) |
36 | 38 | else
|
37 |
| - app.set('computeUrl', process.env.COMPUTE_URL); // set to a geometry server running on the same machine. NOTE: Port 8082 is when Geometry Server is running debug |
| 39 | + app.set('computeUrl', process.env.COMPUTE_URL) // set to a geometry server running on the same machine. NOTE: Port 8082 is when Geometry Server is running debug |
38 | 40 |
|
39 |
| -compute.url = app.get('computeUrl'); |
| 41 | +compute.url = app.get('computeUrl') |
40 | 42 |
|
41 | 43 | if(process.env.COMPUTE_TOKEN !== undefined)
|
42 | 44 | compute.authToken = process.env.COMPUTE_TOKEN
|
43 | 45 |
|
44 | 46 | if(process.env.APP_URL !== undefined)
|
45 |
| - app.set('appUrl', process.env.APP_URL); |
| 47 | + app.set('appUrl', process.env.APP_URL) |
46 | 48 | else
|
47 |
| - app.set('appUrl', 'http://localhost' + process.env.PORT || '3000' + '/'); |
48 |
| - |
49 |
| -console.log( app.set('computeUrl')); |
50 |
| -console.log(require('os').hostname()); |
51 |
| -console.log(process.env.NODE_ENV); |
52 |
| - |
53 |
| -app.use(logger('dev')); |
54 |
| -app.use(express.json()); |
55 |
| -app.use(express.urlencoded({ extended: false })); |
56 |
| -app.use(cookieParser()); |
57 |
| -app.use(cors()); |
58 |
| -app.use(compression()); |
| 49 | + app.set('appUrl', 'http://localhost' + process.env.PORT || '3000' + '/') |
| 50 | + |
| 51 | +console.log('VERSION: ' + process.env.npm_package_version) |
| 52 | +console.log('COMPUTE_URL: ' + app.get('computeUrl')) |
| 53 | +console.log('NODE_ENV: ' + process.env.NODE_ENV) |
| 54 | + |
| 55 | +app.use(logger('dev')) |
| 56 | +app.use(express.json()) |
| 57 | +app.use(express.urlencoded({ extended: false })) |
| 58 | +app.use(cookieParser()) |
| 59 | +app.use(cors()) |
| 60 | +app.use(compression()) |
59 | 61 | app.use('/example', express.static('example'))
|
60 | 62 |
|
61 |
| -app.use('/', indexRouter); |
62 |
| -app.use('/definition', definitionRouter); |
| 63 | +app.use('/', indexRouter) |
| 64 | +app.use('/definition', definitionRouter) |
63 | 65 |
|
64 | 66 | function getFiles(dir) {
|
65 | 67 | return new Promise ( (resolve, reject) => {
|
66 | 68 | fs.readdir(dir, (err, files) => {
|
67 |
| - if(err) reject(err); |
68 |
| - else resolve(files); |
69 |
| - }); |
70 |
| - } ); |
71 |
| -}; |
| 69 | + if(err) reject(err) |
| 70 | + else resolve(files) |
| 71 | + }) |
| 72 | + } ) |
| 73 | +} |
72 | 74 |
|
73 | 75 | getFiles( app.get('definitionsDir') )
|
74 |
| -.then( (files) => { |
| 76 | + .then( (files) => { |
75 | 77 |
|
76 |
| - if(files.length === 0) |
77 |
| - throw new Error('No definitions found on server'); |
| 78 | + if(files.length === 0) |
| 79 | + throw new Error('No definitions found on server') |
78 | 80 |
|
79 |
| - app.set('definitions', []); |
80 |
| - console.log(files); |
| 81 | + app.set('definitions', []) |
| 82 | + console.log(files) |
81 | 83 |
|
82 |
| - let fullUrl = app.get('appUrl'); // watch this. |
| 84 | + let fullUrl = app.get('appUrl') // watch this. |
83 | 85 |
|
84 |
| - files.forEach(file => { |
| 86 | + files.forEach(file => { |
85 | 87 |
|
86 |
| - if(file.includes('.gh') || file.includes('.ghx')) { |
87 |
| - let id = uuidv4(); |
88 |
| - app.get('definitions').push({name: file, id:id}); |
| 88 | + if(file.includes('.gh') || file.includes('.ghx')) { |
| 89 | + let id = uuidv4() |
| 90 | + app.get('definitions').push({name: file, id:id}) |
89 | 91 |
|
90 |
| - compute.computeFetch('io', {'requestedFile':fullUrl + 'definition/'+ id}).then(result => { |
91 |
| - app.get('definitions').find(d => d.id === id).inputs = result.Inputs === undefined ? result.InputNames : result.Inputs; |
92 |
| - app.get('definitions').find(d => d.id === id).outputs = result.Outputs === undefined ? result.OutputNames: result.Outputs; |
93 |
| - }).catch( (error) => console.log(error)); |
| 92 | + compute.computeFetch('io', {'requestedFile':fullUrl + 'definition/'+ id}).then(result => { |
| 93 | + app.get('definitions').find(d => d.id === id).inputs = result.Inputs === undefined ? result.InputNames : result.Inputs |
| 94 | + app.get('definitions').find(d => d.id === id).outputs = result.Outputs === undefined ? result.OutputNames: result.Outputs |
| 95 | + }).catch( (error) => console.log(error)) |
94 | 96 |
|
95 |
| - } |
96 |
| - }); |
| 97 | + } |
| 98 | + }) |
97 | 99 |
|
98 |
| -}) |
99 |
| -.catch( (error)=>{ console.log(error) }); |
| 100 | + }) |
| 101 | + .catch( (error)=>{ console.log(error) }) |
100 | 102 |
|
101 | 103 | // catch 404 and forward to error handler
|
102 | 104 | app.use(function(req, res, next) {
|
103 |
| - next(createError(404)); |
104 |
| -}); |
| 105 | + next(createError(404)) |
| 106 | +}) |
105 | 107 |
|
106 | 108 | // error handler
|
107 | 109 | app.use(function(err, req, res, next) {
|
108 | 110 |
|
109 | 111 | // set locals, only providing error in development
|
110 |
| - res.locals.message = err.message; |
111 |
| - console.log(err.message); |
112 |
| - res.locals.error = req.app.get('env') === 'development' ? err : {}; |
| 112 | + res.locals.message = err.message |
| 113 | + console.log(err.message) |
| 114 | + res.locals.error = req.app.get('env') === 'development' ? err : {} |
113 | 115 |
|
114 | 116 | // send the error
|
115 |
| - res.status(err.status || 500); |
116 |
| - res.send(err.message); |
117 |
| -}); |
| 117 | + res.status(err.status || 500) |
| 118 | + res.send(err.message) |
| 119 | +}) |
118 | 120 |
|
119 |
| -module.exports = app; |
| 121 | +module.exports = app |
0 commit comments