Permalink
Browse files

work already, docs to be done

  • Loading branch information...
1 parent c1fa13f commit af8d791fe6ce33d84f3b45f077ce887546680f3b @nicocube committed May 30, 2012
Showing with 190 additions and 0 deletions.
  1. +112 −0 bin/llama-script.js
  2. +14 −0 index.js
  3. +41 −0 lib/llama.js
  4. +23 −0 package.json
View
@@ -0,0 +1,112 @@
+#!/usr/bin/env node
+/*
+ * Copyright 2012 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+ */
+
+require('knit').config(function (bind) {
+ bind('llama').to(require(__dirname + '/../lib/llama.js'))
+}).inject(function (fs, llama) {
+ var
+ default_options_location = __dirname + '/../config.json',
+ local_value_location = './config.json'
+
+ var options = {
+ folder: '.',
+ static : ["/favicon.ico","/img", "/css"],
+ PORT: 8000
+ }
+
+ var usage = "Usage: llama [options]\n" +
+ "Options:\n\n" +
+ " -c, --config FILE the path to a JSON file with options (convention is to search for config.json in folder)\n" +
+ " -f, --folder Folder the path to the folder to run llama in\n" +
+ " -p, --port PORT the PORT number to run this llama with\n" +
+ " -h, --help display this help and exit\n" +
+ " -v, --version output version information and exit";
+
+ loadConfig(default_options_location,true,true)
+
+
+ var useLocalConfig = true,
+ hasConfig=false,
+ hasFolder = false,
+ hasPort = false
+
+ process.argv.slice(2).forEach(function (arg) {
+ switch (arg) {
+ case '-h':
+ case '--help':
+ console.log(usage)
+ process.exit(0)
+ return;
+ case '-v':
+ case '--version':
+ var content = fs.readFileSync(__dirname + '/../package.json', 'utf8')
+ var pkg = JSON.parse(content)
+ console.log(pkg.version)
+ process.exit(0)
+ return;
+ case '-c':
+ case '--config':
+ hasConfig=true
+ return;
+ case '-p':
+ case '--port':
+ hasPort=true
+ return;
+ case '-f':
+ case '--folder':
+ hasFolder=true
+ return;
+ default:
+ if (hasConfig) {
+ loadConfig(arg)
+ hasConfig=false
+ useLocalConfig=false
+ }
+ if (hasFolder) {
+ options.folder = arg
+ hasFolder=false
+ }
+ if (hasPort) {
+ options.PORT = arg
+ hasPort = false
+ }
+ return;
+ }
+
+ })
+
+ if (useLocalConfig)
+ loadConfig(local_value_location,true,true)
+
+ llama(options)
+
+
+//*************
+
+ function loadConfig(file, noFault, noMessage) {
+ try {
+ var content = fs.readFileSync(file, 'utf8')
+ var update = JSON.parse(content)
+ for (u in update) {
+ if (typeof u === 'string') {
+ options[u] = update[u]
+ }
+ }
+ } catch(e) {
+ if (noFault) {
+ if (!noMessage) console.warn(e)
+ } else
+ throw e
+ }
+ }
+
+})
View
@@ -0,0 +1,14 @@
+/*
+ * Copyright 2012 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+ */
+
+// simple export to move on with
+
+module.exports = require('./lib/snail.js')
View
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2012 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and limitations under the License.
+ */
+
+module.exports = function (options) {
+ require('knit').inject(function (knit, underscore, wool, mime) {
+ return knit.config(function (bind) {
+ bind('_').to(underscore._)
+ bind('mime').to(mime.lookup)
+ bind('urlparser').to(require('url').parse)
+ bind('default_field').to('url')
+ bind('def_filter').to(wool.filter)
+ bind('http_status').to(wool.http_status)
+ bind('root_filter').to(function (filter) {return filter.root_filter}).is('builder')
+ bind('filter').to(function (def_filter) {return def_filter('url')}).is('builder')
+ bind('dispatch').to(wool.dispatch).is('builder')
+ bind('static').to(function (http_status, mime, urlparser, fs) {return wool.static(http_status, mime, urlparser, fs);}).is('builder')
+ bind('template').to(wool.template).is('builder')
+ }).inject(function(http, fs, _, filter, dispatch, static, http_status, template) {
+ var static_methods = ['HEAD', 'GET']
+ var template_regex = options.regex ? new RegExp(options.regex) : template.regex
+
+ return http.createServer(function (req,res) {
+ console.log('%s %s %s',Date(), req.method, req.url)
+ dispatch.chain([
+ dispatch.rule({method: static_methods, url: function(url) {return options.static.some(function(x) {return url.indexOf(x)==0})}}, static.build(0,options.folder)),
+ dispatch.rule({method: static_methods, url: template_regex}, template.build(options.template_folder || options.folder, options.data_folder || options.folder, template_regex)),
+ dispatch.rule({method: filter.not(static_methods)}, function(req,res) { http_status(405)(res); }),
+ dispatch.rule(true, function(req,res) { http_status(404)(res); })
+ ])(req,res)
+ })
+ })
+ }).listen(options.PORT)
+}
View
@@ -0,0 +1,23 @@
+{
+ "author": "Nicolas Lochet",
+ "name": "llama",
+ "description": "Small convention-oriented, filesystem JSON-based, CMS server",
+ "version": "0.0.0",
+ "homepage": "https://github.com/nicocube/llama",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/nicocube/llama.git"
+ },
+ "main": "index.js",
+ "dependencies": {
+ "knit": "*",
+ "wool": "*",
+ "underscore": "*"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "bin" : { "llama" : "./bin/llama-script.js" }
+}

0 comments on commit af8d791

Please sign in to comment.