-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
90 lines (76 loc) · 2.35 KB
/
app.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
require('@babel/register')({
cwd: __dirname,
plugins: ['@babel/plugin-transform-modules-commonjs'],
only: [
'./lib/*'
]
})
let argv = require('minimist')(process.argv.slice(2))
let pjson = require('./package.json')
let substruct = require('@internalfx/substruct')
let path = require('path')
let { ApolloServer, AuthenticationError } = require('apollo-server-koa')
let { typeDefs, resolvers } = require('./graphql/index.js')
require('./lib/cycle.js')
let configFile = null
let HELPTEXT = `
FreeSWITCH Integration Server v${pjson.version}
OPTIONS
===================================================================
--config Specify path to config file, if not specified FSIS
will look for config.js in current working directory.
`
let main = async function () {
process.env.NODE_ENV = argv.dev ? 'development' : 'production'
if (argv.help) {
console.log(HELPTEXT)
return
}
let configPath = argv.config ? argv.config : './config.js'
configPath = path.join(process.cwd(), configPath)
try {
configFile = require(configPath)
} catch (err) {
console.log('CONFIG FILE NOT FOUND! ======================')
throw err
}
substruct.configure({
// buildNuxt: argv.build,
runDir: process.cwd(),
appDir: __dirname,
fsis: configFile
})
let apollo = new ApolloServer({
typeDefs,
resolvers,
formatError: function (error) {
let data = JSON.decycle(error)
console.log('================================================================== GRAPHQL ERROR')
console.dir(data, { colors: true, depth: null })
console.log('================================================================================')
return data
},
context: async function ({ ctx }) {
let session = ctx.state.session
let nedb = substruct.services.nedb
let freeswitch = substruct.services.freeswitch
if (session.loggedIn !== true) {
throw new AuthenticationError('You are not logged in')
}
return {
session,
nedb,
freeswitch
}
}
})
substruct.start().then(async function ({ koa, config }) {
apollo.applyMiddleware({ app: substruct.koa, path: '/api/graphql' })
console.log('Server Started...')
}).catch(function (err) {
console.error(err.stack)
})
}
main().catch(function (err) {
console.log(err)
})