Added the cdn v2

This commit is contained in:
Mathias Wagner 2022-07-19 21:17:59 +02:00
parent 28e8171183
commit 0233a2866b
8 changed files with 3897 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/cdn/ && ssh-agent bash -c 'ssh-add ~/.ssh/cdn_rsa; git reset --hard && git pull origin master && git pull origin master' && npm i && cd /var/www/ && pm2 restart cdn"

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

4
ContentDeliveryV2/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# Project exclude paths
/node_modules/
/.env
/.idea

32
ContentDeliveryV2/app.js Normal file
View File

@ -0,0 +1,32 @@
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const mime = require('mime');
const database = require("./config/database");
const media = require("./models/Media");
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("/:resourceID", (req, res) => {
const resource = req.params.resourceID.split(".")
if (resource.length < 2)
return res.status(400).json({message: "The specified URL is not in the correct format"});
media.findOne({where: {assetID: resource[0], assetEnding: resource[resource.length - 1]}})
.then(r => {
require('fs').readFile(process.env.CDN_MEDIA_PATH+"/"+r.assetID, (err, data) =>
res.header("Content-Type", mime.lookup(r.assetEnding)).send(data)
);
})
.catch(() => res.status(404).json({message: "The specified file was not found."}));
});
app.get('*', (req, res) => {
res.status(400).json({message: "The specified URL is not in the correct format"});
});
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,25 @@
const Sequelize = require('sequelize');
const db = require("../config/database");
module.exports = db.define("media", {
assetID: {
type: Sequelize.STRING,
allowNull: false
},
assetCreator: {
type: Sequelize.STRING,
allowNull: false
},
assetEnding: {
type: Sequelize.STRING,
allowNull: false
},
assetName: {
type: Sequelize.STRING,
allowNull: false
},
assetDescription: {
type: Sequelize.STRING,
defaultValue: "Default sheepstar asset"
}
});

3761
ContentDeliveryV2/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,24 @@
{
"name": "sheepstarcdn",
"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",
"mime": "^1.6.0",
"mysql2": "^2.2.5",
"sequelize": "^6.6.2"
}
}