Skip to content

Commit

Permalink
Merge pull request simonlourson#22 from Sinetheta/bots-preview
Browse files Browse the repository at this point in the history
Add link previews for social sharing
  • Loading branch information
Sinetheta authored Apr 8, 2023
2 parents d009177 + 7ad2dc5 commit e29e73c
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class App {

// Create a new express application instance and add middleware
this.app = express();
this.app.set('views', path.join(__dirname, 'views'));
this.app.set('view engine', 'ejs');

this.app.use(helmet.contentSecurityPolicy({
directives: {
Expand Down
Binary file added app/public/images/site-preview-large-square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/public/images/site-preview-large-wide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 10 additions & 10 deletions app/routes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Request, Response, Application } from "express";
import express from 'express';
import path from 'path';
import { Application } from "express";
import express from 'express';
import expressJwt from 'express-jwt'

import { StaticController } from './static-controller';
import { LoginController } from './api/login-controller';
import { RegisterController } from "./api/register-controller";
import { DuplicateCheckController } from "./api/duplicate-check-controller";
import { BlueprintController } from "./api/blueprint-controller";
var Recaptcha = require('express-recaptcha').RecaptchaV3;

export class Routes {
public staticController = new StaticController();
public loginController = new LoginController();
public registerController = new RegisterController();
public duplicateCheckController = new DuplicateCheckController();
Expand All @@ -18,10 +19,10 @@ export class Routes {
public routes(app: Application): void {
// Initialize authentication middleware
//let auth = expressJwt({secret: process.env.JWT_SECRET as string, userProperty: 'tokenPayload' });
let auth = expressJwt({secret: process.env.JWT_SECRET as string });
let auth = expressJwt({ secret: process.env.JWT_SECRET as string });
let recaptcha = new Recaptcha(process.env.CAPTCHA_SITE as string, process.env.CAPTCHA_SECRET as string);


if (process.env.ENV_NAME == 'development') {
console.log('Initializing routes without recaptcha verification');
app.route("/api/login").post(this.loginController.login);
Expand All @@ -32,7 +33,7 @@ export class Routes {
app.route("/api/login").post(recaptcha.middleware.verify, this.loginController.login);
app.route("/api/register").post(recaptcha.middleware.verify, this.registerController.register);
}

// Anonymous access
app.route("/api/checkusername").get(this.duplicateCheckController.checkUsername);
app.route("/api/getblueprint/:id").get(this.uploadBlueprintController.getBlueprint);
Expand All @@ -46,9 +47,8 @@ export class Routes {
app.route("/api/likeblueprint").post(auth, this.uploadBlueprintController.likeBlueprint);
app.route("/api/deleteblueprint").post(auth, this.uploadBlueprintController.deleteBlueprint);

app.get('/', this.staticController.serveHtml);
app.use(express.static(path.join(__dirname, "public")));
app.get('*', function(req, res){
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('*', this.staticController.serveHtml);
}
}
}
13 changes: 13 additions & 0 deletions app/static-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from 'path';
import { Request, Response } from "express";
import isbot from 'isbot'

export class StaticController {
public serveHtml(req: Request, res: Response) {
if (isbot(req.get('user-agent'))) {
res.render('index-robots');
} else {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
}
}
}
24 changes: 24 additions & 0 deletions app/views/index-robots.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta property="og:type" content="website" />
<meta property="og:title" content="Blueprint Not Included" />
<meta property="og:description" content="A place to upload, build, and share your favourite blueprint designs for Oxygen Not Included." />

<!-- Discord bot will respect "og:image:url" but needs a square image for small previews -->
<meta property="og:image:url" content="/images/site-preview-large-square.png" />
<meta property="ogimage:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="1200" />
<meta property="og:image:alt" content="An example blueprint, shown against the website editor." />

<!-- Discord bot will ignore "og:image:url" but can display a wide image -->
<meta property="og:image" content="/images/site-preview-large-wide.png" />
<meta property="ogimage:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="671" />
<meta property="og:image:alt" content="An example blueprint, shown against the website editor, next to the blueprint not included logo." />
</head>
<body>
</body>
</html>
106 changes: 103 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
"scripts": {
"tsc": "tsc -b",
"dev": "ts-node-dev --respawn --transpileOnly ./app/server.ts",
"prod": "tsc -b && node ./build/server.js",
"prebuild": "rm -rf build/",
"build": "npm run build:backend && npm run build:lib && npm run build:frontend",
"build:backend": "tsc -b",
"build:lib": "(cd lib && tsc -b)",
"build:frontend": "(cd frontend && npm run build)",
"postbuild": "npm run copy:libjs && npm run copy:views && npm run copy:public && npm run copy:frontend",
"copy:libjs": "rsync -amv --include='*.js' --include='*/' --exclude='*' lib/ build/lib",
"copy:views": "rsync -zarv --prune-empty-dirs --include '*/' --include='*.ejs' --exclude='*' 'app' 'build'",
"copy:frontend": "rsync -amv frontend/dist/blueprintnotincluded/ build/app/public",
"copy:public": "rsync -amv app/public/ build/app/public",
"serve:prod": "node ./build/app/server.js",
"updateBasedOn": "tsc && node ./build/api/batch/update-based-on.js",
"updatePositionCorrection": "tsc && node ./build/api/batch/update-position-correction.js",
"updateThumbnail": "ts-node-dev --expose_gc --max_old_space_size=4096 --transpileOnly ./app/api/batch/update-thumbnail.ts",
Expand All @@ -27,10 +37,12 @@
"crypto-js": "^3.1.9-1",
"csharp-binary-stream": "^1.0.2",
"dotenv": "^8.2.0",
"ejs": "^3.1.9",
"express": "^4.17.1",
"express-jwt": "^5.3.1",
"express-recaptcha": "^5.0.1",
"helmet": "^4.1.0",
"isbot": "^3.6.8",
"jimp": "^0.14.0",
"jsdom": "^16.3.0",
"jsdom-global": "^3.0.2",
Expand Down

0 comments on commit e29e73c

Please sign in to comment.