Permalink
Browse files

add new API

  • Loading branch information...
maccman committed Feb 10, 2012
1 parent 9c0132a commit 310069a4107d84814478396cf3c94a8f29d99f58
View
@@ -23,8 +23,14 @@ if ( !filename ) {
}
filename = path.resolve(filename);
+process.chdir(path.dirname(filename))
var app = new App;
global.app = app;
require(filename);
-strata.run(app);
+
+strata.run(app, {
+ host: app.config.host,
+ port: app.config.port,
+ socket: app.config.socket
+});
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -27,7 +27,7 @@
this.served = true;
if (Array.isArray(response)) {
return this.callback.apply(this, response);
- } else if (response.body != null) {
+ } else if ((response != null ? response.body : void 0) != null) {
return this.callback(response.status || 200, response.headers || {}, response.body || '');
} else {
return this.callback(200, {}, response || '');
@@ -54,6 +54,14 @@
return this.env.route;
});
+ Context.prototype.__defineGetter__('session', function() {
+ return this.env.session;
+ });
+
+ Context.prototype.__defineSetter__('session', function(value) {
+ return this.env.session = value;
+ });
+
Context.wrap = function(app) {
return function(env, callback) {
var context, result;
View
@@ -1,10 +1,12 @@
(function() {
- var context, fs, head, path, sendFile;
+ var context, fs, head, path, redirect, sendFile, strata;
fs = require('fs');
path = require('path');
+ strata = require('strata');
+
context = require('./context');
sendFile = function(file, options) {
@@ -34,9 +36,15 @@
return [status, {}, body];
};
+ redirect = function(url) {
+ strata.redirect(this.env, this.callback, url);
+ return this.served = true;
+ };
+
context.include({
sendFile: sendFile,
- head: head
+ head: head,
+ redirect: redirect
});
module.exports = {
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,68 @@
+(function() {
+ var exists, fs, mime, path, sendFile, strata, utils;
+
+ path = require('path');
+
+ fs = require('fs');
+
+ mime = require('mime');
+
+ strata = require('./index');
+
+ utils = strata.utils;
+
+ exists = function(path) {
+ var fiber;
+ fiber = Fiber.current;
+ path.exists(path, function(exists) {
+ return fiber.run(exists);
+ });
+ return yield();
+ };
+
+ sendFile = function(callback, path, stats) {
+ return callback(200, {
+ 'Content-Type': mime.lookup(path),
+ 'Content-Length': stats.size.toString(),
+ 'Last-Modified': stats.mtime.toUTCString()
+ }, fs.createReadStream(path));
+ };
+
+ module.exports = function(app, root, index) {
+ if (typeof root !== 'string') throw new strata.Error('Invalid root directory');
+ if (!path.existsSync(root)) {
+ throw new strata.Error("Directory " + root + " does not exist");
+ }
+ if (!fs.statSync(root).isDirectory()) {
+ throw new strata.Error("" + root + " is not a directory");
+ }
+ if (index && typeof index === 'string') index = [index];
+ return function(env, callback) {
+ var fullPath, indexPath, pathInfo, stats, _i, _len;
+ if (env.requestMethod !== 'GET') return app(env, callback);
+ pathInfo = unescape(env.pathInfo);
+ if (pathInfo.indexOf('..') !== -1) return utils.forbidden(env, callback);
+ fullPath = path.join(root, pathInfo);
+ exists = fs.existsSync(fullPath);
+ if (!exists) return app(env, callback);
+ stats = fs.statSync(fullPath);
+ if (stats.isFile()) {
+ return sendFile(callback, fullPath, stats);
+ } else if (stats.isDirectory() && index) {
+ for (_i = 0, _len = index.length; _i < _len; _i++) {
+ indexPath = index[_i];
+ indexPath = path.join(fullPath, indexPath);
+ exists = fs.existsSync(indexPath);
+ if (exists) {
+ sendFile(callback, indexPath, stats);
+ break;
+ }
+ }
+ return app(env, callback);
+ } else {
+ return app(env, callback);
+ }
+ };
+ };
+
+}).call(this);
View
@@ -0,0 +1,35 @@
+(function() {
+ var coffee, context, fs, path, view;
+
+ path = require('path');
+
+ fs = require('fs');
+
+ context = require('../context');
+
+ coffee = require('coffee-script');
+
+ view = function(template, context) {
+ var fiber;
+ fiber = Fiber.current;
+ fs.readFile(template, 'utf8', function(err, data) {
+ var headers, result;
+ if (err) fiber.throwInto(err);
+ headers = {
+ 'Content-Type': 'text/javascript'
+ };
+ result = coffee.compile(data);
+ return fiber.run([200, headers, result]);
+ });
+ return yield();
+ };
+
+ context.include({
+ coffee: view
+ });
+
+ module.exports = {
+ coffee: view
+ };
+
+}).call(this);
View
@@ -0,0 +1,35 @@
+(function() {
+ var context, eco, fs, path, view;
+
+ path = require('path');
+
+ fs = require('fs');
+
+ context = require('../context');
+
+ eco = require('eco');
+
+ view = function(template, context) {
+ var fiber;
+ fiber = Fiber.current;
+ fs.readFile(template, 'utf8', function(err, data) {
+ var headers, result;
+ if (err) fiber.throwInto(err);
+ headers = {
+ 'Content-Type': 'text/html'
+ };
+ result = eco.render(data, projects);
+ return fiber.run([200, headers, result]);
+ });
+ return yield();
+ };
+
+ context.include({
+ eco: view
+ });
+
+ module.exports = {
+ eco: view
+ };
+
+}).call(this);
View
@@ -0,0 +1,37 @@
+(function() {
+ var context, fs, less, path, view;
+
+ path = require('path');
+
+ fs = require('fs');
+
+ context = require('../context');
+
+ less = require('less');
+
+ view = function(template, context) {
+ var fiber;
+ fiber = Fiber.current;
+ fs.readFile(template, 'utf8', function(err, data) {
+ var headers;
+ if (err) fiber.throwInto(err);
+ headers = {
+ 'Content-Type': 'text/css'
+ };
+ return less.render(data, function(err, css) {
+ if (err) fiber.throwInto(err);
+ return fiber.run([200, headers, css]);
+ });
+ });
+ return yield();
+ };
+
+ context.include({
+ less: view
+ });
+
+ module.exports = {
+ less: view
+ };
+
+}).call(this);
Oops, something went wrong.

0 comments on commit 310069a

Please sign in to comment.