backend is coming

master
adb-sh 4 years ago
parent ba55edfcf0
commit 9e749d7aa3

@ -2,7 +2,15 @@ const http = require('http');
const url = require('url');
const fs = require('fs');
const ws = require('ws');
const redis = require('redis')
//redis client
const redis_client = redis.createClient()
redis_client.on("error", function (error) {
console.error(error)
})
//HTTP server
http.createServer(function (req, res) {
const q = url.parse(req.url, true);
let filename = "./public" + q.pathname;
@ -18,6 +26,7 @@ http.createServer(function (req, res) {
});
}).listen(8080);
//WS server
const wss = new ws.Server({
port: 8081,
perMessageDeflate: {
@ -41,10 +50,35 @@ const wss = new ws.Server({
}
});
//WS handler
wss.on('connection', ws => {
ws.on('message', message => {
console.log(`Received message => ${message}`)
if ({message} == 'long_url ') ws.send('error: url is empty')
let msg = `${message}`.split(";", 2)
if (msg[0] === 'long_url') {
if (msg[1] === '') ws.send('error;url is empty')
if (msg[1].length > 2000) ws.send('error;your url is too big')
else{
let ran_key = get_key(8)
//redis_client.set("key", "value", redis.print)
ws.send('short_url;http://127.0.0.1'+ran_key)
}
}
//ws.send('got your request: '+msg)
console.log(msg[0])
})
ws.send('websocket connected')
})
//random key
const forbidden_array = ['sym', 'admin', 'stats']
function get_key(length) {
let forbidden = false; let output = ''
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
do{
for (let i = 0; i < length; i++ )
output += characters.charAt(Math.floor(Math.random() * characters.length))
for (let i = 0; i < forbidden_array.length; i++) if (output === forbidden_array[i]) forbidden = true
} while (forbidden)
return output
}

@ -5,20 +5,23 @@
<link rel="stylesheet" type="text/css" href="./sym/dark.css">
<script src="./sym/script.js"></script>
<script>
const wsurl = 'ws://192.168.188.36:8081'
const wsurl = 'ws://127.0.0.1:8081'
const socket = new WebSocket(wsurl)
socket.onopen = () => {
socket.send('new session')
socket.send('new_session ')
}
socket.onerror = (error) => {
console.log(`WebSocket error: ${error}`)
}
socket.onmessage = (e) => {
console.log(e.data)
document.getElementById('error-message').innerText = e.data
let msg = e.data.split(";", 2)
if (msg[0] === 'error') {
document.getElementById('error-message').innerText = msg[1]
document.getElementById('error-box').style.display = 'block'
}
}
function short_url(){
socket.send('long_url '+document.getElementById('name-input').value)
}
@ -27,12 +30,12 @@
<body>
<div class="login">
<div class="title">short your url</div>
<form name="search" action="javascript:socket.send('long_url '+document.getElementById('name-input').value)" method="POST">
<form name="search" action="javascript:socket.send('long_url;'+document.getElementById('name-input').value)" method="POST">
<div class="inputbox" id="name">
<input class="input" id="name-input" type="text" onblur="deselected('name');" onfocus="selected('name');" name="url" value="" maxlength="2000" placeholder="https://your.long.url/junk">
</div>
<input type="hidden" value="search" name="login">
<a href="javascript:socket.send('long_url '+document.getElementById('name-input').value)">
<a href="javascript:socket.send('long_url;'+document.getElementById('name-input').value)">
<div class="btn-blue" id="short-btn">
<div class="btn-text">short</div>
</div>
@ -43,7 +46,7 @@
<div onclick="document.getElementById('error-box').style.display = 'none'" style="position: absolute; top:5px; right: 5px;" class="sym_btn-invisible">
<img class="icon" src="./sym/ic_close_white_24px.svg">
</div>
<div id="error-message">
<div id="error-message" class="btn-text">
</div>
</div>
</body>

Loading…
Cancel
Save