mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: modifed server.js to server.ts
This commit is contained in:
parent
a7397b74a0
commit
9b4b8a7f24
@ -1,44 +1,49 @@
|
|||||||
const path = require('path');
|
import path from 'path';
|
||||||
const fs = require('fs');
|
import * as fs from 'fs';
|
||||||
const pino = require('pino');
|
import pino from 'pino';
|
||||||
const cheerio = require('cheerio');
|
import { type CheerioAPI, load } from 'cheerio';
|
||||||
const { fetch } = require('bun');
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-expect-error
|
||||||
|
import { fetch } from 'bun';
|
||||||
|
|
||||||
const distDir = path.join(__dirname, 'dist');
|
const distDir = path.join(__dirname, 'dist');
|
||||||
const indexPath = path.join(distDir, 'index.html');
|
const indexPath = path.join(distDir, 'index.html');
|
||||||
const baseURL = process.env.AF_BASE_URL;
|
const baseURL = process.env.AF_BASE_URL as string;
|
||||||
const setOrUpdateMetaTag = ($, selector, attribute, content) => {
|
const defaultSite = 'https://appflowy.io';
|
||||||
|
|
||||||
|
const setOrUpdateMetaTag = ($: CheerioAPI, selector: string, attribute: string, content: string) => {
|
||||||
if ($(selector).length === 0) {
|
if ($(selector).length === 0) {
|
||||||
$('head').append(`<meta ${attribute}="${selector.match(/\[(.*?)\]/)[1]}" content="${content}">`);
|
$('head').append(`<meta ${attribute}="${selector.match(/\[(.*?)\]/)?.[1]}" content="${content}">`);
|
||||||
} else {
|
} else {
|
||||||
$(selector).attr('content', content);
|
$(selector).attr('content', content);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a new logger instance
|
|
||||||
const logger = pino({
|
const logger = pino({
|
||||||
transport: {
|
transport: {
|
||||||
target: 'pino-pretty',
|
target: 'pino-pretty',
|
||||||
level: 'info',
|
|
||||||
options: {
|
options: {
|
||||||
colorize: true,
|
colorize: true,
|
||||||
translateTime: 'SYS:standard',
|
translateTime: 'SYS:standard',
|
||||||
destination: `${__dirname}/pino-logger.log`,
|
destination: `${__dirname}/pino-logger.log`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
level: 'info',
|
||||||
});
|
});
|
||||||
|
|
||||||
const logRequestTimer = (req) => {
|
const logRequestTimer = (req: Request) => {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const pathname = new URL(req.url).pathname;
|
const pathname = new URL(req.url).pathname;
|
||||||
|
|
||||||
logger.info(`Incoming request: ${pathname}`);
|
logger.info(`Incoming request: ${pathname}`);
|
||||||
return () => {
|
return () => {
|
||||||
const duration = Date.now() - start;
|
const duration = Date.now() - start;
|
||||||
|
|
||||||
logger.info(`Request for ${pathname} took ${duration}ms`);
|
logger.info(`Request for ${pathname} took ${duration}ms`);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchMetaData = async (url) => {
|
const fetchMetaData = async (url: string) => {
|
||||||
logger.info(`Fetching meta data from ${url}`);
|
logger.info(`Fetching meta data from ${url}`);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
@ -56,29 +61,24 @@ const fetchMetaData = async (url) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const createServer = async (req) => {
|
const createServer = async (req: Request) => {
|
||||||
const timer = logRequestTimer(req);
|
const timer = logRequestTimer(req);
|
||||||
const reqUrl = new URL(req.url);
|
const reqUrl = new URL(req.url);
|
||||||
|
|
||||||
const hostname = req.headers.get('host');
|
const hostname = req.headers.get('host');
|
||||||
|
|
||||||
logger.info(`Request URL: ${hostname}${reqUrl.pathname}`);
|
logger.info(`Request URL: ${hostname}${reqUrl.pathname}`);
|
||||||
|
|
||||||
const [
|
const [namespace, publishName] = reqUrl.pathname.slice(1).split('/');
|
||||||
namespace,
|
|
||||||
publishName,
|
|
||||||
] = reqUrl.pathname.slice(1).split('/');
|
|
||||||
|
|
||||||
logger.info(`Namespace: ${namespace}, Publish Name: ${publishName}`);
|
logger.info(`Namespace: ${namespace}, Publish Name: ${publishName}`);
|
||||||
|
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
|
|
||||||
if (namespace === '' || !publishName) {
|
if (namespace === '' || !publishName) {
|
||||||
timer();
|
timer();
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 302,
|
status: 302,
|
||||||
headers: {
|
headers: {
|
||||||
'Location': 'https://appflowy.io',
|
Location: defaultSite,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -91,28 +91,27 @@ const createServer = async (req) => {
|
|||||||
logger.error(`Error fetching meta data: ${error}`);
|
logger.error(`Error fetching meta data: ${error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let htmlData = fs.readFileSync(indexPath, 'utf8');
|
const htmlData = fs.readFileSync(indexPath, 'utf8');
|
||||||
const $ = cheerio.load(htmlData);
|
const $ = load(htmlData);
|
||||||
|
|
||||||
const description = 'Write, share, and publish docs quickly on AppFlowy.\nGet started for free.';
|
const description = 'Write, share, and publish docs quickly on AppFlowy.\nGet started for free.';
|
||||||
let title = 'AppFlowy';
|
let title = 'AppFlowy';
|
||||||
const url = 'https://appflowy.io';
|
const url = `https://${hostname}${reqUrl.pathname}`;
|
||||||
let image = '/og-image.png';
|
let image = '/og-image.png';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Inject meta data into the HTML to support SEO and social sharing
|
|
||||||
if (metaData && metaData.view) {
|
if (metaData && metaData.view) {
|
||||||
title = `${metaData.view.name} | AppFlowy`;
|
title = `${metaData.view.name} | AppFlowy`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cover = metaData.view.extra ? JSON.parse(metaData.view.extra)?.cover : null;
|
const cover = metaData.view.extra ? JSON.parse(metaData.view.extra)?.cover : null;
|
||||||
|
|
||||||
if (cover) {
|
if (cover) {
|
||||||
if (['unsplash', 'custom'].includes(cover.type)) {
|
if (['unsplash', 'custom'].includes(cover.type)) {
|
||||||
image = cover.value;
|
image = cover.value;
|
||||||
} else if (cover.type === 'built_in') {
|
} else if (cover.type === 'built_in') {
|
||||||
image = `/covers/m_cover_image_${cover.value}.png`;
|
image = `/covers/m_cover_image_${cover.value}.png`;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
@ -147,8 +146,13 @@ const createServer = async (req) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
declare const Bun: {
|
||||||
|
serve: (options: { port: number; fetch: typeof createServer; error: (err: Error) => Response }) => void;
|
||||||
|
};
|
||||||
|
|
||||||
const start = () => {
|
const start = () => {
|
||||||
try {
|
try {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
Bun.serve({
|
Bun.serve({
|
||||||
port: 3000,
|
port: 3000,
|
||||||
fetch: createServer,
|
fetch: createServer,
|
||||||
@ -157,7 +161,7 @@ const start = () => {
|
|||||||
return new Response('Internal Server Error', { status: 500 });
|
return new Response('Internal Server Error', { status: 500 });
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
logger.info(`Server is running on port 3000`);
|
logger.info('Server is running on port 3000');
|
||||||
logger.info(`Base URL: ${baseURL}`);
|
logger.info(`Base URL: ${baseURL}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
@ -166,3 +170,5 @@ const start = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
start();
|
start();
|
||||||
|
|
||||||
|
export {};
|
@ -6,7 +6,7 @@
|
|||||||
service nginx start
|
service nginx start
|
||||||
|
|
||||||
# Start the frontend server
|
# Start the frontend server
|
||||||
bun run server.js
|
bun run server.ts
|
||||||
|
|
||||||
tail -f /dev/null
|
tail -f /dev/null
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@
|
|||||||
"vite.config.ts",
|
"vite.config.ts",
|
||||||
"cypress.config.ts",
|
"cypress.config.ts",
|
||||||
"cypress",
|
"cypress",
|
||||||
"deploy/server.js"
|
"deploy/server.ts"
|
||||||
],
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
|
Loading…
Reference in New Issue
Block a user