Permalink
Browse files

Add option for basepath

  • Loading branch information...
1 parent efd58a5 commit 517598ef18c233dc3ae8b588a1a32e043a54be34 @joefiorini joefiorini committed Jun 15, 2012
Showing with 193 additions and 107 deletions.
  1. +20 −1 lib/concrete.js
  2. +5 −5 lib/public/concrete.js
  3. +92 −44 lib/server.js
  4. +7 −6 lib/views/index.coffee
  5. +1 −1 lib/views/jobPartial.coffee
  6. +2 −1 package.json
  7. +8 −2 src/concrete.coffee
  8. +58 −47 src/server.coffee
View
21 lib/concrete.js
@@ -1,14 +1,25 @@
+// Generated by CoffeeScript 1.3.1
(function() {
var argv, colors, git, optimist, startServer, usage, version;
+
process.title = 'Concrete';
+
version = '0.0.3';
+
colors = require('colors');
+
usage = 'Usage: concrete [-hpv] path_to_git_repo';
+
optimist = require('optimist');
+
argv = optimist.usage('Usage: concrete [-hpv] path_to_git_repo'.green).options('h', {
alias: 'host',
describe: "The hostname or ip of the host to bind to",
"default": '0.0.0.0'
+ }).options('d', {
+ alias: 'basedir',
+ describe: "Application base path for mounted instances",
+ "default": "/"
}).options('p', {
alias: 'port',
describe: "The port to listen on",
@@ -19,25 +30,33 @@
alias: 'version',
describe: "Show version"
}).argv;
+
if (argv.help) {
optimist.showHelp();
process.exit(1);
}
+
if (argv.v) {
console.log(("Concrete v" + version).green);
process.exit(1);
}
+
if (argv._.length === 0) {
optimist.showHelp();
console.log('You must specify a Git repo'.red);
process.exit(1);
}
+
startServer = function() {
var server;
+ global.currentNamespace = argv.d;
server = require('../lib/server');
server.listen(argv.p, argv.h);
- return console.log("Concrete listening on port %d with host %s".green, argv.p, argv.h);
+ return console.log("Concrete listening on port %d with host %s in directory %s".green, argv.p, argv.h, argv.d);
};
+
git = require('../lib/git');
+
git.init(argv._[0], startServer);
+
}).call(this);
View
10 lib/public/concrete.js
@@ -14,9 +14,8 @@
});
};
updateJob = function(job) {
- var id;
- id = $(job).find('.job_id').first().html();
- return $.get("/job/" + id, function(data) {
+ url = $(job).find("a").attr("href");
+ return $.get(url, function(data) {
$(job).find('.job_container').first().html(data.log);
if (data.finished) {
$(job).find('a img.loader').remove();
@@ -45,7 +44,7 @@
jobTemplate = function() {
return li('.job', function() {
a({
- href: "/job/" + (this.job._id.toString())
+ href: baseUrl + "job/" + (this.job._id.toString())
}, function() {
var d;
d = new Date(this.job.addedTime);
@@ -79,7 +78,8 @@
closeAll();
$('button.build').hide();
$('li.nojob').hide();
- $.post('/', function(data) {
+ var url = $(this).closest("form").attr("action");
+ $.post(url, function(data) {
var job;
if ($('ul.jobs').find('li.nojob').length > 0) {
$('ul.jobs').find('li.nojob').first().remove();
View
136 lib/server.js
@@ -1,12 +1,39 @@
+// Generated by CoffeeScript 1.3.1
(function() {
- var app, express, fs, jobs, path, runner, stylus;
+ var app, authorize, deferredApp, express, fs, git, jobs, path, runner, stylus;
+
express = require('express');
+
stylus = require('stylus');
+
fs = require('fs');
+
path = require('path');
+
runner = require('./runner');
+
jobs = require('./jobs');
- app = module.exports = express.createServer();
+
+ git = require('./git');
+
+ require('express-namespace');
+
+ authorize = function(user, pass) {
+ return user === git.user && pass === git.pass;
+ };
+
+ if (git.user && git.pass) {
+ app = module.exports = express.createServer(express.basicAuth(authorize));
+ } else {
+ app = module.exports = express.createServer();
+ }
+
+ app.helpers({
+ baseUrl: function() {
+ return path.normalize("" + global.currentNamespace + "/");
+ }
+ });
+
app.configure(function() {
var coffeeDir, publicDir;
app.set('views', __dirname + '/views');
@@ -25,74 +52,95 @@
}
}));
coffeeDir = __dirname + '/views';
- publicDir = __dirname + '/public';
+ publicDir = __dirname + this._locals.baseUrl() + '/public';
app.use(express.compiler({
src: coffeeDir,
dest: publicDir,
enable: ['coffeescript']
}));
app.use(express.logger());
app.use(app.router);
- return app.use(express.static(__dirname + '/public'));
+ return app.use(global.currentNamespace, express.static(__dirname + '/public'));
});
+
app.configure('development', function() {
return app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
+
app.configure('production', function() {
return app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
- app.get('/', function(req, res) {
- return jobs.getAll(function(jobs) {
- return res.render('index', {
- project: path.basename(process.cwd()),
- jobs: jobs
+
+ deferredApp = function() {
+ app.get('/', function(req, res) {
+ return jobs.getAll(function(jobs) {
+ return res.render('index', {
+ project: path.basename(process.cwd()),
+ jobs: jobs
+ });
});
});
- });
- app.get('/jobs', function(req, res) {
- return jobs.getAll(function(jobs) {
- return res.json(jobs);
+ app.get('/jobs', function(req, res) {
+ return jobs.getAll(function(jobs) {
+ return res.json(jobs);
+ });
});
- });
- app.get('/job/:id', function(req, res) {
- return jobs.get(req.params.id, function(job) {
- return res.json(job);
+ app.get('/job/:id', function(req, res) {
+ return jobs.get(req.params.id, function(job) {
+ return res.json(job);
+ });
});
- });
- app.get('/job/:id/:attribute', function(req, res) {
- return jobs.get(req.params.id, function(job) {
- if (job[req.params.attribute] != null) {
- return res.json(job[req.params.attribute]);
- } else {
- return res.send("The job doesn't have the " + req.params.attribute + " attribute");
- }
+ app.get('/job/:id/:attribute', function(req, res) {
+ return jobs.get(req.params.id, function(job) {
+ if (job[req.params.attribute] != null) {
+ return res.json(job[req.params.attribute]);
+ } else {
+ return res.send("The job doesn't have the " + req.params.attribute + " attribute");
+ }
+ });
});
- });
- app.get('/clear', function(req, res) {
- return jobs.clear(function() {
- return res.redirect('/jobs');
+ app.get('/clear', function(req, res) {
+ return jobs.clear(function() {
+ return res.redirect("" + (this._locals.baseUrl()) + "/jobs");
+ });
});
- });
- app.get('/add', function(req, res) {
- return jobs.addJob(function() {
- return res.redirect('/jobs');
+ app.get('/add', function(req, res) {
+ return jobs.addJob(function() {
+ return res.redirect("" + (this._locals.baseUrl()) + "/jobs");
+ });
});
- });
- app.post('/', function(req, res) {
- return jobs.addJob(function(job) {
- runner.build();
- if (req.xhr) {
- console.log(job);
- return res.json(job);
- } else {
- return res.redirect('/');
- }
+ app.get('/ping', function(req, res) {
+ return jobs.getLast(function(job) {
+ if (job.failed) {
+ return res.send(412);
+ } else {
+ return res.send(200);
+ }
+ });
});
- });
+ return app.post('/', function(req, res) {
+ return jobs.addJob(function(job) {
+ runner.build();
+ if (req.xhr) {
+ console.log(job);
+ return res.json(job);
+ } else {
+ return res.redirect("" + (this._locals.baseUrl()) + "/");
+ }
+ });
+ });
+ };
+
+ if (global.currentNamespace !== "/") {
+ app.namespace(global.currentNamespace, deferredApp);
+ } else {
+ deferredApp();
+ }
+
}).call(this);
View
13 lib/views/index.coffee
@@ -4,18 +4,19 @@ html ->
meta charset: 'utf-8'
title "#{if @title then @title+' - ' else ''}Concrete"
meta(name: 'description', content: @desc) if @desc?
- link rel: 'stylesheet', href: 'stylesheets/app.css'
- script src: 'js/jquery-1.6.2.min.js'
- script src: 'js/coffeekup.js'
- script src: 'concrete.js'
-
+ link rel: 'stylesheet', href: "#{@baseUrl()}stylesheets/app.css"
+ script src: "#{@baseUrl()}js/jquery-1.6.2.min.js"
+ script src: "#{@baseUrl()}js/coffeekup.js"
+ script src: "#{@baseUrl()}concrete.js"
+ script ->
+ "window.baseUrl = '#{@baseUrl()}';"
body ->
header ->
hgroup ->
h1 'CONCRETE'
h2 '.project', -> @project
nav ->
- form method: 'post', action: '/', ->
+ form method: 'post', action: @baseUrl(), ->
button '.build', -> 'Build'
div '#content', ->
View
2 lib/views/jobPartial.coffee
@@ -1,5 +1,5 @@
li '.job', ->
- a href: "/job/#{@job._id.toString()}", ->
+ a href: "#{@baseUrl()}job/#{@job._id.toString()}", ->
d = new Date(@job.addedTime)
div '.time', -> "#{d.toDateString()} #{d.toTimeString()}"
div '.job_id', -> "#{@job._id.toString()}"
View
3 package.json
@@ -28,7 +28,8 @@
"express": "2.x",
"stylus": "0.x",
"coffeekup": "0.3.x",
- "mongodb": "0.9.x"
+ "mongodb": "0.9.x",
+ "express-namespace": "*"
},
"engines": {
"node": ">=0.4.9"
View
10 src/concrete.coffee
@@ -13,6 +13,11 @@ optimist = require 'optimist'
describe: "The hostname or ip of the host to bind to",
default: '0.0.0.0'
})
+ .options('d', {
+ alias: 'basedir',
+ describe: "Application base path for mounted instances",
+ default: "/"
+ })
.options('p', {
alias: 'port',
describe: "The port to listen on",
@@ -45,10 +50,11 @@ if argv._.length == 0
# start server command
startServer = ->
# start the server
+ global.currentNamespace = argv.d
server = require '../lib/server'
server.listen argv.p, argv.h
- console.log "Concrete listening on port %d with host %s".green,
- argv.p, argv.h
+ console.log "Concrete listening on port %d with host %s in directory %s".green,
+ argv.p, argv.h, argv.d
# check the path and start the git request
git = require '../lib/git'
View
105 src/server.coffee
@@ -5,6 +5,7 @@ path = require 'path'
runner = require './runner'
jobs = require './jobs'
git = require './git'
+require 'express-namespace'
authorize = (user, pass) ->
user == git.user and pass == git.pass
@@ -14,6 +15,10 @@ if git.user and git.pass
else
app = module.exports = express.createServer()
+app.helpers
+ baseUrl: ->
+ path.normalize("#{global.currentNamespace}/")
+
app.configure ->
app.set 'views', __dirname + '/views'
app.set 'quiet', yes
@@ -33,61 +38,67 @@ app.configure ->
stylus(str).set 'compress', true
coffeeDir = __dirname + '/views'
- publicDir = __dirname + '/public'
+ publicDir = __dirname + @_locals.baseUrl() + '/public'
app.use express.compiler src: coffeeDir, dest: publicDir, enable: ['coffeescript']
app.use express.logger()
app.use app.router
- app.use express.static __dirname + '/public'
+ app.use global.currentNamespace, express.static __dirname + '/public'
app.configure 'development', ->
app.use express.errorHandler dumpExceptions: on, showStack: on
app.configure 'production', ->
app.use express.errorHandler dumpExceptions: on, showStack: on
-app.get '/', (req, res) ->
- jobs.getAll (jobs)->
- res.render 'index',
- project: path.basename process.cwd()
- jobs: jobs
-
-app.get '/jobs', (req, res) ->
- jobs.getAll (jobs)->
- res.json jobs
-
-app.get '/job/:id', (req, res) ->
- jobs.get req.params.id, (job) ->
- res.json job
-
-app.get '/job/:id/:attribute', (req, res) ->
- jobs.get req.params.id, (job) ->
- if job[req.params.attribute]?
- # if req.xhr...
- res.json job[req.params.attribute]
- else
- res.send "The job doesn't have the #{req.params.attribute} attribute"
-
-app.get '/clear', (req, res) ->
- jobs.clear ->
- res.redirect '/jobs'
-
-app.get '/add', (req, res) ->
- jobs.addJob ->
- res.redirect '/jobs'
-
-app.get '/ping', (req, res) ->
- jobs.getLast (job) ->
- if job.failed
- res.send(412)
- else
- res.send(200)
-
-app.post '/', (req, res) ->
- jobs.addJob (job)->
- runner.build()
- if req.xhr
- console.log job
- res.json job
- else
- res.redirect '/'
+deferredApp = ->
+ app.get '/', (req, res) ->
+ jobs.getAll (jobs)->
+ res.render 'index',
+ project: path.basename process.cwd()
+ jobs: jobs
+
+ app.get '/jobs', (req, res) ->
+ jobs.getAll (jobs)->
+ res.json jobs
+
+ app.get '/job/:id', (req, res) ->
+ jobs.get req.params.id, (job) ->
+ res.json job
+
+ app.get '/job/:id/:attribute', (req, res) ->
+ jobs.get req.params.id, (job) ->
+ if job[req.params.attribute]?
+ # if req.xhr...
+ res.json job[req.params.attribute]
+ else
+ res.send "The job doesn't have the #{req.params.attribute} attribute"
+
+ app.get '/clear', (req, res) ->
+ jobs.clear ->
+ res.redirect "#{@_locals.baseUrl()}/jobs"
+
+ app.get '/add', (req, res) ->
+ jobs.addJob ->
+ res.redirect "#{@_locals.baseUrl()}/jobs"
+
+ app.get '/ping', (req, res) ->
+ jobs.getLast (job) ->
+ if job.failed
+ res.send(412)
+ else
+ res.send(200)
+
+ app.post '/', (req, res) ->
+ jobs.addJob (job)->
+ runner.build()
+ if req.xhr
+ console.log job
+ res.json job
+ else
+ res.redirect "#{@_locals.baseUrl()}/"
+
+if global.currentNamespace != "/"
+ app.namespace global.currentNamespace, deferredApp
+else
+ deferredApp()

0 comments on commit 517598e

Please sign in to comment.