seedlinux

find open source torrents and stats for people wishing to contribute bandwidth.
git clone git://git.beardyjay.co.uk/seedlinux
Log | Files | Refs | README | LICENSE

commit 456fcf76a8e0bfd49a31ef29c28abd82c8dccfd9
parent 97a9a0208a7d9787e975bbe7dac83392569ff1f8
Author: Jay Scott <jay@jayscott.co.uk>
Date:   Wed, 28 Jun 2017 21:58:52 +0100

refactor code, updating dataset

Diffstat:
Mapp.js | 33++++++++++++---------------------
Aapp/config.js | 12++++++++++++
Aapp/db.js | 12++++++++++++
Aconfig/development.json | 7+++++++
Mroutes/index.js | 14++++++++------
Mroutes/parse.js | 28++++++++++++++--------------
6 files changed, 65 insertions(+), 41 deletions(-)

diff --git a/app.js b/app.js @@ -1,38 +1,29 @@ -var express = require('express'); -var path = require('path'); -var favicon = require('serve-favicon'); -var logger = require('morgan'); -var cookieParser = require('cookie-parser'); -var bodyParser = require('body-parser'); +'use strict'; -// mongodb -var mongo = require('mongodb'); -var monk = require('monk'); -var db = monk('localhost:27017/seedlinux'); +const express = require('express'); +const path = require('path'); +const favicon = require('serve-favicon'); +const logger = require('morgan'); +const cookieParser = require('cookie-parser'); +const bodyParser = require('body-parser'); -// routes -var parse = require('./routes/parse'); -var index = require('./routes/index'); +const dbconn = require('./app/db'); +const parse = require('./routes/parse'); +const index = require('./routes/index'); -var app = express(); +const app = express(); -app.locals.inspect = require('util').inspect; - -// view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'pug'); -// uncomment after placing your favicon in /public -//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -// db access app.use(function(req,res,next){ - req.db = db; + req.db = dbconn.db; next(); }); diff --git a/app/config.js b/app/config.js @@ -0,0 +1,11 @@ +'use strict'; + +if(process.env.NODE_ENV === 'production') { + module.exports = { + host : process.env.NODE_HOST || "", + dbURI : process.env.dbURI | "" + } + +} else { + module.exports = require('../config/development.json'); +} + \ No newline at end of file diff --git a/app/db.js b/app/db.js @@ -0,0 +1,11 @@ +'use strict'; + +const config = require('./config'); +const mongo = require('mongodb'); +const monk = require('monk'); + +const db = monk(config.dbURI); + +module.exports = { + db +}+ \ No newline at end of file diff --git a/config/development.json b/config/development.json @@ -0,0 +1,6 @@ +{ + "host": "http://localhost:3000", + "dbURI": "localhost:27017/seedlinux", + "collection" : "torrents", + "torrent_data" : "data/torrents" +}+ \ No newline at end of file diff --git a/routes/index.js b/routes/index.js @@ -1,12 +1,14 @@ -let express = require('express'); -let router = express.Router(); +'use strict'; + +const config = require('../app/config'); +const express = require('express'); +const router = express.Router(); /* GET home page. */ -router.get('/', function(req, res, next) { - let db = req.db; - let collection = db.get('torrents'); +router.get('/', (req, res, next) => { + const collection = req.db.get(config.collection); - collection.find({}, {}, function(e, docs) { + collection.find({}, {}, (e, docs) => { res.render('index', { 'torrentlist': docs, 'title': 'Index Page', diff --git a/routes/parse.js b/routes/parse.js @@ -1,30 +1,30 @@ +'use strict'; + +const config = require('../app/config'); + const express = require('express'); -var parseTorrent = require('parse-torrent'); -var appRoot = require('app-root-path'); -var fs = require('fs'); -var router = express.Router(); +const parseTorrent = require('parse-torrent'); +const appRoot = require('app-root-path'); +const fs = require('fs'); +const router = express.Router(); /** TODO: - Refactor. - - Move torrent to config option or cmd parsed option. - Move to commandline tool instead of web route. - get seeders / leechers on first pass. - - */ -router.get('/', function (req, res) { +router.get('/', (req, res, next) => { - var db = req.db; - var collection = db.get('torrents'); - var torrentFolder = './data/torrents'; + const collection = req.db.get(config.collection); + const torrentFolder = config.torrent_data; fs.readdir(torrentFolder, (err, files, next) => { files.forEach(file => { - tFile = fs.readFileSync(torrentFolder + '/' + file) - tData = parseTorrent(tFile) + let tFile = fs.readFileSync(torrentFolder + '/' + file); + let tData = parseTorrent(tFile); - var tMagnet = parseTorrent.toMagnetURI({ + let tMagnet = parseTorrent.toMagnetURI({ infoHash: tData.infoHash })