@opengis/admin 0.1.58 → 0.1.60
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +29 -29
- package/config.js +4 -4
- package/dist/{add-page-d6TCPawD.js → add-page-C3Wh2-Ml.js} +13 -11
- package/dist/admin-interface-C4m7uvG2.js +896 -0
- package/dist/admin-view-YtM2-LrW.js +225 -0
- package/dist/admin.js +1 -1
- package/dist/admin.umd.cjs +95 -207
- package/dist/card-page-WrYs8chM.js +133 -0
- package/dist/{card-view-UYkkl_Pu.js → card-view-BwtqUSFP.js} +1 -1
- package/dist/{edit-page-CtKHElG1.js → edit-page-CMTqsvJf.js} +34 -33
- package/dist/import-file-bdYAo8iW.js +29759 -0
- package/dist/style.css +1 -1
- package/dist/userMenu-CT1xO2Pt.js +5 -0
- package/module/settings/card/admin.roles.table/access.hbs +27 -27
- package/module/settings/card/admin.roles.table/general_info.hbs +16 -16
- package/module/settings/card/admin.roles.table/index.yml +14 -14
- package/module/settings/card/admin.roles.table/users.hbs +27 -27
- package/module/settings/card/admin.routes.table/general_info.hbs +40 -40
- package/module/settings/card/admin.routes.table/index.yml +8 -8
- package/module/settings/card/admin.routes.table/users.hbs +33 -33
- package/module/settings/card/admin.users.table/general_info.hbs +25 -25
- package/module/settings/card/admin.users.table/index.yml +12 -12
- package/module/settings/card/admin.users.table/logs.hbs +30 -30
- package/module/settings/card/admin.users.table/user_roles.hbs +24 -24
- package/module/settings/cls/core.actions.json +13 -13
- package/module/settings/cls/core.scope.json +13 -13
- package/module/settings/cls/properties.site_status.json +13 -13
- package/module/settings/cls/properties.widget_status.json +13 -13
- package/module/settings/cls/users.user_type.json +13 -13
- package/module/settings/form/admin.access.form.json +36 -36
- package/module/settings/form/admin.custom_column.form.json +71 -71
- package/module/settings/form/admin.properties.form.json +15 -15
- package/module/settings/form/admin.roles.form.json +19 -19
- package/module/settings/form/admin.routes.form.json +25 -25
- package/module/settings/form/admin.user_properties.form.json +15 -15
- package/module/settings/form/admin.user_roles.form.json +21 -21
- package/module/settings/form/admin.users.form.json +150 -150
- package/module/settings/form/user.user_roles.form.json +13 -13
- package/module/settings/interface/admin.properties.json +4 -4
- package/module/settings/interface/admin.roles.json +4 -4
- package/module/settings/interface/admin.routes.json +4 -4
- package/module/settings/interface/admin.users.json +4 -4
- package/module/settings/menu.json +50 -50
- package/module/settings/select/core.routes.sql +1 -1
- package/module/settings/select/core.user_uid.sql +1 -1
- package/module/settings/table/admin.access.table.json +77 -77
- package/module/settings/table/admin.custom_column.table.json +94 -94
- package/module/settings/table/admin.properties.table.json +33 -33
- package/module/settings/table/admin.roles.table.json +58 -58
- package/module/settings/table/admin.routes.table.json +67 -67
- package/module/settings/table/admin.user_properties.table.json +28 -28
- package/module/settings/table/admin.user_roles.table.json +66 -66
- package/module/settings/table/admin.users.table.json +119 -119
- package/package.json +79 -81
- package/plugin.js +100 -162
- package/server/helpers/controllers/badge.js +11 -11
- package/server/helpers/controllers/hb.js +2 -2
- package/server/helpers/controllers/map.js +2 -2
- package/server/helpers/controllers/mls.js +2 -2
- package/server/helpers/controllers/vue.js +2 -2
- package/server/helpers/index.mjs +13 -13
- package/server/plugins/adminHook.js +165 -0
- package/server/plugins/cron.js +11 -0
- package/server/plugins/docs.js +28 -28
- package/server/plugins/hook.js +152 -228
- package/server/plugins/vite.js +69 -69
- package/server/routes/calendar/controllers/calendar.data.js +88 -88
- package/server/routes/calendar/index.mjs +17 -17
- package/server/routes/data/controllers/cardData.js +57 -56
- package/server/routes/data/controllers/cardTabData.js +49 -49
- package/server/routes/data/controllers/funcs/getFilterSQL/index.js +92 -92
- package/server/routes/data/controllers/funcs/getFilterSQL/util/formatValue.js +170 -170
- package/server/routes/data/controllers/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/routes/data/controllers/funcs/getFilterSQL/util/getFilterQuery.js +64 -64
- package/server/routes/data/controllers/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
- package/server/routes/data/controllers/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/server/routes/data/controllers/tableData.js +112 -145
- package/server/routes/data/controllers/tableDataId.js +27 -27
- package/server/routes/data/controllers/tableFilter.js +63 -63
- package/server/routes/data/controllers/utils/assignTokens.js +30 -30
- package/server/routes/data/controllers/utils/getColumns.js +8 -21
- package/server/routes/data/index.mjs +15 -15
- package/server/routes/data/schema.js +7 -7
- package/server/routes/menu/controllers/getMenu.js +34 -34
- package/server/routes/menu/index.mjs +5 -5
- package/server/routes/notifications/controllers/readNotifications.js +30 -30
- package/server/routes/notifications/controllers/userNotifications.js +64 -64
- package/server/routes/notifications/hook/onWidgetSet.js +63 -63
- package/server/routes/notifications/index.mjs +40 -40
- package/server/routes/properties/controllers/admin.properties.get.js +29 -29
- package/server/routes/properties/controllers/user.properties.get.js +34 -34
- package/server/routes/properties/controllers/user.properties.post.js +30 -30
- package/server/routes/properties/funcs/getSettings.js +56 -56
- package/server/routes/properties/funcs/setSettings.js +44 -44
- package/server/routes/properties/funcs/utils/dataInsert.js +26 -26
- package/server/routes/properties/index.mjs +26 -26
- package/server/routes/root.mjs +3 -3
- package/server/routes/templates/controllers/getTemplate.js +20 -16
- package/server/routes/templates/index.mjs +14 -14
- package/server/templates/cls/itree.recrzone_category.json +73 -73
- package/server/templates/cls/test.json +9 -9
- package/server/templates/form/admin.user_cls.data.form.json +49 -49
- package/server/templates/form/admin.user_group_rel.form.json +21 -21
- package/server/templates/form/cp_building.form.json +32 -32
- package/server/templates/form/form-user-pass.json +10 -10
- package/server/templates/form/form-user_group.json +39 -39
- package/server/templates/form/form-users.json +156 -156
- package/server/templates/form/user_group_access.form.json +22 -22
- package/server/templates/select/account_id.json +2 -2
- package/server/templates/table/gis.dataset.table.json +43 -43
- package/server/templates/table/management.user_group.table.json +112 -112
- package/server/templates/table/management.users.table.json +126 -126
- package/utils.js +21 -21
- package/dist/admin-interface-w6YuIWM3.js +0 -1343
- package/dist/admin-view-3Sz3UF_y.js +0 -461
- package/dist/card-page-BsQts5XS.js +0 -230
- package/dist/import-file-CsCQ5xvs.js +0 -47264
package/server/plugins/hook.js
CHANGED
@@ -1,228 +1,152 @@
|
|
1
|
-
import
|
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
|
-
req.funcs = fastify;
|
154
|
-
const { user } = req.session?.passport || {};
|
155
|
-
req.user = user;
|
156
|
-
});
|
157
|
-
|
158
|
-
// preSerialization
|
159
|
-
fastify.addHook('preSerialization', async (req, reply, payload) => {
|
160
|
-
if (req.url.includes('/suggest/') && !req.query.json) {
|
161
|
-
return payload?.data;
|
162
|
-
}
|
163
|
-
if (payload?.redirect) {
|
164
|
-
return reply.redirect(payload.redirect);
|
165
|
-
}
|
166
|
-
if (reply.sent) {
|
167
|
-
return null;
|
168
|
-
}
|
169
|
-
|
170
|
-
if (['200', '400', '500', '403', '404'].includes(payload?.status)) {
|
171
|
-
reply.status(payload.status);
|
172
|
-
}
|
173
|
-
/* if (payload.headers) {
|
174
|
-
reply.headers(payload.headers);
|
175
|
-
} */
|
176
|
-
if (payload?.buffer) {
|
177
|
-
return payload.buffer;
|
178
|
-
}
|
179
|
-
if (payload?.file) {
|
180
|
-
// const buffer = await readFile(payload.file);
|
181
|
-
// return reply.send(buffer);
|
182
|
-
const stream = fs.createReadStream(payload.file);
|
183
|
-
return stream;
|
184
|
-
// return reply.send(stream);
|
185
|
-
}
|
186
|
-
|
187
|
-
if (payload?.message) {
|
188
|
-
return payload.message;
|
189
|
-
}
|
190
|
-
return payload;
|
191
|
-
});
|
192
|
-
|
193
|
-
// preValidation
|
194
|
-
fastify.addHook('preValidation', async (req) => {
|
195
|
-
const parseRawBody = ['POST', 'PUT'].includes(req.method) && req.body && typeof req.body === 'string'
|
196
|
-
&& req.body.trim(/\r\n/g).startsWith('{')
|
197
|
-
&& req.body.trim(/\r\n/g).endsWith('}');
|
198
|
-
if (parseRawBody) {
|
199
|
-
try {
|
200
|
-
req.body = JSON.parse(req.body || '{}');
|
201
|
-
}
|
202
|
-
catch (err) {
|
203
|
-
// throw new Error('invalid body');
|
204
|
-
// return { error: 'invalid body', status: 400 };
|
205
|
-
}
|
206
|
-
}
|
207
|
-
});
|
208
|
-
|
209
|
-
// allow upload file
|
210
|
-
const kIsMultipart = Symbol.for('[FastifyMultipart.isMultipart]');
|
211
|
-
fastify.addContentTypeParser('multipart', (request, _, done) => {
|
212
|
-
request[kIsMultipart] = true;
|
213
|
-
done(null);
|
214
|
-
});
|
215
|
-
|
216
|
-
// parse Body
|
217
|
-
function contentParser(req, body, done) {
|
218
|
-
const parseBody = decodeURIComponent(body.toString()).split('&').reduce((acc, el) => {
|
219
|
-
const [key, val] = el.split('=');
|
220
|
-
return { ...acc, [key]: val };
|
221
|
-
}, {});
|
222
|
-
done(null, parseBody);
|
223
|
-
}
|
224
|
-
|
225
|
-
fastify.addContentTypeParser('application/x-www-form-urlencoded', { parseAs: 'buffer' }, contentParser);
|
226
|
-
}
|
227
|
-
|
228
|
-
export default fp(plugin);
|
1
|
+
import { addHook, getToken, setToken, getTemplate } from '@opengis/fastify-table/utils.js';
|
2
|
+
|
3
|
+
export default async function plugin(fastify) {
|
4
|
+
|
5
|
+
addHook('preTable', async ({ req }) => {
|
6
|
+
if (!req?.routeOptions?.method) return;
|
7
|
+
const mode = { POST: 'a', DELETE: 'w', PUT: 'w', GET: 'w' }[req.routeOptions.method] || 'w';
|
8
|
+
const { funcs, params = {}, session = {} } = req;
|
9
|
+
const { uid } = funcs?.config?.auth?.disable ? { uid: '1' } : (session?.passport?.user || {});
|
10
|
+
const opt = await getToken({
|
11
|
+
uid, token: params.id || params.form || params.table, mode, json: 1,
|
12
|
+
});
|
13
|
+
if (opt) Object.assign(req, { opt });
|
14
|
+
if (!opt && !funcs?.config?.local) {
|
15
|
+
return { message: 'access restricted: edit', status: 403 };
|
16
|
+
}
|
17
|
+
return null;
|
18
|
+
});
|
19
|
+
|
20
|
+
addHook('afterTable', async ({ req = {}, res = {}, rows = [], table }) => {
|
21
|
+
const { pg, funcs, params = {}, session = {} } = req;
|
22
|
+
const { uid } = funcs?.config?.auth?.disable ? { uid: '1' } : (session?.passport?.user || {});
|
23
|
+
if (!uid || !table || !pg?.pk?.[table] || !rows.length || !params.table) return;
|
24
|
+
|
25
|
+
// admin.custom_column - user column data
|
26
|
+
const { rows: properties = [] } = await pg.query(`select column_id, name, title, format, data from admin.custom_column
|
27
|
+
where _table and entity=$1 and uid=$2`, [params.table, uid]);
|
28
|
+
const extraColumnList = properties.map((row) => ({ id: row.column_id, name: row.name, title: row.title, format: row.format, data: row.data }));
|
29
|
+
if (!extraColumnList?.length) return;
|
30
|
+
|
31
|
+
if (res?.columns?.length) {
|
32
|
+
extraColumnList.forEach((col) => res.columns.push(col));
|
33
|
+
}
|
34
|
+
|
35
|
+
const { rows: extraData = [] } = await pg.query(`select object_id, json_object_agg( property_id, coalesce(value_date::text,value_text) ) as extra from crm.extra_data
|
36
|
+
where property_entity=$1 and property_id=any($2) and object_id=any($3) group by object_id`, [params.table, extraColumnList?.map((el) => el.id), rows.map((el) => el.id)]);
|
37
|
+
|
38
|
+
if (!extraData?.length) {
|
39
|
+
// Object.assign(rows?.[0] || {}, { ...extraColumnList.reduce((acc, curr) => Object.assign(acc, { [curr.name]: null }), {}) });
|
40
|
+
return;
|
41
|
+
}
|
42
|
+
|
43
|
+
rows.filter((row) => extraData.map((el) => el?.object_id).includes(row.id)).forEach((row) => {
|
44
|
+
const { extra = {} } = extraData.find((el) => el.object_id === row.id);
|
45
|
+
Object.assign(row, { ...Object.fromEntries(Object.entries(extra).map((el) => [extraColumnList.find((col) => col.id === el[0]).name, el[1]])) });
|
46
|
+
});
|
47
|
+
|
48
|
+
// admin.custom_column - metaFormat
|
49
|
+
await Promise.all(extraColumnList.filter((el) => el?.data).map(async (attr) => {
|
50
|
+
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => el);
|
51
|
+
if (!values.length) return;
|
52
|
+
const cls = await getSelectVal({ name: attr.data, values });
|
53
|
+
if (!cls) return;
|
54
|
+
rows.forEach(el => {
|
55
|
+
const val = el[attr.name]?.map?.(c => cls[c] || c) || cls[el[attr.name]] || el[attr.name];
|
56
|
+
if (!val) return;
|
57
|
+
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
58
|
+
});
|
59
|
+
}));
|
60
|
+
});
|
61
|
+
|
62
|
+
// extract table from form token for user columns - p.2 - read (refactor to global token)
|
63
|
+
/* addHook('preTemplate', async ({ req = {} }) => {
|
64
|
+
const { funcs, params = {}, session = {} } = req;
|
65
|
+
const { uid } = funcs?.config?.auth?.disable ? { uid: '1' } : (session?.passport?.user || {});
|
66
|
+
if (!uid || params?.type !== 'form' || !params?.name) return null;
|
67
|
+
|
68
|
+
const { table, form } = await getToken({
|
69
|
+
uid, token: params.name, mode: 'w', json: 1,
|
70
|
+
}) || {};
|
71
|
+
if (form) {
|
72
|
+
Object.assign(req.params || {}, { name: form, table });
|
73
|
+
}
|
74
|
+
}); */
|
75
|
+
|
76
|
+
addHook('afterTemplate', async ({ req = {}, data = {} }) => {
|
77
|
+
const { funcs, session = {} } = req;
|
78
|
+
const { uid } = funcs?.config?.auth?.disable ? { uid: '1' } : (session?.passport?.user || {});
|
79
|
+
const { pg, params = {} } = req;
|
80
|
+
// extract table from form token for user columns - p.1 - assign (refactor to global token)
|
81
|
+
/* if (params?.type === 'table') {
|
82
|
+
const { form } = await getTemplate('table', params.name) || {};
|
83
|
+
if (!form) return;
|
84
|
+
const editTokens = setToken({
|
85
|
+
ids: [{ table: params.name, form }],
|
86
|
+
mode: 'w',
|
87
|
+
uid,
|
88
|
+
array: 1,
|
89
|
+
});
|
90
|
+
Object.assign(data, { token: editTokens[0] });
|
91
|
+
} */
|
92
|
+
if (!uid || !data || params?.type !== 'form' || !params?.name) return null;
|
93
|
+
|
94
|
+
const { rows: properties = [] } = await pg.query(`select name, title, format, data from admin.custom_column
|
95
|
+
where entity=$1 and uid=$2`, [params.table || params.name, uid]);
|
96
|
+
await Promise.all(properties.map(async (el) => {
|
97
|
+
const clsData = el.data ? await getTemplate(['cls', 'select'], el.data) : undefined;
|
98
|
+
const type = clsData ? 'Select' : ({ date: 'DatePicker' }[el.format] || 'Text');
|
99
|
+
Object.assign(data?.schema || data || {}, { [el.name]: { type, ua: el.title, data: el.data, options: type === 'Select' && Array.isArray(clsData) && clsData?.length ? clsData : undefined, extra: 1 } });
|
100
|
+
}));
|
101
|
+
});
|
102
|
+
|
103
|
+
addHook('afterUpdate', async ({ req, res = {} }) => {
|
104
|
+
const {
|
105
|
+
pg, funcs, session = {}, params = {}, body = {},
|
106
|
+
} = req;
|
107
|
+
const { uid } = funcs?.config?.auth?.disable ? { uid: '1' } : (session?.passport?.user || {});
|
108
|
+
if (!uid) return null;
|
109
|
+
|
110
|
+
const loadTable = await getTemplate('table', params.table);
|
111
|
+
if (!pg.pk[loadTable?.table || params.table]) return null;
|
112
|
+
const pk = pg.pk[loadTable?.table || params.table];
|
113
|
+
const id = res[pk];
|
114
|
+
|
115
|
+
const { rows: properties = [] } = await pg.query(`select column_id, name, title, format, data from admin.custom_column
|
116
|
+
where entity=$1 and uid=$2`, [params.table, uid]);
|
117
|
+
|
118
|
+
if (!id || !properties?.length) return null;
|
119
|
+
|
120
|
+
const q = `delete from crm.extra_data where property_entity='${params.table}' and object_id='${id}';${properties
|
121
|
+
.filter((el) => Object.keys(body).includes(el.name))
|
122
|
+
.map((el) => `insert into crm.extra_data(property_id,property_key,property_entity,object_id,${el.format?.toLowerCase() === 'date' ? 'value_date' : 'value_text'})
|
123
|
+
select '${el.column_id}', '${el.name}', '${params.table}', '${id}', ${el.format?.toLowerCase() === 'date' ? `'${body[el.name]}'::timestamp without time zone` : `'${body[el.name]}'::text`}`)
|
124
|
+
.join(';\n') || ''}`;
|
125
|
+
return pg.query(q);
|
126
|
+
});
|
127
|
+
|
128
|
+
addHook('afterInsert', async ({ req, res = {} }) => {
|
129
|
+
const {
|
130
|
+
pg, funcs, session = {}, params = {}, body = {},
|
131
|
+
} = req;
|
132
|
+
const { uid } = funcs?.config?.auth?.disable ? { uid: '1' } : (session?.passport?.user || {});
|
133
|
+
if (!uid) return null;
|
134
|
+
|
135
|
+
const loadTable = await getTemplate('table', params.table);
|
136
|
+
if (!pg.pk[loadTable?.table || params.table]) return null;
|
137
|
+
const pk = pg.pk[loadTable?.table || params.table];
|
138
|
+
const id = res.rows?.[0]?.[pk];
|
139
|
+
|
140
|
+
const { rows: properties = [] } = await pg.query(`select column_id, name, title, format, data from admin.custom_column
|
141
|
+
where entity=$1 and uid=$2`, [params.table, uid]);
|
142
|
+
|
143
|
+
if (!id || !properties?.length) return null;
|
144
|
+
|
145
|
+
const q = properties
|
146
|
+
.filter((el) => Object.keys(body).includes(el.name))
|
147
|
+
.map((el) => `insert into crm.extra_data(property_id,property_key,property_entity,object_id,${el.format?.toLowerCase() === 'date' ? 'value_date' : 'value_text'})
|
148
|
+
select '${el.column_id}', '${el.name}', '${params.table}', '${id}', ${el.format?.toLowerCase() === 'date' ? `'${body[el.name]}'::timestamp without time zone` : `'${body[el.name]}'::text`}`)
|
149
|
+
.join(';\n');
|
150
|
+
return pg.query(q);
|
151
|
+
});
|
152
|
+
}
|
package/server/plugins/vite.js
CHANGED
@@ -1,69 +1,69 @@
|
|
1
|
-
import fs from 'fs';
|
2
|
-
import path, { dirname } from 'path';
|
3
|
-
import { fileURLToPath } from 'url';
|
4
|
-
|
5
|
-
const dir = dirname(fileURLToPath(import.meta.url));
|
6
|
-
const root = `${dir}/../..`;
|
7
|
-
|
8
|
-
const isProduction = process.env.NODE_ENV === 'production';
|
9
|
-
|
10
|
-
async function plugin(fastify) {
|
11
|
-
// vite server
|
12
|
-
if (!isProduction) {
|
13
|
-
const vite = await import('vite');
|
14
|
-
|
15
|
-
const viteServer = await vite.createServer({
|
16
|
-
server: {
|
17
|
-
middlewareMode: true,
|
18
|
-
},
|
19
|
-
});
|
20
|
-
// hot reload
|
21
|
-
viteServer.watcher.on('all', function (d, t) {
|
22
|
-
if (!t.includes('module')) return;
|
23
|
-
console.log(d, t);
|
24
|
-
viteServer.ws.send({ type: 'full-reload' });
|
25
|
-
});
|
26
|
-
|
27
|
-
// this is middleware for vite's dev servert
|
28
|
-
fastify.addHook('onRequest', async (req, reply) => {
|
29
|
-
const { user } = req.session?.passport || {};
|
30
|
-
const { disableAuth } = req.funcs?.config || {};
|
31
|
-
if (!user && !disableAuth) {
|
32
|
-
// return reply.send('access restricted');
|
33
|
-
}
|
34
|
-
|
35
|
-
const next = () => new Promise((resolve) => {
|
36
|
-
viteServer.middlewares(req.raw, reply.raw, () => resolve());
|
37
|
-
});
|
38
|
-
await next();
|
39
|
-
});
|
40
|
-
fastify.get('*', async () => { });
|
41
|
-
return;
|
42
|
-
}
|
43
|
-
|
44
|
-
// From Build
|
45
|
-
fastify.get('*', async (req, reply) => {
|
46
|
-
const { user } = req.session?.passport || {};
|
47
|
-
|
48
|
-
|
49
|
-
const stream = fs.createReadStream('dist/index.html');
|
50
|
-
return reply.type('text/html').send(stream);
|
51
|
-
});
|
52
|
-
|
53
|
-
async function staticFile(req, reply) {
|
54
|
-
const assetsDir = 'dist';
|
55
|
-
const filePath = path.join(root, assetsDir, req.url);
|
56
|
-
const ext = path.extname(filePath);
|
57
|
-
const mime = {
|
58
|
-
'.js': 'text/javascript', '.css': 'text/css', '.woff2': 'application/font-woff', '.png': 'image/png', '.svg': 'image/svg+xml', '.jpg': 'image/jpg',
|
59
|
-
}[ext];
|
60
|
-
reply.headers({ 'Cache-control': 'max-age=3600, public' });
|
61
|
-
const stream = fs.createReadStream(filePath);
|
62
|
-
return mime ? reply.type(mime).send(stream) : stream;
|
63
|
-
}
|
64
|
-
|
65
|
-
fastify.get('/assets/*', staticFile);
|
66
|
-
fastify.get('/public/*', staticFile);
|
67
|
-
}
|
68
|
-
|
69
|
-
export default plugin;
|
1
|
+
import fs from 'fs';
|
2
|
+
import path, { dirname } from 'path';
|
3
|
+
import { fileURLToPath } from 'url';
|
4
|
+
|
5
|
+
const dir = dirname(fileURLToPath(import.meta.url));
|
6
|
+
const root = `${dir}/../..`;
|
7
|
+
|
8
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
9
|
+
|
10
|
+
async function plugin(fastify) {
|
11
|
+
// vite server
|
12
|
+
if (!isProduction) {
|
13
|
+
const vite = await import('vite');
|
14
|
+
|
15
|
+
const viteServer = await vite.createServer({
|
16
|
+
server: {
|
17
|
+
middlewareMode: true,
|
18
|
+
},
|
19
|
+
});
|
20
|
+
// hot reload
|
21
|
+
viteServer.watcher.on('all', function (d, t) {
|
22
|
+
if (!t.includes('module')) return;
|
23
|
+
console.log(d, t);
|
24
|
+
viteServer.ws.send({ type: 'full-reload' });
|
25
|
+
});
|
26
|
+
|
27
|
+
// this is middleware for vite's dev servert
|
28
|
+
fastify.addHook('onRequest', async (req, reply) => {
|
29
|
+
const { user } = req.session?.passport || {};
|
30
|
+
const { disableAuth } = req.funcs?.config || {};
|
31
|
+
if (!user && !disableAuth) {
|
32
|
+
// return reply.send('access restricted');
|
33
|
+
}
|
34
|
+
|
35
|
+
const next = () => new Promise((resolve) => {
|
36
|
+
viteServer.middlewares(req.raw, reply.raw, () => resolve());
|
37
|
+
});
|
38
|
+
await next();
|
39
|
+
});
|
40
|
+
fastify.get('*', async () => { });
|
41
|
+
return;
|
42
|
+
}
|
43
|
+
|
44
|
+
// From Build
|
45
|
+
fastify.get('*', async (req, reply) => {
|
46
|
+
const { user } = req.session?.passport || {};
|
47
|
+
|
48
|
+
if (!user) return reply.redirect('/login');
|
49
|
+
const stream = fs.createReadStream('dist/index.html');
|
50
|
+
return reply.type('text/html').send(stream);
|
51
|
+
});
|
52
|
+
|
53
|
+
async function staticFile(req, reply) {
|
54
|
+
const assetsDir = 'dist';
|
55
|
+
const filePath = path.join(root, assetsDir, req.url);
|
56
|
+
const ext = path.extname(filePath);
|
57
|
+
const mime = {
|
58
|
+
'.js': 'text/javascript', '.css': 'text/css', '.woff2': 'application/font-woff', '.png': 'image/png', '.svg': 'image/svg+xml', '.jpg': 'image/jpg',
|
59
|
+
}[ext];
|
60
|
+
reply.headers({ 'Cache-control': 'max-age=3600, public' });
|
61
|
+
const stream = fs.createReadStream(filePath);
|
62
|
+
return mime ? reply.type(mime).send(stream) : stream;
|
63
|
+
}
|
64
|
+
|
65
|
+
fastify.get('/assets/*', staticFile);
|
66
|
+
fastify.get('/public/*', staticFile);
|
67
|
+
}
|
68
|
+
|
69
|
+
export default plugin;
|