-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
288 lines (281 loc) · 7.58 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const { Keystone } = require('@keystonejs/keystone');
const { PasswordAuthStrategy } = require('@keystonejs/auth-password');
const { Text, Checkbox, Password, File, Integer, Relationship, DateTime, } = require('@keystonejs/fields');
const { Markdown } = require('@keystonejs/fields-markdown');
const { GraphQLApp } = require('@keystonejs/app-graphql');
const { AdminUIApp } = require('@keystonejs/app-admin-ui');
const { NuxtApp } = require('@keystonejs/app-nuxt');
const { LocalFileAdapter } = require('@keystonejs/file-adapters');
const { MongooseAdapter: Adapter } = require('@keystonejs/adapter-mongoose');
const { atTracking } = require('@keystonejs/list-plugins');
const expressSession = require('express-session');
const MongoStore = require('connect-mongo')(expressSession);
const { createItems } = require('@keystonejs/server-side-graphql-client');
require('dotenv').config();
const config = {
endpoint: process.env.ENDPOINT,
keystoneconfig: {
name: "topdocs",
secureCookies: process.env.NODE_ENV === 'production',
cookie: {
secure: process.env.NODE_ENV === 'production', // Default to true in production
maxAge: 1000 * 60 * 60 * 24 * 30, // 30 days
sameSite: false,
},
sessionStore: new MongoStore({ url: process.env.MONGOURI }),
adapter: new Adapter({
mongoUri: process.env.MONGOURI,
}),
cookieSecret: process.env.COOKIESECRET,
onConnect: async () => {
const users = await keystone.lists.User.adapter.findAll();
if (!users.length) {
await createItems({
keystone,
listKey: 'User',
items: [{
data: {
name: 'admin',
email: '[email protected]',
isAdmin: true,
password: 'adminadmin'
}
}]
})
}
}
}
};
const keystone = new Keystone(config.keystoneconfig);
// Access control functions
const userIsAdmin = ({ authentication: { item: user } }) =>
Boolean(user && user.isAdmin);
const userOwnsItem = ({ authentication: { item: user }, existingItem }) => {
if (!user) {
return false;
}
return existingItem.id === user.id;
};
const userIsAdminOrOwner = auth => {
const isAdmin = access.userIsAdmin(auth);
const isOwner = access.userOwnsItem(auth);
return isAdmin ? isAdmin : isOwner;
};
const access = { userIsAdmin, userOwnsItem, userIsAdminOrOwner };
const fileAdaper = new LocalFileAdapter({
src: './src/static/poster',
path: '/poster',
})
keystone.createList('Image', {
schemaDoc: '上传的附件图片,可以交给文档引用',
fields: {
file: {
type: File,
adapter: fileAdaper,
isRequired: true,
}
},
access: {
create: access.userIsAdmin,
update: access.userIsAdmin,
delete: access.userIsAdmin
},
plugins: [
atTracking(),
],
label: 'File'
})
keystone.createList('Doc', {
schemaDoc: '文档',
fields: {
name: { type: Text },
md: { type: Markdown },
category: { type: Relationship, ref: "Category" }
},
access: {
create: access.userIsAdmin,
update: access.userIsAdmin,
delete: access.userIsAdmin
},
plugins: [
atTracking(),
],
label: 'Doc'
});
keystone.createList('Category', {
schemaDoc: '文档分类',
fields: {
name: { type: Text },
},
access: {
create: access.userIsAdmin,
update: access.userIsAdmin,
delete: access.userIsAdmin
},
plugins: [
atTracking(),
],
label: 'Category'
});
keystone.createList('Setting', {
label: 'Setting',
schemaDoc: 'CMS的核心设置',
fields: {
host: { type: Text, },
name: { type: Text, defaultValue: "跨世代文档编辑系统" },
seotitle: { type: Text, },
keywords: { type: Text, },
description: { type: Text, defaultValue: "跨世代文档编辑系统是新一代的实时在线动态文档系统,支持MARKDOWN语法。" },
github: { type: Text },
index: { type: Markdown }
},
plugins: [
atTracking(),
],
access: {
create: access.userIsAdmin,
update: access.userIsAdmin,
delete: access.userIsAdmin
},
})
keystone.createList('User', {
label: 'User',
fields: {
name: { type: Text, isUnique: true },
email: {
type: Text,
isUnique: true,
access: {
read: access.userIsAdminOrOwner,
}
},
isAdmin: {
type: Checkbox, access: {
create: access.userIsAdmin,
update: access.userIsAdmin,
}
},
password: {
type: Password,
isRequired: true
},
},
// To create an initial user you can temporarily remove access controls
access: {
update: access.userIsAdmin,
delete: access.userIsAdmin,
},
plugins: [
atTracking(),
],
});
const authStrategy = keystone.createAuthStrategy({
type: PasswordAuthStrategy,
list: 'User',
});
module.exports = {
keystone,
apps: [
new GraphQLApp({
apollo: {
tracing: true,
cacheControl: {
defaultMaxAge: 3600,
}
}
}),
new AdminUIApp({ authStrategy }),
new NuxtApp({
srcDir: 'src',
buildDir: 'dist',
cache: true,
mode: 'universal',
loading: {
color: '#fff',
},
/*
** Headers of the page
*/
head: {
title: 'moviecms',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0' },
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
{ rel: 'stylesheet', href: 'https://cdn.jsdelivr.net/npm/simplebar@latest/dist/simplebar.css' },
// {
// rel: "stylesheet",
// href:
// "https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css"
// }
],
script: [
{ src: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/simplebar.min.js' }
],
},
/*
** Load Vuetify into the app
*/
plugins: [],
build: {
extractCSS: true,
},
/*
** Load Vuetify CSS globally
*/
css: ['~/assets/app.css'],
modules: ['@nuxtjs/apollo', "@nuxtjs/markdownit",
["@nuxtjs/moment", ["zh-cn"]], ['@nuxtjs/vuetify', {
theme: {
dark: true
},
defaultAssets: {
icons: false
},
icons: {
iconfont: 'mdiSvg', // default - only for display purposes
},
}], 'nuxt-ssr-cache'],
cache: {
// if you're serving multiple host names (with differing
// results) from the same server, set this option to true.
// (cache keys will be prefixed by your host name)
// if your server is behind a reverse-proxy, please use
// express or whatever else that uses 'X-Forwarded-Host'
// header field to provide req.hostname (actual host name)
useHostPrefix: false,
pages: [
// these are prefixes of pages that need to be cached
// if you want to cache all pages, just include '/'
'/'
],
store: {
type: 'redis',
host: 'localhost',
ttl: 10 * 60 * 60,
configure: [
// these values are configured
// on redis upon initialization
['maxmemory', '200mb'],
['maxmemory-policy', 'allkeys-lru'],
],
},
},
markdownit: {
injected: true
},
apollo: {
clientConfigs: {
default: {
httpEndpoint: config.endpoint,
}
}
},
}),
],
configureExpress: app => {
app.set('trust proxy', true);
},
};