-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote exampleserver in javascript to get around jashkenas/coffeescr…
- Loading branch information
Showing
1 changed file
with
65 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |