diff options
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | routes/api/index.js | 15 | ||||
| -rw-r--r-- | routes/api/private/admin.js (renamed from routes/api/admin.js) | 2 | ||||
| -rw-r--r-- | routes/api/private/user.js (renamed from routes/api/user.js) | 0 | ||||
| -rw-r--r-- | routes/api/public/screenshot.js | 46 | ||||
| -rw-r--r-- | routes/api/screenshot.js | 24 | ||||
| -rw-r--r-- | server.js | 6 |
7 files changed, 62 insertions, 33 deletions
@@ -15,7 +15,7 @@ If you would like to contribute to the website, please see the [contributing gui Below is the screenshot of how the website looks like right now. This screenshot is being updated programatically by [That Computer Scientist's Screenshot API](https://api.thatcomputerscientist.com/screenshot), and will update as I push more changes to the repository. - + ## Cloning, Installing, and Running diff --git a/routes/api/index.js b/routes/api/index.js index 774828be..1b61ce45 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -1,10 +1,19 @@ const router = require("express").Router(); -const admin = require("./admin"); -const user = require("./user"); -const screenshot = require("./screenshot"); +const admin = require("./private/admin"); +const user = require("./private/user"); +const screenshot = require("./public/screenshot"); router.use("/admin", admin); router.use("/user", user); router.use("/screenshot", screenshot); +router.get("/", (req, res) => { + res.status(200).json({ + message: "Welcome to That Computer Scientist's Public API Service.", + apis: { + screenshot: "/screenshot", + } + }); +}); + module.exports = router; diff --git a/routes/api/admin.js b/routes/api/private/admin.js index dd5d2744..8b7d7d5f 100644 --- a/routes/api/admin.js +++ b/routes/api/private/admin.js @@ -1,7 +1,7 @@ const router = require("express").Router(); const mysql = require("mysql2"); const bcrypt = require("bcryptjs"); -const validateAuthorization = require("../../functions/validate"); +const validateAuthorization = require("../../../functions/validate"); require("dotenv").config(); const connectionURL = process.env.DATABASE_URL; diff --git a/routes/api/user.js b/routes/api/private/user.js index 8874d2e5..8874d2e5 100644 --- a/routes/api/user.js +++ b/routes/api/private/user.js diff --git a/routes/api/public/screenshot.js b/routes/api/public/screenshot.js new file mode 100644 index 00000000..6c8c1fe0 --- /dev/null +++ b/routes/api/public/screenshot.js @@ -0,0 +1,46 @@ +const router = require("express").Router(); +const puppeteer = require("puppeteer"); +const fs = require("fs"); +const yaml = require("yaml"); +const config = yaml.parse(fs.readFileSync("site.config.yml", "utf8")); + +router.get("/", async (req, res) => { + const width = parseInt(req.query.width) ? parseInt(req.query.width) : 1920; + const height = parseInt(req.query.height) ? parseInt(req.query.height) : 1080; + const url = req.query.url || config.url; + const format = req.query.format ? ['png', 'jpeg', 'webp'].includes(req.query.format) ? req.query.format : 'png' : 'png'; + const fullpage = req.query.fullpage || true; + + // Set screenshot options + const options = { type: format, fullPage: fullpage }; + + // Take a screenshot + const image = async (url, width, height, options) => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + page.setViewport({ width, height }); + // Go to the url + await page.goto(url, { waitUntil: "networkidle2" }); + // Take a screenshot + const screenshot = await page.screenshot(options); + // Close the browser + await browser.close(); + // Return the screenshot + return screenshot; + } + + // Get the screenshot + image(url, width, height, options).then(screenshot => { + // Send the screenshot + res.setHeader("Content-Type", `image/${format}`); + res.send(screenshot); + }).catch(err => { + // Send an error + res.status(500).json({ + message: "Error", + error: err + }); + }); +}); + +module.exports = router; diff --git a/routes/api/screenshot.js b/routes/api/screenshot.js deleted file mode 100644 index 06340981..00000000 --- a/routes/api/screenshot.js +++ /dev/null @@ -1,24 +0,0 @@ -const router = require("express").Router(); -const puppeteer = require("puppeteer"); -const fs = require("fs"); -const yaml = require("yaml"); -const config = yaml.parse(fs.readFileSync("site.config.yml", "utf8")); - -router.get("/", async (req, res) => { - const browser = await puppeteer.launch(); - const page = await browser.newPage(); - page.setViewport({ width: 1920, height: 1080 }); - // Go to the url - await page.goto(config.url, { - waitUntil: "networkidle2", - }); - // Take a screenshot - const image = await page.screenshot({fullPage : true}); - await browser.close(); - // Send the image as a response - res.setHeader("Content-Type", "image/png"); - res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); - res.send(image); -}); - -module.exports = router; @@ -32,10 +32,8 @@ app.use( }) ); -// Setup API subdomain -app.use( - subdomain("api", require("./routes/api")) -); +app.use(subdomain("screenshot.api", require("./routes/api/public/screenshot.js"))); +app.use(subdomain("api", require("./routes/api"))); // Set Template Engine app.set("view engine", "ejs"); |
