-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (184 loc) · 6.46 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// import '@babel/polyfill'
import path from 'path'
import express from 'express'
import favicon from 'serve-favicon'
import webpack from 'webpack'
import { AUTH_TOKEN, PORTAPI, PORT } from './constants'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import webpackHotServerMiddleware from 'webpack-hot-server-middleware'
// import { findVideos, findVideo } from './api'
// import { GraphQLServer } from 'graphql-yoga'
import QLServer, { options } from './src/index.ts'
const chalk = require('chalk')
/*
const { Prisma } = require('prisma-binding')
const Query = require('./resolvers/Query')
const Mutation = require('./resolvers/Mutation')
const AuthPayload = require('./resolvers/AuthPayload')
const Subscription = require('./resolvers/Subscription')
const Feed = require('./resolvers/Feed')
const resolvers = {
Query,
Mutation,
AuthPayload,
Subscription,
Feed
}
*/
options.port = PORTAPI
const DEV = process.env.NODE_ENV === 'development'
let clientConfig
let serverConfig, formatWebpackMessages
if (DEV) {
clientConfig = require('../webpack/client.dev')
serverConfig = require('../webpack/server.dev')
formatWebpackMessages = require('./formatmessage')
} else {
clientConfig = require('../webpack/client.prod')
serverConfig = require('../webpack/server.prod')
}
const publicPath = clientConfig.output.publicPath
const outputPath = clientConfig.output.path
const jwToken = null
// app.use(favicon(path.resolve(__dirname, '../public', 'favicon.ico')))
// UNIVERSAL HMR + STATS HANDLING GOODNESS:
const app = express()
console.log('efe')
app.get('/api/videos/:category', async (req, res) => {
// const jwToken = req.headers.authorization.split(' ')[1]
// const data = await findVideos(req.params.category, jwToken)
const data = { category: 'test' }
res.json(data)
})
app.get('/api/video/:slug', async (req, res) => {
// const jwToken = req.headers.authorization.split(' ')[1]
// const data = await findVideo(req.params.slug, jwToken)
const data = { category: 'test' }
res.json(data)
})
/*
const options = {
port: PORTAPI,
debug: true,
// endpoint: "https://eu1.prisma.sh/public-foamcentaur-934/hackernews-graphql-js/dev/",
subscriptions: '/subscriptions',
playground: '/playground'
}
const QLServer = new GraphQLServer({
typeDefs: 'server/schema.graphql',
resolvers,
context: req => ({
...req,
db: new Prisma({
typeDefs: 'server/generated/prisma.graphql', // the auto-generated GraphQL schema of the Prisma API
// endpoint: 'https://us1.prisma.sh/public-relicgargoyle-251/ts-test/dev', // the endpoint of the Prisma API
endpoint: 'https://eu1.prisma.sh/public-foamcentaur-934/hackernews-graphql-js/dev/',
debug: true, // log all GraphQL queries & mutations sent to the Prisma API
secret: 'mysecret123'
// secret: 'mysecret123', // only needed if specified in `database/prisma.yml`
})
})
})*/
if (DEV) {
console.log('run')
let isFirstCompile = true
let multiCompiler, devMiddleware
try {
multiCompiler = webpack([clientConfig, serverConfig])
devMiddleware = webpackDevMiddleware(multiCompiler, {
publicPath,
writeToDisk: true,
hot: true,
contentBase: publicPath,
watchContentBase: true,
serverSideRender: true
})
app.use(devMiddleware)
// const clientCompiler = webpack([clientConfig]);
const clientCompiler = multiCompiler.compilers[0]
const hotMiddleware = webpackHotMiddleware(clientCompiler)
app.use(hotMiddleware)
app.use('/staticssr', express.static(serverConfig.output.path))
app.use(
webpackHotServerMiddleware(multiCompiler, {
serverRendererOptions: { outputPath: clientConfig.output.path }
})
)
} catch (err) {
isFirstCompile = true
console.log(chalk.red('Failed to compile.'))
console.log()
console.log(err.message || err)
console.log()
process.exit(1)
}
multiCompiler.hooks.invalid.tap('invalid', () => {
console.log('Compiling...')
})
multiCompiler.hooks.done.tap('done', stats => {
// We have switched off the default Webpack output in WebpackDevServer
// options so we are going to "massage" the warnings and errors and present
// them in a readable focused way.
const messages = formatWebpackMessages(stats.toJson({}, true))
const isSuccessful = !messages.errors.length && !messages.warnings.length
if (isSuccessful && isFirstCompile) {
console.log(chalk.green('Compiled successfully!'))
QLServer.start(options, () => {
console.log(`Api Server is running on localhost:${PORTAPI}`)
app.listen(PORT, () => {
console.log(`Server is running on localhost:${PORT}`)
})
})
}
// If errors exist, only show errors.
if (messages.errors.length) {
// Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise.
if (messages.errors.length > 1) {
messages.errors.length = 1
}
console.log(chalk.red('Failed to compile.\n'))
console.log(messages.errors.join('\n\n'))
return
}
isFirstCompile = false
// Show warnings if no errors were found.
if (messages.warnings.length) {
console.log(chalk.yellow('Compiled with warnings.\n'))
console.log(messages.warnings.join('\n\n'))
// Teach some ESLint tricks.
console.log(
`\nSearch for the ${chalk.underline(
chalk.yellow('keywords')
)} to learn more about each warning.`
)
console.log(
`To ignore, add ${chalk.cyan(
'// eslint-disable-next-line'
)} to the line before.\n`
)
}
})
const debug =
typeof v8debug === 'object' ||
/--debug|--inspect/.test(process.execArgv.join(' '))
// just to inform there is a problem in serverside!
if (!debug) {
app.use((error, req, res, next) => {
// Gets called because of `wrapAsync()`
res.json({ message: error.message })
})
}
} else {
const serverRender = require('../buildServer/main.js').default // eslint-disable-line import/no-unresolved
const clientStats = require('../buildClient/stats.json') // eslint-disable-line import/no-unresolved
app.use('/static', express.static(outputPath))
app.use(serverRender({ clientStats, outputPath }))
QLServer.start(options, () => {
console.log(`Api Server is running on localhost:${PORTAPI}`)
app.listen(PORT, () => {
console.log(`Server is running on localhost:${PORT}`)
})
})
}