Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
msimmer committed Sep 28, 2019
0 parents commit d08ce39
Show file tree
Hide file tree
Showing 14 changed files with 2,241 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
env: {
browser: true,
es6: true
},
extends: 'eslint:recommended',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
require: true,
process: true
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
},
rules: {}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
env.sh
41 changes: 41 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const createError = require('http-errors')
const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('morgan')

const indexRouter = require('./routes/index')
const usersRouter = require('./routes/users')

const app = express()

// view engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')

app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

app.use('/', indexRouter)
app.use('/users', usersRouter)

// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404))
})

// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}

// render the error page
res.status(err.status || 500)
res.render('error')
})

module.exports = app
86 changes: 86 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

const app = require('../app')
const debug = require('debug')('dashboard:server')
const http = require('http')

/**
* Get port from environment and store in Express.
*/

const port = normalizePort(process.env.PORT || '3000')
app.set('port', port)

/**
* Create HTTP server.
*/

const server = http.createServer(app)

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port)
server.on('error', onError)
server.on('listening', onListening)

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
const port = parseInt(val, 10)

if (isNaN(port)) {
// named pipe
return val
}

if (port >= 0) {
// port number
return port
}

return false
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error
}

const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(`${bind} requires elevated privileges`)
process.exit(1)
break
case 'EADDRINUSE':
console.error(`${bind} is already in use`)
process.exit(1)
break
default:
throw error
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
const addr = server.address()
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`
debug(`Listening on ${bind}`)
}
13 changes: 13 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class API {
addClients() {}
removeClients() {}

startClients() {}
stopClients() {}

addTorrents() {}
createTorrent() {}
removeTorrents() {}
}

module.exports = new API()
103 changes: 103 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const mysql = require('mysql')
const connection = mysql.createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASS,
database: process.env.DATABASE_NAME
})

class Database {
constructor() {
this.connection = connection
this.connection.connect()
}

destroy() {
this.connection.end()
}

getClients(callback) {
return this.connection.query('SELECT * FROM clients', (error, results) => {
if (error) return callback(error)
return callback(null, results)
})
}

addClients(n, callback) {
// Get available ports
return this.connection.query(`SELECT * FROM ports WHERE available = 1 LIMIT ${n}`, (error1, results1) => {
if (error1) return callback(error1)
if (!results1.length) return callback(new Error('No available ports'))

// Save reference to ports
const rpcPorts = results1.reduce((acc, curr) => acc.concat(curr.rpc_port), []).join()

// Set up insert
const data = [
results1.reduce(
(acc, curr) => acc.push([`transmission-${curr.rpc_port}`, curr.rpc_port, curr.transmission_port]) && acc,
[]
)
]

// Insert rows
return this.connection.query('INSERT INTO clients (name, rpc_port, transmission_port) VALUES ?', data, error2 => {
if (error2) return callback(error2)

// Set available status on ports table
return this.connection.query(
`UPDATE ports SET available = 0 WHERE rpc_port IN (${rpcPorts})`,
(error3, results3) => {
callback(null, results3)
}
)
})
})
}

removeClients() {}

startClients(ports, callback) {
const ports_ = ports.join()
return this.connection.query(`UPDATE clients SET active = 1 WHERE rpc_port IN (${ports_})`, (error, results) => {
if (error) return callback(error)
callback(null, results)
})
}

stopClients(ports, callback) {
const ports_ = ports.join()
return this.connection.query(`UPDATE clients SET active = 0 WHERE rpc_port IN (${ports_})`, (error, results) => {
if (error) return callback(error)
callback(null, results)
})
}
}

// module.exports = new Database()

const db = new Database()
const callback = (err, results) => {
if (err) console.log('ERR!', err)
console.log(results)
connection.end()
}

// db.getClients(callback)
// db.addClients(2, callback)
// db.startClients([9091, 9092], callback)
// db.stopClients([9091, 9092], callback)

/**
CREATE TABLE `clients` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL,
`rpc_port` bigint(20) unsigned NOT NULL,
`transmission_port` bigint(20) unsigned NOT NULL,
`active` tinyint(1) DEFAULT '0',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `rpc_port` (`rpc_port`),
UNIQUE KEY `transmission_port` (`transmission_port`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
*/
Loading

0 comments on commit d08ce39

Please sign in to comment.