From ce4faeb82887175ed845c29d547f84eea51a6daf Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 24 Jan 2022 23:48:52 +0300 Subject: [PATCH] fix inaccurate console logs (ft. my dumbass) --- index.js | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..24d8d48 --- /dev/null +++ b/index.js @@ -0,0 +1,203 @@ + +const express = require('express'); +const app = express(); +const fs = require('fs'); +require('dotenv').config(); + +// Use tycrek's logging library for fancy messages +const TLog = require('@tycrek/log'); +const logger = new TLog(); + +const logger2 = new TLog({ + timestamp: { + enabled: false + } + /* Options can be set here */ +}); + +// Check if a custom port is set in .env +// if it is, then check if a custom url is defined. Paste the correct IP:port combo or the custom url. +// You will need to handle domain -> raw IP redirection yourself. +if(process.env.PORT) { + const port = process.env.PORT; + logger2 + .success('Custom port is set. Using custom port ' + port) + app.listen(port, () => { + if(process.env.CUSTOMURL) { + const customurl = process.env.CUSTOMURL; + logger2 + .success('Custom URL defined as ' + customurl) + .info('App listening at ' + 'https://' + customurl) + .comment('Godspeed, little fella!') + const getRandomImageApi = () => { + const path = getRandomImagePath(); + const id = path.substring(0, path.length - 4).split('-')[1]; + return { + id: parseInt(id), + url: 'https://' + customurl + id, + }; + } + + app.get('/api/random', (req, res) => { + return res.json(getRandomImageApi()); + }); + + app.get('/api/:id', (req, res) => { + const image = getImageById(req.params.id, false); + + if (image) { + return res.json({ + id: parseInt(req.params.id), + url: 'https://' + customurl + '/' + req.params.id, + }); + + } + else { + return res.json({ + error: 'Image not found' + }); + } + }); + + } + else { + logger2 + .error('Custom URL not defined. Falling back to http://localhost:' + port) + .comment('Godspeed, little fella!') + const getRandomImageApi = () => { + const path = getRandomImagePath(); + const id = path.substring(0, path.length - 4).split('-')[1]; + return { + id: parseInt(id), + url: 'http://localhost:' + port + '/' + id, + }; + } + app.get('/api/random', (req, res) => { + return res.json(getRandomImageApi()); + }); + app.get('/api/:id', (req, res) => { + const image = getImageById(req.params.id, false); + if (image) { + return res.json({ + id: parseInt(req.params.id), + url: 'http://localhost:' + port + '/' + req.params.id, + }); + } else { + return res.json({ + error: 'Image not found' + }); + } + }); + + } + }); +} + +else { + const port = 3005; + logger2 + .error('Custom port is not set. Falling back to port ' + port); + app.listen(port, () => { + if(process.env.CUSTOMURL) { + logger2 + .success('Custom URL defined. Using custom url ' + customurl + '\n(Hope you have configured a reverse proxy.)') + .success('App listening at ' + 'https://' + customurl); + console.log('Godspeed, little fella!'); + const getRandomImageApi = () => { + const path = getRandomImagePath(); + const id = path.substring(0, path.length - 4).split('-')[1]; + return { + id: parseInt(id), + url: 'http://' + customurl + '/' + id, + }; + } + app.get('/api/random', (req, res) => { + return res.json(getRandomImageApi()); + }); + + } + else { + logger2 + .error('Custom URL not defined. Falling back to http://localhost:' + port) + .info('App listening at ' + 'http://localhost:' + port) + .comment('Godspeed, little fella!') + const getRandomImageApi = () => { + const path = getRandomImagePath(); + const id = path.substring(0, path.length - 4).split('-')[1]; + return { + id: parseInt(id), + url: 'http://localhost:' + port + '/' + id, + }; + } + + app.get('/api/random', (req, res) => { + return res.json(getRandomImageApi()); + }); + + } + }); +} + +// Check if a custom port is set in .env +// if it is, then check if a custom url is defined. +// Makes sure API routes are configured correctly. + + +app.get('/', (req, res) => { + res.sendFile(getRandomImagePath(), (err) => { + if (err) { + res.status(err.status).end(); + } + }); +}); + +app.get('/:id', (req, res) => { + const image = getImageById(req.params.id); + if (image) { + res.sendFile(image, (err) => { + if (err) { + res.status(err.status).end(); + } + }); + } else { + return res.json({ + error: 'Image not found' + }); + } +}); + +app.get('/api/list', (req, res) => { + return res.json(getAllImageIds()); +}); + + +const getRandomImagePath = () => { + const images = fs.readdirSync('./images'); + return __dirname + '/images/' + images[Math.floor(Math.random() * images.length)]; +}; + +const getImageById = (id, path = true) => { + const images = fs.readdirSync('./images'); + + for (const image of images) { + if (image.substring(0, image.length - 4).split('-')[1] === id) { + if (path) { + return __dirname + '/images/' + image; + } else { + return image; + } + } + } + + return null; +}; + +const getAllImageIds = () => { + const ids = []; + + fs.readdirSync('./images').forEach(image => { + ids.push(parseInt(image.substring(0, image.length - 4) .split('-')[1])); + }); + + return ids.sort((a, b) => a - b); +};