Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
added sharing of code
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfl committed Nov 27, 2010
1 parent 3f9e730 commit fdebc32
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 58 deletions.
83 changes: 73 additions & 10 deletions Bespin/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,27 @@
"pointer": "#newUserCommand",
"params": []
},
{
"ep": "command",
"name": "share",
"description": "Share the current file with other using a short url",
"pointer": "#shareCommand",
"params": []
},
{
"ep": "command",
"name": "docs",
"description": "Docs for JsApp",
"key": "ctrl_h",
"pointer": "#docsCommand",
"params":[]
},
{
"ep": "command",
"name": "sidebar",
"description": "Toggle the showing of the sidebar",
"pointer": "#sidebarCommand",
"params": []
}
]
});
Expand All @@ -119,18 +133,29 @@ var userPass=null;
var userToken=null;

if(location.hash) {
var hash = location.hash.substring(1).split(",");
userToken=hash[0];
userName=hash[1];
location.hash="";
switch(location.hash[1]) {
case 'u':
var hash = location.hash.substring(2).split(",");
userToken=hash[0];
userName=hash[1];
location.hash="";
break;
case 's':
location.href="/s/"+location.hash.substring(2);
break;
}
}
if(location.href.indexOf("/s/")!=-1) {
env.editor.value = env.editor.value.replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&amp;/g, "&");
env.commandLine.execute('goto 1');
}

function track (event) {
_gaq.push(['_trackEvent', 'editor', event]);
}

exports.openCommand = function (args, request) {
if(!userToken) return request.done("Not logedin");
if(!userToken) { alert("you need to login for this command"); return request.done("Not logedin"); }
if(!('file' in args)) {
env.commandLine.setInput('open ');
return;
Expand All @@ -151,7 +176,7 @@ exports.openCommand = function (args, request) {
};

exports.saveCommand = function (args, request) {
if(!userToken) return request.done("Not logedin");
if(!userToken) { alert("you need to login for this command"); return request.done("Not logedin"); }
if(!loadFile && (!('file' in args) || args['file']=="")) {
env.commandLine.setInput('save ');
return;
Expand Down Expand Up @@ -198,7 +223,7 @@ exports.newCommand = function (args, request) {
var domainLimit=2;
exports.deployCommand = function (args, request) {
//var saveFile=false;
if(!userToken) return request.done("Not logedin");
if(!userToken) { alert("you need to login for this command"); return request.done("Not logedin"); }
if(!loadFile)
return alert("The file has no name, you must use the save command first to give it a name");
if(loadValue != env.editor.value) {
Expand Down Expand Up @@ -257,7 +282,7 @@ exports.deployCommand = function (args, request) {
};

exports.listCommand = function (args, request) {
if(!userToken) return request.done("Not logedin");
if(!userToken) { alert("you need to login for this command"); return request.done("Not logedin"); }
if(fileList.length)
request.done(fileList.join("<br>"));
else
Expand Down Expand Up @@ -295,6 +320,10 @@ exports.loginCommand = function (args,request) {
if($("#moreUser:hidden").show().size()) {
return false;
}
if($("#userName").val() < 3) {
alert("Username is to short");
return false;
}
if(!$("#password").val()) {
alert("There is no password entered");
return false;
Expand Down Expand Up @@ -363,13 +392,47 @@ exports.loginCommand = function (args,request) {
exports.newUserCommand = function (args, request) {
exports.loginCommand(args, request);
$("#newUserButton").click();
}
};

exports.shareCommand = function (args, request) {
if(!userToken) { alert("you need to login for this command"); return request.done("Not logedin"); }
if(!loadFile)
return alert("The file has no name, you must use the save command first to give it a name");
if(loadValue != env.editor.value) {
if(confirm("Would you like to save the file before sharing")) {
loadValue=env.editor.value;
Ajax.Call({
"action": "save",
"name": loadFile,
"val": loadValue
});
}
}
Ajax.Call({
"action": "share",
"file": loadFile,
}, function (b) {
if(b.ok) {
request.done('Code shared at: <a href="http://jsapp.us/s/'+b.num+'">http://jsapp.us/s/'+b.num+'</a>');
location.hash="s"+b.num;
}else{
request.done("Failed to share code");
}
});
Ajax.send();
};


exports.docsCommand = function () {
window.open("http://wiki.matthewfl.com/jsapp:start");
//window.open("http://wiki.matthewfl.com/jsapp:start");
window.open("https://github.com/matthewfl/node-host/wiki");
track("help");
};

exports.sidebarCommand = function () {
window.sideBar(null);
};

var Ajax = {
docTitle: document.title,
buffer: [],
Expand Down
11 changes: 11 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This is the code base from http://jsapp.us


To get started developing
Install nginx ( http://nginx.org/ ) and load in the config file from devel/nginx.conf
run:
ln -s dn.nstore.js db.js
ln -s config.sample.js config.js
python devel/HostRedirProxy.py
node master.js
then configure you web browser to proxy through localhost:8000, this will redirect any request to jsapp.us to your local nginx install
4 changes: 3 additions & 1 deletion db.notes
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ prefixes:
owner_[subHostName]=user for who the owner of the sub app domain is without too much lookup
host_[hostName] for applications with own host name, points to app_
lsOwn_[user]=[subHostName] List of subdomains that are owned by the user
lsHost_[user]=[hostName] List of domains that are owned by user
lsHost_[user]=[hostName] List of domains that are owned by user
share_# The code for shared code
share_index The value of the last shared code, just use db.addInt to access
7 changes: 7 additions & 0 deletions db.tyrant.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ exports.remove = function (key, callback) {
exports.setCat = function (key, value, callback) {
tyrant.putcat(key, value);
if(callback) process.nextTick(callback);
};

exports.addInt = function (key, value, callback) {
callback = callback || function () {};
tyrant.addint(key, value, function (err, dat) {
callback(err ? null : dat);
});
};
136 changes: 136 additions & 0 deletions devel/HostRedirProxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/bin/sh -
"exec" "python" "-O" "$0" "$@"

__doc__ = """Tiny HTTP Proxy.
This module implements GET, HEAD, POST, PUT and DELETE methods
on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
method is also implemented experimentally, but has not been
tested yet.
Any help will be greatly appreciated. SUZUKI Hisao
"""

__version__ = "0.2.1"

import BaseHTTPServer, select, socket, SocketServer, urlparse

class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
__base = BaseHTTPServer.BaseHTTPRequestHandler
__base_handle = __base.handle

server_version = "TinyHTTPProxy/" + __version__
rbufsize = 0 # self.rfile Be unbuffered

def handle(self):
(ip, port) = self.client_address
if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients:
self.raw_requestline = self.rfile.readline()
if self.parse_request(): self.send_error(403)
else:
self.__base_handle()

def _connect_to(self, netloc, soc):
if netloc.find("jsapp.us") != -1 or netloc.find("google-analytics.com") != -1:
host_port = "localhost", 80
else:
i = netloc.find(':')
if i >= 0:
host_port = netloc[:i], int(netloc[i+1:])
else:
host_port = netloc, 80
print "\t" "connect to %s:%d" % host_port
try: soc.connect(host_port)
except socket.error, arg:
try: msg = arg[1]
except: msg = arg
self.send_error(404, msg)
return 0
return 1

def do_CONNECT(self):
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
if self._connect_to(self.path, soc):
self.log_request(200)
self.wfile.write(self.protocol_version +
" 200 Connection established\r\n")
self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
self.wfile.write("\r\n")
self._read_write(soc, 300)
finally:
print "\t" "bye"
soc.close()
self.connection.close()

def do_GET(self):
(scm, netloc, path, params, query, fragment) = urlparse.urlparse(
self.path, 'http')
if scm != 'http' or fragment or not netloc:
self.send_error(400, "bad url %s" % self.path)
return
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
if self._connect_to(netloc, soc):
self.log_request()
soc.send("%s %s %s\r\n" % (
self.command,
urlparse.urlunparse(('', '', path, params, query, '')),
self.request_version))
self.headers['Connection'] = 'close'
del self.headers['Proxy-Connection']
for key_val in self.headers.items():
soc.send("%s: %s\r\n" % key_val)
soc.send("\r\n")
self._read_write(soc)
finally:
print "\t" "bye"
soc.close()
self.connection.close()

def _read_write(self, soc, max_idling=20):
iw = [self.connection, soc]
ow = []
count = 0
while 1:
count += 1
(ins, _, exs) = select.select(iw, ow, iw, 3)
if exs: break
if ins:
for i in ins:
if i is soc:
out = self.connection
else:
out = soc
data = i.recv(8192)
if data:
out.send(data)
count = 0
else:
print "\t" "idle", count
if count == max_idling: break

do_HEAD = do_GET
do_POST = do_GET
do_PUT = do_GET
do_DELETE=do_GET

class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer): pass

if __name__ == '__main__':
from sys import argv
if argv[1:] and argv[1] in ('-h', '--help'):
print argv[0], "[port [allowed_client_name ...]]"
else:
if argv[2:]:
allowed = []
for name in argv[2:]:
client = socket.gethostbyname(name)
allowed.append(client)
print "Accept: %s (%s)" % (client, name)
ProxyHandler.allowed_clients = allowed
del argv[2:]
else:
print "Any clients will be served..."
BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)
7 changes: 1 addition & 6 deletions config/nginx.conf → devel/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ http {

#gzip on;

upstream production {
consistent_hash $host;
server 127.0.0.1:9000;
}


# production
server {
Expand All @@ -49,7 +44,7 @@ http {
#access_log logs/host.access.log main;

location / {
proxy_pass http://production;
proxy_pass http://127.0.0.1:9000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header host $host;
}
Expand Down
Loading

0 comments on commit fdebc32

Please sign in to comment.