Skip to content

Commit

Permalink
Rewrote exampleserver in javascript to get around jashkenas/coffeescr…
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed Sep 19, 2011
1 parent 932d561 commit 9c69c72
Showing 1 changed file with 65 additions and 49 deletions.
114 changes: 65 additions & 49 deletions bin/exampleserver
Original file line number Diff line number Diff line change
@@ -1,67 +1,83 @@
#!/usr/bin/env coffee
#!/usr/bin/env node

connect = require 'connect'
sharejs = require '../src/server'
hat = require('hat').rack(32, 36)
// This is a simple example sharejs server which hosts the sharejs
// examples in examples/.
//
// It demonstrates a few techniques to get different application behaviour.

argv = require('optimist').
require('coffee-script');
var connect = require('connect'),
sharejs = require('../src/server'),
hat = require('hat').rack(32, 36);

var argv = require('optimist').
usage("Usage: $0 [-p portnum]").
default('p', 8000).
alias('p', 'port').
argv
argv;

server = connect(
var server = connect(
connect.favicon(),
connect.static(__dirname + '/../examples'),
connect.router (app) ->

renderer = require '../examples/_static'
app.get '/static/:docName', (req, res, next) ->
docName = req.params.docName
renderer docName, server.model, res, next

wiki = require '../examples/_wiki'
app.get '/wiki/?', (req, res, next) ->
res.writeHead 301, {location: '/wiki/Main'}
res.end()
connect.router(function (app) {
var renderer = require('../examples/_static');
app.get('/static/:docName', function(req, res, next) {
var docName;
docName = req.params.docName;
renderer(docName, server.model, res, next);
});

app.get '/wiki/:docName', (req, res, next) ->
docName = req.params.docName
wiki docName, server.model, res, next
var wiki = require('../examples/_wiki');
app.get('/wiki/?', function(req, res, next) {
res.writeHead(301, {location: '/wiki/Main'});
res.end();
});

app.get '/pad/?', (req, res, next) ->
docName = hat()
res.writeHead 303, {location: '/pad/pad.html#' + docName}
res.write ''
res.end()
app.get('/wiki/:docName', function(req, res, next) {
var docName;
docName = req.params.docName;
wiki(docName, server.model, res, next);
});

app.get '/?', (req, res, next) ->
res.writeHead 302, {location: '/index.html'}
res.end()
app.get('/pad/?', function(req, res, next) {
var docName;
docName = hat();
res.writeHead(303, {location: '/pad/pad.html#' + docName});
res.write('');
res.end();
});

)
app.get('/?', function(req, res, next) {
res.writeHead(302, {location: '/index.html'});
res.end();
});
})
);

options =
db:
type:'memory'
auth: (client, action) ->
# This auth handler rejects any ops bound for docs starting with 'readonly'.
if action.name == 'submit op' and action.docName.match /^readonly/
action.reject()
else
action.accept()
var options = {
db: {type: 'memory'},
auth: function(client, action) {
// This auth handler rejects any ops bound for docs starting with 'readonly'.
if (action.name === 'submit op' && action.docName.match(/^readonly/)) {
action.reject();
} else {
action.accept();
}
}
};

# Lets try and enable redis persistance if redis is installed...
try
require 'redis'
options.db = type:'redis'
// Lets try and enable redis persistance if redis is installed...
try {
require('redis');
options.db = {type: 'redis'};
} catch (e) {}

console.log "Options: ", options
console.log("Options: ", options);

port = argv.p
var port = argv.p;

# Attach the sharejs REST and Socket.io interfaces to the server
sharejs.attach server, options
// Attach the sharejs REST and Socket.io interfaces to the server
sharejs.attach(server, options);

server.listen port
console.log "Demos running at http://localhost:#{port}/"
server.listen(port);
console.log("Demos running at http://localhost:" + port);

0 comments on commit 9c69c72

Please sign in to comment.