Added some api functions, an express server for the API and nunjucks

Added nunjucks templates for frontend pages.
This commit is contained in:
knotteye
2019-09-28 21:43:25 -05:00
parent 085dd02148
commit b0533f973b
16 changed files with 1954 additions and 5 deletions

39
src/api.ts Normal file
View File

@@ -0,0 +1,39 @@
import * as db from "./database"
var config: any;
function init(conf: object){
config = conf;
}
async function register(name: string, password: string, streamer: boolean) {
if(!config.registration){
return {"error":"registration disabled"};
}
else {
if(name.includes(';') || name.includes(' ')) return {"error":"illegal characters"};
let s: boolean;
if(streamer && config.streamKeys) s = true;
else s = false;
let r: boolean = await db.addUser(name, password, s, false);
if(r) return {"success":""};
else return {"error":""};
}
}
async function login(name: string, pass: string) {
return await db.validatePassword(name, pass);
}
async function users(num: number) {
return await db.query('select username from users limit '+num);
}
async function user(name: string) {
}
async function instance() {
}
export { init, register };

View File

@@ -1,6 +1,8 @@
import * as mediaserver from "./server";
import * as ircd from "./ircd";
import * as db from "./database";
import * as api from "./api";
import * as http from "./http";
import * as config from "config";
function run(): void{
@@ -12,7 +14,10 @@ function run(): void{
streamKeys: config.media.streamKeys,
registration: config.satyr.registration,
webFormat: config.satyr.webFormat,
restrictedNames: config.satyr.restrictedNames
restrictedNames: config.satyr.restrictedNames,
name: config.satyr.name,
domain: config.satyr.domain,
email: config.satyr.email
};
const nms: object = {
logType: config.server.logs,
@@ -47,6 +52,8 @@ function run(): void{
}
};
api.init(satyr);
http.init(satyr);
db.init(dbcfg, bcryptcfg);
mediaserver.init(nms, satyr);
ircd.init();

View File

@@ -12,6 +12,7 @@ function init (db: object, bcrypt: object){
async function addUser(name: string, password: string, streamer: boolean, admin: boolean){
//does not respect registration setting in config
//nor stream keys
if(password === '') return false;
let key: string = ' ';
if (streamer) key = await genKey();
@@ -60,7 +61,8 @@ async function query(query: string){
}
async function validatePassword(username: string, password: string){
;
let pass: any= await query('select password from users where username=\''+username+'\' limit 1');
return await bcrypt.compare(password, pass[0].password_hash);
}
export { query, raw, init, addUser, rmUser, addStreamKey, rmStreamKey };
export { query, raw, init, addUser, rmUser, addStreamKey, rmStreamKey, validatePassword };

47
src/http.ts Normal file
View File

@@ -0,0 +1,47 @@
import * as express from "express";
import * as njk from "nunjucks";
import * as bodyparser from "body-parser";
import * as api from "./api";
import * as db from "./database";
var app = express();
var njkconf;
function init(satyr: any){
app.listen(8000);
njk.configure('templates', {
autoescape: true,
express : app,
watch: true
});
njkconf ={
sitename: satyr.name,
domain: satyr.domain,
email: satyr.email,
user: '',
streamtitle: '',
};
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index.njk', njkconf);
});
app.get('/about', (req, res) => {
res.render('about.njk', njkconf);
});
app.get('/users/*', (req, res) => {
njkconf.user = req.url.split('/')[2].toLowerCase();
res.render('user.njk', njkconf);
});
app.get('/registration', (req, res) => {
res.render('registration.njk', njkconf);
});
app.post('/api/register', (req, res) => {
api.register(req.body.username, req.body.password, req.body.streamer).then( (result) => {
res.send({"error":""});
});
});
app.use(express.static('site'));
}
export { init };

View File

@@ -32,8 +32,8 @@ function init (mediaconfig: any, satyrconfig: any) {
}
console.log("[NodeMediaServer] Public endpoint, checking record flag.");
//if this stream is from the public endpoint, check if we should be recording
return db.query('select username from users where username=\''+key+'\' and record_flag=true limit 1').then((results) => {
if(results[0].username && satyrconfig.record){
return db.query('select username,record_flag from users where username=\''+key+'\' limit 1').then((results) => {
if(results[0].record_flag && satyrconfig.record){
console.log('[NodeMediaServer] Initiating recording for stream:',id);
mkdir(mediaconfig.http.mediaroot+'/'+mediaconfig.trans.tasks[0].app+'/'+results[0].username, { recursive : true }, (err) => {
if (err) throw err;