generated from soederpop/skypager-fullstack-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (156 loc) · 5 KB
/
index.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
exports = module.exports = {}
exports.history = function history() {
return !this.runtime.isDevelopment
}
exports.serveStatic = function serveStatic() {
return !this.runtime.isDevelopment
}
exports.cors = true
exports.appWillMount = function appWillMount(app) {
const { runtime } = this
return app
}
exports.appDidMount = async function appDidMount(app) {
const { runtime } = this
await Promise.all([
runtime.sheets && runtime.sheets.discover(),
runtime.googleDocs && runtime.googleDocs.discover()
])
if (runtime.isDevelopment) {
setupDevelopment.call(this, app)
}
return app
}
function setupDevelopment(app) {
const { runtime } = this
const serviceAccount = runtime.fsx.readJsonSync(runtime.resolve('secrets', 'serviceAccount.json'))
const { hot } = this.options
const webpack = require('webpack')
const merge = require('webpack-merge')
const devMiddleware = require('webpack-dev-middleware')
const hotMiddleware = require('webpack-hot-middleware')
const config = merge(require('@skypager/webpack/config/webpack.config')('development'), {
node: {
process: 'mock',
},
plugins: [
new webpack.DefinePlugin({
SERVICE_ACCOUNT_EMAIL: JSON.stringify(serviceAccount.client_email),
SERVICE_ACCOUNT_PROJECT_ID: JSON.stringify(serviceAccount.project_id),
}),
],
externals: [
{
react: 'global React',
'react-dom': 'global ReactDOM',
'react-router-dom': 'global ReactRouterDOM',
'semantic-ui-react': 'global semanticUIReact',
'prop-types': 'global PropTypes',
'@skypager/web': 'global skypager',
},
],
})
config.entry[1] = 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000'
const compiler = webpack(config)
const middleware = devMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
})
app.use(middleware)
if (hot !== false) {
app.use(
hotMiddleware(compiler, {
path: '/__webpack_hmr',
})
)
}
/*
app.get('*', (req, res) => {
middleware.fileSystem.readFile(
runtime.pathUtils.resolve(compiler.outputPath, 'index.html'),
(err, file) => {
if (err) {
res.sendStatus(404)
} else {
res.send(file.toString())
}
}
)
})
*/
return app
}
function appWillMount(app, options = {}, context = {}) {
const { runtime } = this
const { isEmpty } = this.lodash
const { colors } = runtime.cli
runtime.debug('Mounting sheets app')
app.use((req, _, next) => {
!isEmpty(req.params)
? runtime.info(`${colors.green(req.method)} ${req.url}`, req.params)
: runtime.info(`${colors.green(req.method)} ${req.url}`)
next()
})
app.get('/sheets', (req, res) => {
const data = runtime.sheets.allMembers()
res.json(data)
})
app.get('/sheets-meta/:sheetName', (req, res) => {
const { sheetName } = req.params
if (!sheetName || !sheetName.length || !runtime.sheets.checkKey(req.params.sheetName)) {
res.status(404).json({ notFound: true, error: true, sheetName })
} else {
const sheet = runtime.sheet(sheetName)
sheet.whenReady().then(() => {
res.json({
info: sheet.info,
worksheets: sheet.worksheets,
})
})
}
})
app.get('/sheets/:sheetName', (req, res) => {
const { sheetName } = req.params
if (!sheetName || !sheetName.length || !runtime.sheets.checkKey(req.params.sheetName)) {
res.status(404).json({ notFound: true, error: true, sheetName })
} else {
const sheet = runtime.sheet(sheetName)
sheet
.whenReady()
.then(() => (!req.params.refresh && sheet.has('data') ? sheet.data : sheet.loadAll()))
.then(data => res.status(200).json(data))
.catch(error => {
runtime.error(error.message)
console.log(error.stack)
res.status(500).send('<pre>' + error.message + '\n' + error.stack + '</pre>')
})
}
})
app.get('/sheets/:sheetName/:worksheetId', (req, res) => {
const { worksheetId, sheetName } = req.params
if (!sheetName || !sheetName.length || !runtime.sheets.checkKey(req.params.sheetName)) {
runtime.error(`404 ${req.method} ${req.url}`)
res.status(404).json({ notFound: true, error: true, sheetName })
} else {
const sheet = runtime.sheet(sheetName)
sheet
.whenReady()
.then(() => (!req.params.refresh && sheet.has('data') ? sheet.data : sheet.loadAll()))
.then(data => {
if (worksheetId === '_info') {
res.status(200).json(sheet.info)
} else if (!data[worksheetId]) {
res.status(404).send('Invalid Worksheet')
} else {
res.status(200).json(data[worksheetId])
}
})
.catch(error => {
runtime.error(`500 ${req.method} ${req.url}`, error.message)
console.log(error.stack)
res.status(500).send('<pre>' + error.message + '\n' + error.stack + '</pre>')
})
}
})
return app
}