Server working with Astro and all API endpoints

This commit is contained in:
Alicia Sykes 2024-05-06 21:51:46 +01:00
parent c9e57400fd
commit d9135883de

View File

@ -1,13 +1,22 @@
const express = require('express'); import express from 'express';
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const cors = require('cors'); import cors from 'cors';
const rateLimit = require('express-rate-limit'); import rateLimit from 'express-rate-limit';
const historyApiFallback = require('connect-history-api-fallback'); import historyApiFallback from 'connect-history-api-fallback';
require('dotenv').config(); import dotenv from 'dotenv';
import { handler as ssrHandler } from './dist/server/entry.mjs';
// Load environment variables from .env file
dotenv.config();
// Create the Express app
const app = express(); const app = express();
const __filename = new URL(import.meta.url).pathname;
const __dirname = path.dirname(__filename);
const port = process.env.PORT || 3000; // The port to run the server on const port = process.env.PORT || 3000; // The port to run the server on
const API_DIR = '/api'; // Name of the dir containing the lambda functions const API_DIR = '/api'; // Name of the dir containing the lambda functions
const dirPath = path.join(__dirname, API_DIR); // Path to the lambda functions dir const dirPath = path.join(__dirname, API_DIR); // Path to the lambda functions dir
@ -52,10 +61,13 @@ if (process.env.API_ENABLE_RATE_LIMIT === 'true') {
// Read and register each API function as an Express routes // Read and register each API function as an Express routes
fs.readdirSync(dirPath, { withFileTypes: true }) fs.readdirSync(dirPath, { withFileTypes: true })
.filter(dirent => dirent.isFile() && dirent.name.endsWith('.js')) .filter(dirent => dirent.isFile() && dirent.name.endsWith('.js'))
.forEach(dirent => { .forEach(async dirent => {
const routeName = dirent.name.split('.')[0]; const routeName = dirent.name.split('.')[0];
const route = `${API_DIR}/${routeName}`; const route = `${API_DIR}/${routeName}`;
const handler = require(path.join(dirPath, dirent.name)); // const handler = require(path.join(dirPath, dirent.name));
const handlerModule = await import(path.join(dirPath, dirent.name));
const handler = handlerModule.default || handlerModule;
handlers[route] = handler; handlers[route] = handler;
app.get(route, async (req, res) => { app.get(route, async (req, res) => {
@ -115,13 +127,6 @@ fs.readdirSync(dirPath, { withFileTypes: true })
res.json(results); res.json(results);
}); });
// Handle SPA routing
app.use(historyApiFallback({
rewrites: [
{ from: /^\/api\/.*$/, to: (context) => context.parsedUrl.path },
]
}));
// Serve up the GUI - if build dir exists, and GUI feature enabled // Serve up the GUI - if build dir exists, and GUI feature enabled
if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') { if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') {
app.get('*', async (req, res) => { app.get('*', async (req, res) => {
@ -131,7 +136,6 @@ if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') {
'Web-Check API is up and running!<br />Access the endpoints at ' 'Web-Check API is up and running!<br />Access the endpoints at '
+'<a href="/api"><code>/api</code></a>' +'<a href="/api"><code>/api</code></a>'
); );
res.status(500).send(htmlContent); res.status(500).send(htmlContent);
}); });
} else if (!fs.existsSync(guiPath)) { } else if (!fs.existsSync(guiPath)) {
@ -143,14 +147,32 @@ if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') {
'Run <code>yarn build</code> to continue, then restart the server.' 'Run <code>yarn build</code> to continue, then restart the server.'
); );
res.status(500).send(htmlContent); res.status(500).send(htmlContent);
}); });
} else { // GUI enabled, and build files present, let's go!! } else { // GUI enabled, and build files present, let's go!!
app.use('/', express.static('dist/client/')); app.use(express.static('dist/client/'));
// app.use(express.static(guiPath)); app.use((req, res, next) => {
const locals = {
title: 'New title',
};
ssrHandler(req, res, next, locals);
});
} }
// Handle SPA routing
app.use(historyApiFallback({
rewrites: [
{ from: /^\/api\/.*$/, to: (context) => context.parsedUrl.path },
{ from: /^.*$/, to: '/index.html' }
]
}));
// Anything left unhandled (which isn't an API endpoint), return a 404
app.use((req, res, next) => { app.use((req, res, next) => {
if (!req.path.startsWith('/api/')) {
res.status(404).sendFile(path.join(__dirname, 'public', 'error.html')); res.status(404).sendFile(path.join(__dirname, 'public', 'error.html'));
} else {
next();
}
}); });
// Print nice welcome message to user // Print nice welcome message to user