1
0
Fork 0

sparkles: Added landing page for catch-all route and config verification

This commit is contained in:
Joost De Cock 2019-07-03 15:41:05 +02:00 committed by Joost De Cock
parent 3335643e90
commit 3cf44ecd04
4 changed files with 189 additions and 30 deletions

View file

@ -18,32 +18,32 @@ export default {
saltRounds: 10
},
encryption: {
key: process.env.MONGO_ENC_KEY,
key: process.env.FS_ENC_KEY || '', // Prevent mongoose plugin from throwing an error
},
jwt: {
secretOrKey: process.env.MONGO_ENC_KEY,
issuer: process.env.JWT_ISSUER,
audience: process.env.JWT_ISSUER,
secretOrKey: process.env.FS_ENC_KEY,
issuer: process.env.FS_JWT_ISSUER,
audience: process.env.FS_JWT_ISSUER,
expiresIn: "36 days",
},
languages: ["en", "de", "es", "fr", "nl"],
smtp: {
host: process.env.SMTP_HOST,
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
host: process.env.FS_SMTP_HOST,
user: process.env.FS_SMTP_USER,
pass: process.env.FS_SMTP_PASS,
},
oauth: {
github: {
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
clientId: process.env.FS_GITHUB_CLIENT_ID,
clientSecret: process.env.FS_GITHUB_CLIENT_SECRET,
tokenUri: "https://github.com/login/oauth/access_token",
dataUri: "https://api.github.com/user"
},
google: {
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
clientId: process.env.FS_GOOGLE_CLIENT_ID,
clientSecret: process.env.FS_GOOGLE_CLIENT_SECRET,
tokenUri: "https://oauth2.googleapis.com/token",
dataUri: "https://people.googleapis.com/v1/people/me?personFields=emailAddresses,names,photos"
}

View file

@ -0,0 +1,92 @@
const verifyConfig = (config, chalk) => {
const nonEmptyString = input => {
if (typeof input === "string" && input.length > 0) return true;
return false;
}
const warnings = [];
const errors = [];
// Required (error when missing)
//
// Database
if (!nonEmptyString(config.db.uri))
errors.push({ e: "FS_MONGO_URI", i: "mongo" });
// Encryption
if (!nonEmptyString(config.encryption.key))
errors.push({ e: "FS_ENC_KEY", i: "encryption" });
// JWT
if (!nonEmptyString(config.jwt.issuer))
errors.push({ e: "FS_JWT_ISSUER", i: "jwt" });
// Wanted (warning when missing)
//
// API
if (!nonEmptyString(config.api))
warnings.push({ e: "FS_BACKEND", i: "api" });
// Site
if (!nonEmptyString(config.api))
warnings.push({ e: "FS_SITE", i: "site" });
// SMTP
if (!nonEmptyString(config.smtp.host))
warnings.push({ e: "FS_SMTP_HOST", i: "smtp" });
if (!nonEmptyString(config.smtp.user))
warnings.push({ e: "FS_SMTP_USER", i: "smtp" });
if (!nonEmptyString(config.smtp.pass))
warnings.push({ e: "FS_SMTP_PASS", i: "smtp" });
// OAUTH
if (!nonEmptyString(config.oauth.github.clientId))
warnings.push({ e: "FS_GITHUB_CLIENT_ID", i: "oauth" });
if (!nonEmptyString(config.oauth.github.clientSecret))
warnings.push({ e: "FS_GITHUB_CLIENT_SECRET", i: "oauth" });
if (!nonEmptyString(config.oauth.google.clientId))
warnings.push({ e: "FS_GOOGLE_CLIENT_ID", i: "oauth" });
if (!nonEmptyString(config.oauth.google.clientSecret))
warnings.push({ e: "FS_GOOGLE_CLIENT_SECRET", i: "oauth" });
for (let {e, i} of warnings) {
console.log(
chalk.yellow("Warning:"),
"Missing",
chalk.yellow(e),
"environment variable. Some features won't be available.",
"\n",
chalk.yellow("See: "),
chalk.yellow.bold("https://dev.freesewing.org/backend/configuration#"+i),
"\n",
);
}
for (let {e, i} of errors) {
console.log(
chalk.redBright("Error:"),
"Required environment variable",
chalk.redBright(e),
"is missing. The backend won't start without it.",
"\n",
chalk.yellow("See: "),
chalk.yellow.bold("https://dev.freesewing.org/backend/configuration#"+i),
"\n",
);
}
if (errors.length > 0) {
console.log(chalk.redBright("Invalid configuration. Stopping here..."));
return process.exit(1);
}
return true;
}
export default verifyConfig;

View file

@ -2,11 +2,17 @@ import express from "express";
import mongoose from "mongoose";
import chalk from "chalk";
import passport from "passport";
import config from "./config";
import config from "./config/index";
import verifyConfig from "./config/verify";
import expressMiddleware from "./middleware/express";
import passportMiddleware from "./middleware/passport";
import routes from "./routes";
import path from "path";
// Verify configuration
verifyConfig(config, chalk);
// Start Express
const app = express();
// Load Express middleware
@ -38,25 +44,14 @@ mongoose
process.exit();
});
app.get("/", async (req, res) => {
try {
const thing = await Promise.resolve({ one: "two" }); // async/await!
return res.json({ ...thing, hello: "world" }); // object-rest-spread!
} catch (e) {
return res.json({ error: e.message });
}
});
// Catch-all route
app.get("/", async (req, res)
=> res.sendFile(path.resolve(__dirname, "landing", "index.html")));
const port = process.env.PORT || 3000;
app.listen(port, err => {
if (err) {
console.error('Error occured', err);
}
if (__DEV__) {
// webpack flags!
console.log("> in development");
}
console.log(`> listening on port ${port}`);
if (err) console.error(chalk.red('Error occured'), err);
if (__DEV__) console.log(chalk.yellow("> in development"));
console.log(chalk.green(`> listening on port ${port}`));
});

File diff suppressed because one or more lines are too long