mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-03-22 06:48:11 +00:00
bring admin.jsx
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
import { createRoot } from "react-dom/client";
|
||||||
|
import Admin from "./admin.jsx";
|
||||||
|
|
||||||
|
const props = window.__INITIAL_PROPS__ || {};
|
||||||
|
|
||||||
|
createRoot(document.getElementById("reactRoot")).render(<Admin {...props} />);
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
import fs from "fs";
|
|
||||||
|
|
||||||
const isProd = process.env.NODE_ENV === "production";
|
|
||||||
|
|
||||||
const template = async function ({ vite, url }, name, title = "", props = {}) {
|
|
||||||
const ogTags = [];
|
|
||||||
const ogMeta = props.ogMeta ?? {};
|
|
||||||
|
|
||||||
Object.entries(ogMeta).forEach(([key, value]) => {
|
|
||||||
if (!value) return;
|
|
||||||
ogTags.push(`<meta property="og:${key}" content="${value}">`);
|
|
||||||
});
|
|
||||||
|
|
||||||
const ogMetaTags = ogTags.join("\n");
|
|
||||||
|
|
||||||
// ----------------
|
|
||||||
// PROD
|
|
||||||
// ----------------
|
|
||||||
if (isProd) {
|
|
||||||
const ssrModule = await import(`../build/entry-server-${name}/bundle.js`);
|
|
||||||
|
|
||||||
return `<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, height=device-height, interactive-widget=resizes-visual" />
|
|
||||||
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700" rel="stylesheet" type="text/css" />
|
|
||||||
<link href="/${name}/bundle.css" rel="stylesheet" />
|
|
||||||
<link rel="icon" href="/assets/favicon.ico" type="image/x-icon" />
|
|
||||||
${ogMetaTags}
|
|
||||||
<meta name="twitter:card" content="summary">
|
|
||||||
<title>${title.length ? `${title} - The Homebrewery` : "The Homebrewery - NaturalCrit"}</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<main id="reactRoot">${ssrModule.default(props)}</main>
|
|
||||||
<script src="/${name}/bundle.js"></script>
|
|
||||||
<script>start_app(${JSON.stringify(props)})</script>
|
|
||||||
</body>
|
|
||||||
</html>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------
|
|
||||||
// DEV
|
|
||||||
// ----------------
|
|
||||||
const { default: render } = await vite.ssrLoadModule(`/client/entry-server-${name}.jsx`);
|
|
||||||
|
|
||||||
let html = `<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, height=device-height, interactive-widget=resizes-visual" />
|
|
||||||
${ogMetaTags}
|
|
||||||
<title>${title.length ? `${title} - The Homebrewery` : "The Homebrewery - NaturalCrit"}</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<main id="reactRoot">${render(props)}</main>
|
|
||||||
|
|
||||||
<script type="module" src="/@vite/client"></script>
|
|
||||||
<script type="module" src="/client/entry-client-${name}.jsx"></script>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>`;
|
|
||||||
|
|
||||||
return vite.transformIndexHtml(url, html);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default template;
|
|
||||||
@@ -18,6 +18,12 @@
|
|||||||
<body>
|
<body>
|
||||||
<main id="reactRoot"></main>
|
<main id="reactRoot"></main>
|
||||||
|
|
||||||
<script type="module" src="/client/homebrew/main.jsx"></script>
|
<script type="module">
|
||||||
|
if (window.location.pathname.startsWith('/admin')) {
|
||||||
|
import('/client/admin/main.jsx');
|
||||||
|
} else {
|
||||||
|
import('/client/homebrew/main.jsx');
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ async function start() {
|
|||||||
server: { middlewareMode: true },
|
server: { middlewareMode: true },
|
||||||
appType: "custom",
|
appType: "custom",
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await DB.connect(config).catch((err) => {
|
await DB.connect(config).catch((err) => {
|
||||||
|
|||||||
@@ -4,18 +4,23 @@ import { model as NotificationModel } from './notifications.model.js';
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import Moment from 'moment';
|
import Moment from 'moment';
|
||||||
import zlib from 'zlib';
|
import zlib from 'zlib';
|
||||||
import templateFn from '../client/template.js';
|
import config from './config.js';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs-extra';
|
||||||
|
|
||||||
|
const nodeEnv = config.get('node_env');
|
||||||
|
const isProd = nodeEnv === 'production';
|
||||||
|
|
||||||
import HomebrewAPI from './homebrew.api.js';
|
import HomebrewAPI from './homebrew.api.js';
|
||||||
import asyncHandler from 'express-async-handler';
|
import asyncHandler from 'express-async-handler';
|
||||||
import { splitTextStyleAndMetadata } from '../shared/helpers.js';
|
import { splitTextStyleAndMetadata } from '../shared/helpers.js';
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
|
|
||||||
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
|
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
|
||||||
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3';
|
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3';
|
||||||
|
|
||||||
|
export default function createAdminApi(vite) {
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
const mw = {
|
const mw = {
|
||||||
adminOnly : (req, res, next)=>{
|
adminOnly : (req, res, next)=>{
|
||||||
if(!req.get('authorization')){
|
if(!req.get('authorization')){
|
||||||
@@ -371,15 +376,28 @@ router.delete('/admin/notification/delete/:id', mw.adminOnly, async (req, res, n
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/admin', mw.adminOnly, (req, res)=>{
|
router.get('/admin', mw.adminOnly, asyncHandler(async (req, res) => {
|
||||||
templateFn('admin', {
|
const props = {
|
||||||
url : req.originalUrl
|
url : req.originalUrl
|
||||||
})
|
};
|
||||||
.then((page)=>res.send(page))
|
|
||||||
.catch((err)=>{
|
const htmlPath = isProd
|
||||||
console.log(err);
|
? path.resolve('build', 'index.html')
|
||||||
res.sendStatus(500);
|
: path.resolve('index.html');
|
||||||
});
|
|
||||||
});
|
let html = fs.readFileSync(htmlPath, 'utf-8');
|
||||||
|
|
||||||
|
if (!isProd && vite?.transformIndexHtml) {
|
||||||
|
html = await vite.transformIndexHtml(req.originalUrl, html);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(html.replace(
|
||||||
|
'<head>',
|
||||||
|
`<head>\n<script id="props">window.__INITIAL_PROPS__ = ${JSON.stringify(props)}</script>`
|
||||||
|
));
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
return router;
|
||||||
|
}
|
||||||
|
|
||||||
export default router;
|
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ export default async function createApp(vite) {
|
|||||||
const isProd = nodeEnv === 'production';
|
const isProd = nodeEnv === 'production';
|
||||||
const isLocalEnvironment = config.get('local_environments').includes(nodeEnv);
|
const isLocalEnvironment = config.get('local_environments').includes(nodeEnv);
|
||||||
|
|
||||||
|
|
||||||
const sanitizeBrew = (brew, accessType)=>{
|
const sanitizeBrew = (brew, accessType)=>{
|
||||||
brew._id = undefined;
|
brew._id = undefined;
|
||||||
brew.__v = undefined;
|
brew.__v = undefined;
|
||||||
@@ -114,7 +113,7 @@ export default async function createApp(vite) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.use(homebrewApi);
|
app.use(homebrewApi);
|
||||||
app.use(adminApi);
|
app.use(adminApi(vite));
|
||||||
app.use(vaultApi);
|
app.use(vaultApi);
|
||||||
|
|
||||||
const welcomeText = fs.readFileSync('./client/homebrew/pages/homePage/welcome_msg.md', 'utf8');
|
const welcomeText = fs.readFileSync('./client/homebrew/pages/homePage/welcome_msg.md', 'utf8');
|
||||||
|
|||||||
Reference in New Issue
Block a user