Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 39465ab1eff996efb108eeed8b1818efe5e77dec 0 parents
@chrisdickinson authored
9 LICENSE.bsd
@@ -0,0 +1,9 @@
+opyright (c) 2010, Chris Dickinson
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+Neither the name of the Wilson Framework nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 bin/runserver
@@ -0,0 +1,13 @@
+#!/usr/bin/env node
+var node = process.argv[0] === 'node',
+ settings = node ? process.argv[2] : process.argv[1],
+ port = node ? process.argv[3] : process.argv[2],
+ sys = require('sys');
+
+var runserver = require('wilson').runserver;
+
+require.paths.push(process.cwd());
+sys.debug(require.paths);
+
+runserver(settings, port);
+
20 lib/conf.js
@@ -0,0 +1,20 @@
+var Settings = function () {
+
+};
+Settings.prototype.extend = function(literal) {
+ for(var i in literal) if(literal.hasOwnProperty(i)) {
+ var target = require(i).settings;
+ this[i] = this[i] === undefined ? {'addons':{}, 'values':{} } : this[i];
+ for(var value_name in literal[i].values) if(literal[i].values.hasOwnProperty(value_name)) {
+ target.set_value(value_name, literal[i].values[value_name]);
+ this[i].values[value_name] = literal[i].values[value_name];
+ }
+
+ for(var addon_name in literal[i].addons) if(literal[i].addons.hasOwnProperty(addon_name)) {
+ target.set_addon(addon_name, literal[i].addons[addon_name]);
+ this[i].addons[addon_name] = literal[i].addons[addon_name];
+ }
+ }
+};
+
+exports.settings = new Settings();
33 lib/http.js
@@ -0,0 +1,33 @@
+var sys = require('sys');
+
+var HttpResponse = function(status_code, headers, content) {
+ this.status_code = status_code;
+ this.headers = headers;
+ this.content = content;
+};
+HttpResponse.prototype.respond = function(response) {
+ response.writeHead(this.status_code, this.headers);
+ response.end(this.content);
+};
+
+Http404 = function() { };
+Http404.prototype = new HttpResponse(404, {'Content-Type':'text/html'});
+Http404.prototype.constructor = Http404;
+
+HttpServerError = function(content) {
+ sys.log("500 - "+content);
+ this.content = "Internal Server Error";
+};
+HttpServerError.prototype = new HttpResponse(500, {'Content-Type':'text/html'});
+HttpServerError.prototype.constructor = HttpServerError;
+
+
+HttpResponseRedirect= function(to) { HttpResponse.apply(this, [301, {'Location':to}, to]); };
+HttpResponseRedirect.prototype = new HttpResponse(301, {});
+HttpResponseRedirect.prototype.constructor = HttpResponseRedirect;
+
+exports.Http404 = Http404;
+exports.HttpServerError = HttpServerError;
+exports.HttpResponseRedirect = HttpResponseRedirect;
+exports.HttpResponse = HttpResponse;
+
6 lib/main.js
@@ -0,0 +1,6 @@
+exports.http = require('./http');
+exports.runserver = require('./runserver').runserver;
+exports.shortcuts = require('./shortcuts');
+exports.conf = require('./conf');
+exports.urls = require('./urls');
+exports.settings = require('./settings');
42 lib/runserver.js
@@ -0,0 +1,42 @@
+var parse = require('url').parse,
+ createServer = require('http').createServer,
+ http = require('./http'),
+ sys = require('sys'),
+ urls = require('./urls'),
+ escaperoute = require('escaperoute');
+
+var runserver = function(settings_file, listen) {
+ try {
+ } catch(err) {
+ sys.debug(err);
+ sys.puts("Could not find the settings file '"+settings_file+"'!");
+ }
+ require(settings_file);
+ var rootUrlConf = urls.getRoot();
+ var server = createServer(function(request, response) {
+ var path = parse(request.url, true).pathname;
+ try {
+ var match = rootUrlConf.match(path);
+ match(request, response);
+ sys.log('GET '+path+' - 200');
+ } catch(err) {
+ if(err instanceof http.HttpResponse) {
+ sys.log('GET '+path+' - '+err.status_code);
+ err.respond(response);
+ } else if(err instanceof escaperoute.NoMatch) {
+ sys.log('GET '+path+' - 404');
+ (new Http404()).respond(response);
+ } else {
+ sys.log('GET '+path+' - 500');
+ response.writeHead(500, {'Content-Type':'text/html'});
+ response.end("<h1>An error occurred.</h1>");
+ }
+ }
+ });
+
+ sys.puts('Running at '+listen);
+ sys.puts('Ctrl-C to stop doing that thing.');
+ server.listen(parseInt(listen, 10));
+};
+
+exports.runserver = runserver;
18 lib/settings.js
@@ -0,0 +1,18 @@
+ var registry = {},
+ values = {};
+
+ var set_addon = function(name, target) {
+ registry[name] = target;
+ };
+
+ var set_value = function(name, target) {
+ values[name] = target;
+ };
+
+ var get_value = function(name) {
+ return values[name];
+ };
+
+exports.get_value = get_value;
+exports.set_value = set_value;
+exports.set_addon = set_addon;
36 lib/shortcuts.js
@@ -0,0 +1,36 @@
+var http = require('./http'),
+ jsdtl = require('jsdtl');
+
+var getObjectOr404 = function(Model, filter, callback) {
+ Model.objects.filter(filter).all(function(objects) {
+ if(objects.length !== 1) {
+ throw (new http.Http404());
+ }
+ callback(objects[0]);
+ });
+};
+
+var renderToResponse = function(request, response) {
+ return function(template, context, content_type) {
+ content_type = content_type ? content_type : 'text/html';
+
+ jsdtl.settings.get_addon('loaders').get_template(template, function(tpl, err) {
+ if(err) {
+ var responder = new http.HttpServerError("Could not find template "+template);
+ responder.respond(response);
+ } else {
+ tpl.render(new jsdtl.core.Context(context), function(data) {
+ response.writeHead(200, {
+ 'Content-Type':content_type,
+ 'Content-Length':data.length
+ });
+ response.write(data);
+ response.end();
+ });
+ }
+ });
+ };
+};
+
+exports.renderToResponse = renderToResponse;
+exports.getObjectOr404 = getObjectOr404;
29 lib/urls.js
@@ -0,0 +1,29 @@
+var escaperoute = require('escaperoute'),
+ conf = require('./conf'),
+ settings = conf.settings;
+
+var root_urlconf = null;
+
+var getRoot = function() {
+ if(root_urlconf) return root_urlconf;
+
+ var loc = settings.wilson.values.root_urlconf.split('.'),
+ app_name = loc[0],
+ rest = loc.slice(1),
+ retval = require(app_name);
+
+ for(var i = 0, len = rest.length; i < len; ++i) {
+ retval = retval[rest[i]];
+ }
+ root_urlconf = retval;
+ return getRoot();
+};
+
+var reverse = function(name, args) {
+ var rootUrls = getRoot();
+ return rootUrls.reverse(name, args);
+};
+
+exports.getRoot = getRoot;
+exports.reverse = reverse;
+
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "wilson",
+ "version":"0.0.1",
+ "description": "a framework for people behind fences",
+ "keywords": [
+ "framework",
+ "orm"
+ ],
+ "author":"Chris Dickinson",
+ "directories": {"lib": "./lib" },
+ "main": "./lib/main",
+ "bin":{
+ "wilson-runserver":"./bin/runserver"
+ },
+ "license": [
+ "New BSD License"
+ ]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.