s.divlo.fr : linkShortener

This commit is contained in:
divlo 2020-08-03 17:15:32 +02:00
parent 653068c9c7
commit fbfd8899e9
12 changed files with 3651 additions and 10 deletions

View File

@ -5,7 +5,7 @@ const express = require('express')
const helmet = require('helmet')
const cors = require('cors')
const morgan = require('morgan')
const redirectToHTTPS = require('express-http-to-https').redirectToHTTPS
const { redirectToHTTPS } = require('express-http-to-https')
/* Files Imports & Variables */
const sequelize = require('./assets/utils/database')

View File

@ -3,6 +3,8 @@ const errorHandling = require('../../utils/errorHandling')
const { requiredFields, serverError } = require('../../config/errors')
const shortLinks = require('../../../models/short_links')
const shortLinkBaseURL = 'https://s.divlo.fr'
module.exports = async ({ res, next }, argsObject) => {
let { url, shortcutName } = argsObject
@ -37,7 +39,7 @@ module.exports = async ({ res, next }, argsObject) => {
// Si l'url a déjà été raccourcie
const urlInDatabase = await shortLinks.findOne({ where: { url } })
if (urlInDatabase) {
const urlShort = `https://short-links.divlo.fr/?q=${urlInDatabase.shortcut}`
const urlShort = `${shortLinkBaseURL}/${urlInDatabase.shortcut}`
return errorHandling(next, {
message: `L'url a déjà été raccourcie... <br/> <br/> <a target="_blank" rel="noopener noreferrer" href="${urlShort}">${urlShort}</a>`,
statusCode: 400
@ -49,7 +51,7 @@ module.exports = async ({ res, next }, argsObject) => {
where: { shortcut: shortcutName }
})
if (shortcutInDatabase) {
const urlShort = `https://short-links.divlo.fr/?q=${shortcutInDatabase.shortcut}`
const urlShort = `${shortLinkBaseURL}/${shortcutInDatabase.shortcut}`
return errorHandling(next, {
message: `Le nom du raccourci a déjà été utilisé... <br/> <br/> <a target="_blank" rel="noopener noreferrer" href="${urlShort}">${urlShort}</a>`,
statusCode: 400
@ -58,10 +60,8 @@ module.exports = async ({ res, next }, argsObject) => {
// Ajout du lien raccourci
const result = await shortLinks.create({ url, shortcut: shortcutName })
const shortcutLinkResult = `https://short-links.divlo.fr/?q=${result.shortcut}`
return res
.status(200)
.json({
const shortcutLinkResult = `${shortLinkBaseURL}/${result.shortcut}`
return res.status(200).json({
resultHTML: `URL Raccourcie : <br/> <br/> <a target="_blank" rel="noopener noreferrer" href="${shortcutLinkResult}">${shortcutLinkResult}</a>`,
result: shortcutLinkResult
})

4
s.divlo.fr/.env.example Normal file
View File

@ -0,0 +1,4 @@
DB_HOST = ""
DB_NAME = ""
DB_USER = ""
DB_PASS = ""

2
s.divlo.fr/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
.env

3
s.divlo.fr/README.md Normal file
View File

@ -0,0 +1,3 @@
# s.divlo.fr
Site web qui permet de rediriger les utilisateurs vers leurs liens raccourcis sur [function.divlo.fr](https://function.divlo.fr/).

72
s.divlo.fr/app.js Normal file
View File

@ -0,0 +1,72 @@
/* Modules */
require('dotenv').config()
const path = require('path')
const express = require('express')
const helmet = require('helmet')
const morgan = require('morgan')
const { redirectToHTTPS } = require('express-http-to-https')
const mysql = require('mysql')
/* Files Imports & Variables */
const app = express()
const database = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
port: process.env.DB_PORT
})
/* Middlewares */
app.use(helmet())
app.use(morgan('dev'))
app.use(express.json())
app.use(redirectToHTTPS([/localhost:(\d{4})/]))
/* EJS Template Engines */
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
/* Routes */
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', (_req, res) => {
return res.render('index')
})
app.get('/:shortcut', (req, res, next) => {
const { shortcut } = req.params
if (shortcut == null) {
return res.redirect('/errors/404')
}
database.query(
'SELECT * FROM short_links WHERE shortcut = ?',
[shortcut],
(error, [result]) => {
if (error != null) {
return next(error)
}
if (result == null) {
return res.redirect('/error/404')
}
return res.redirect(result.url)
}
)
})
/* Errors */
app.use((_req, res) => {
return res.status(404).render('errors')
})
app.use((error, _req, res) => {
console.log(error)
return res.status(500).render('errors')
})
/* Server */
const PORT = process.env.PORT || 8000
app.listen(PORT, () => {
console.log('\x1b[36m%s\x1b[0m', `Started on port ${PORT}.`)
})

3497
s.divlo.fr/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

24
s.divlo.fr/package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "short.divlo.fr",
"version": "1.0.0",
"description": "Link shortener for FunctionProject",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"format": "standard \"./**/*.js\" --fix | snazzy || exit 0"
},
"dependencies": {
"dotenv": "^8.2.0",
"ejs": "^3.1.3",
"express": "^4.17.1",
"express-http-to-https": "^1.1.4",
"helmet": "^4.0.0",
"morgan": "^1.10.0",
"mysql": "^2.18.1"
},
"devDependencies": {
"nodemon": "^2.0.4",
"snazzy": "^8.0.0",
"standard": "^14.3.4"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Short-links</title>
<link rel="icon" type="image/png" href="/images/error404.png" />
</head>
<body>
<style type="text/css">
* {
font-family: Arial;
color: #fff;
background-color: black;
}
</style>
<p>Adresse url non connue</p>
</body>
</html>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Short-links</title>
<link rel="icon" type="image/png" href="/images/linkShortener.png" />
</head>
<body>
<style type="text/css">
* {
font-family: Arial;
color: #fff;
background-color: black;
}
</style>
</body>
</html>