Added the shortener

This commit is contained in:
Mathias Wagner 2022-07-19 21:20:23 +02:00
parent 0233a2866b
commit 2e16cfa2cb
8 changed files with 3911 additions and 0 deletions

View File

@ -0,0 +1,17 @@
name: Deploy to SSH
on:
push:
branches: [ master ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy NodeJS app
uses: garygrossgarten/github-action-ssh@release
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
privateKey: ${{ secrets.REMOTE_SSH_KEY }}
command: "cd /var/www/shortener/ && ssh-agent bash -c 'ssh-add ~/.ssh/shortener_rsa; git reset --hard && git pull origin master && git pull origin master' && npm i && cd /var/www/ && pm2 restart shortener"

21
ShortenerV1/.github/workflows/node.yml vendored Normal file
View File

@ -0,0 +1,21 @@
name: Node.js test
on:
push:
pull_request:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present

2
ShortenerV1/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Project exclude paths
/node_modules/

38
ShortenerV1/app.js Normal file
View File

@ -0,0 +1,38 @@
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const database = require("./config/database");
const ShortenedLink = require("./models/ShortenedLink");
const app = express();
database.authenticate()
.then(() => console.log("Connected to " + process.env.MYSQL_HOSTNAME + " with user " + process.env.MYSQL_USERNAME))
.catch(err => console.log("An error occurred while connecting to database: " + err));
app.use(cors());
app.get("/:shortedID", async (req, res) => {
const link = await ShortenedLink.findOne({where: {shorten_url: req.params.shortedID}});
if (link) {
if (link.show_meta_data) {
let html = "<html><head>";
html += getMeta("og:title", link.meta_title);
html += getMeta("og:description", link.meta_description);
html += getMeta("og:image", link.meta_image);
html += getMeta("theme-color", link.meta_color);
html += '<meta http-equiv="refresh" content="0; url=' + link.original_url + '" />';
html += "</head></html>";
res.status(200).send(html);
} else res.redirect(link.original_url);
} else res.status(404).json({message: "The specified link wasn't found"});
});
function getMeta(name, content) {
return '<meta name="' + name + '" content="'+content+'" />';
}
app.get('*', (req, res) => {
res.redirect("https://sheepstar.xyz/");
});
app.listen(process.env.SERVER_PORT || 3000);

View File

@ -0,0 +1,13 @@
const { Sequelize } = require("sequelize");
module.exports = new Sequelize(process.env.MYSQL_DATABASE, process.env.MYSQL_USERNAME, process.env.MYSQL_PASSWORD, {
host: process.env.MYSQL_HOSTNAME,
dialect: 'mysql',
logging: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
});

View File

@ -0,0 +1,37 @@
const Sequelize = require('sequelize');
const db = require("../config/database");
module.exports = db.define("shortened_links",{
shorten_url: {
type: Sequelize.STRING,
allowNull: false
},
original_url: {
type: Sequelize.STRING,
allowNull: false
},
show_meta_data: {
type: Sequelize.BOOLEAN,
defaultValue: false
},
meta_title: {
type: Sequelize.STRING,
defaultValue: "Default URL title",
allowNull: true
},
meta_description: {
type: Sequelize.STRING,
defaultValue: "Default URL description",
allowNull: true
},
meta_image: {
type: Sequelize.STRING,
defaultValue: "Default URL image",
allowNull: true
},
meta_color: {
type: Sequelize.STRING,
defaultValue: "#5865f2",
allowNull: true
}
});

3760
ShortenerV1/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

23
ShortenerV1/package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "sheepstarshortener",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.7"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mysql2": "^2.2.5",
"sequelize": "^6.6.2"
}
}