Skip to content

Extract database config to a file and share db with child. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ var express = require('express')
, http = require('http')
, path = require('path')
, partials = require('express-partials')
, app = express();
, config = require('./config')
, app = express();

global.io = require('socket.io').listen(app.listen( 3000 ));
global.io = require('socket.io').listen(app.listen( config.port ));

io.configure(function () {
io.set('transports', ['websocket', 'xhr-polling']);
io.set('log level', 0);
io.set('log level', config.log_level);
io.set('force new connection', true);
});

Expand All @@ -21,10 +22,16 @@ io.sockets.on('connection', function (socket)
socket.on('setMaxThreads', function(data){ });
});

// db connect
var db = require('mongoose');
db.connect(config.db.service+'://'+config.db.host+'/'+config.db.database);

app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', { layout:true, pretty: true });
app.set('config', config);
app.set('db', db);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
Expand All @@ -39,3 +46,4 @@ app.configure('development', function(){
});

require('./routes')(app);

9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"log_level": 0,
"port": 3000,
"db": {
"service": "mongodb",
"host": "localhost",
"database": "search_for_404_v4"
}
}
37 changes: 25 additions & 12 deletions crawling-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@
Scraper app. Only runs as child forked process.
*/

var util = require('util'),
http = require('http'),
url = require('url'),
mongoose= require('mongoose'),
argv = require('named-argv'),
jsdom = require('jsdom');

mongoose.connect('mongodb://localhost/search_for_404_v4');
var util = require('util'),
http = require('http'),
url = require('url'),
mongoose = require('mongoose'),
argv = require('named-argv'),
jsdom = require('jsdom');

/*
requestsRunningPool: array of links are requesting now
*/

var scrapeHost = "", max_depth, create_sitemap, link, auth = {},
LinksCheckModel, LinksGrabbedModel,
requestsRunning = 0, requestsRunningPool = [], requestsPerSecond = 0, maxThreads = 5,
checkUrlInterval = null, processingDOM = false;
var scrapeHost = "",
max_depth,
create_sitemap,
link,
auth = {},
LinksCheckModel,
LinksGrabbedModel,
requestsRunning = 0,
requestsRunningPool = [],
requestsPerSecond = 0,
maxThreads = 5,
checkUrlInterval = null,
processingDOM = false,
config;

process.on("message", function(data)
{
switch (data.action)
{
case "setConfig":
config = data.config;
mongoose.connect(config.db.service+'://'+config.db.host+'/'+config.db.database);
break;

case "setAuth":
auth.user = data.auth_user;
auth.pass = data.auth_pass;
Expand Down
17 changes: 14 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ module.exports = function(app) {
}

function getHomePage(req, res) {
res.render('index');
var port = res.app.settings.config.port;

res.render('index', {
port: port
});
}

function postAddScraper(req, res)
Expand All @@ -23,9 +27,16 @@ function postAddScraper(req, res)
auth_pass = req.body.auth_pass,
depth = parseInt(req.body.create_crawler_depth),
create_sitemap = req.body.create_crawler_sitemap == 1,
clean = req.body.clean_crawl == 1;
clean = req.body.clean_crawl == 1,
config = res.app.settings.config;

var child = child_process.fork("crawling-daemon.js");

// setup config
child.send({
action: "setConfig",
config: config
});

if (auth_user!="" && auth_pass!="")
child.send(
Expand Down Expand Up @@ -114,4 +125,4 @@ function postAddScraper(req, res)

// child_processes[url] = child;
res.redirect("/");
}
}
4 changes: 2 additions & 2 deletions views/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap, from Twitter</title>
<title>Node Web Crawler</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
Expand All @@ -17,7 +17,7 @@
<script src="/socket.io/socket.io.js"></script>

<script>
var socket = io.connect(window.___location.hostname, {port: 3000});
var socket = io.connect(window.___location.hostname, {port: <%= port %>});
</script>
</head>

Expand Down