Skip to content

Commit

Permalink
[ @ ] Feature: Load addons/screw from npm
Browse files Browse the repository at this point in the history
  • Loading branch information
pnegri committed Nov 9, 2011
1 parent dad443c commit 3244463
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
7 changes: 7 additions & 0 deletions lib/_monkey-patching.coffee
@@ -1,3 +1,10 @@
require 'coffee-script'
Common = require __dirname + '/common'
require __dirname + '/_monkey-fs'

String.prototype.replaceLast = ( search, replace ) ->
string = this
n = string.lastIndexOf( search )
if n >= 0 && n + replace.length >= string.length
string = string.substring(0, n) + replace
string
43 changes: 25 additions & 18 deletions lib/arcabouco.coffee
Expand Up @@ -5,7 +5,6 @@ require __dirname + '/_monkey-patching'
Haml = require 'haml'
#Common.Http.ServerResponse.prototype.testing = 'BlaBleBli'


Common.Http.ServerResponse.prototype.redirectTo = ( url ) ->
@writeHead 302, { 'Location': url }
@end()
Expand Down Expand Up @@ -86,12 +85,13 @@ class Arcabouco
@Template.loadTemplate Common.Path.normalize (__dirname + '/../views/500.haml')

setInterval =>
# TODO: SEPARATE THIS INTO A CLASS
@requestsPerSecond = @_requestsCounter
@_requestsCounter = 0
# console.log 'Requests per Second: ' + @requestsPerSecond
# console.log 'Mem: ' + Common.Os.totalmem() + ' / ' + Common.Os.freemem()
# console.log 'Load AVG: '
# console.log Common.Os.loadavg()
totalMemory = Common.Os.totalmem()
freeMemory = Common.Os.freemem()
loadAverage = Common.Os.loadavg()

cpus = Common.Os.cpus()
user = 0
nice = 0
Expand Down Expand Up @@ -119,11 +119,8 @@ class Arcabouco
@_lastCPU.idle = idle
@_lastCPU.irq = irq
@_lastCPU.total = user+nice+sys+idle+irq

# console.log (current_user+current_nice+current_sys+current_irq) / current_total
# console.log current_idle / current_total

# console.log Common.Os.cpus()
, 1000

addRoutingToMethod : ( path, method, indexOfController ) ->
Expand Down Expand Up @@ -156,16 +153,17 @@ class Arcabouco
# mountApplicationWithObject
# mountApplicationWithFile

add: ( ControllerObject ) ->
ControllerObject.bootstrap( this ) if ControllerObject.bootstrap
@parseControllerRoutes @controllerInstances.push(ControllerObject)-1

contructRoutingForPattern : ( pattern ) ->
params = []
buildPattern = pattern.replace /\{(.*?)\}/g,
( match, sub1 ) ->
params.push sub1
return '([^\/]+)'
replacer = '([^\/]+)'
n = buildPattern.lastIndexOf( replacer )
if n >= 0 && n + replacer.length >= buildPattern.length
buildPattern = buildPattern.substring(0, n) + "([^$]+)";
buildPattern = buildPattern.replaceLast( '([^\/]+)', '([^$]+)' )
constructedRoute =
regex : new RegExp '^' + buildPattern
params: params
Expand All @@ -177,6 +175,9 @@ class Arcabouco
for pattern in orderedRouteNames
@avaiableRoutes.push @contructRoutingForPattern( pattern )

build: ->
@buildRouting()

parseRequest : ( request ) ->
request.setEncoding 'utf-8'
url = Common.Url.parse request.url, true
Expand Down Expand Up @@ -247,14 +248,20 @@ class Arcabouco
if hasRouted
break

# Replace with response.respondWithPageNotFound()
unless hasRouted
@respondWithNotFound response

createServer : ( serverPort ) ->
testServer = Common.Http.createServer( @dispatchRequest.bind( this ) )
testServer.listen( serverPort )
# testServer.close()
testServer
createServer : ->
Common.Http.createServer( @dispatchRequest.bind( this ) )

createSecureServer: ( privateKey, certificate ) ->
crypto = require 'crypto'
credentials = crypto.createCredentials {
key: privateKey
cert: certificate
}
secureServer = Common.Http.createServer( @dispatchRequest.bind(this) )
secureServer.setSecure( credentials )
secureServer

module.exports = Arcabouco

0 comments on commit 3244463

Please sign in to comment.