Created
February 17, 2012 18:04
-
-
Save johnnyt/1854660 to your computer and use it in GitHub Desktop.
Amber Smalltalk WebServer - runs on top of node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Smalltalk current createPackage: 'Amber-Node' properties: #{}! | |
Object subclass: #WebServer | |
instanceVariableNames: 'port app dirname faye app express bayeux fs assets' | |
category: 'Amber-Node'! | |
!WebServer methodsFor: 'initialization'! | |
initialize | |
super initialize. | |
<self['@dirname'] = __dirname>. | |
port := process env at: 'PORT'. "Heroku will provide a port in production" | |
port ifNil: [ port := 4001 ]. | |
fs := self require: 'fs'. | |
express := self require: 'express'. | |
assets := self require: 'connect-assets'. | |
faye := self require: './lib/faye-node.js'. | |
app := express createServer. | |
bayeux := (faye at: 'NodeAdapter') newValue: #{ 'mount' -> '/faye' }. | |
self | |
configure; | |
initializeRoutes. | |
! | |
initializeRoutes | |
app | |
get: '/' do: [ :req :res | res render: 'index' ]; | |
put: '/*' do: [ :req :res | self handlePUT: req respondTo: res ] | |
! | |
configure | |
bayeux attach: app. | |
app configure: [ app | |
set: 'view options' val: #{ 'layout' -> false }; | |
set: 'view engine' val: 'jade'; | |
use: (express static: dirname, '/public'); | |
use: express logger; | |
use: assets value; | |
use: (app at: 'router') ]; | |
configure: 'development' with: [ app | |
use: (express errorHandler: #{ 'dumpExceptions' -> true . 'showStack' -> true }) ]; | |
configure: 'production' with: [ app | |
use: (express errorHandler) ] | |
! ! | |
!WebServer methodsFor: 'private'! | |
require: aModuleString | |
"call to the Node require function" | |
^require value: aModuleString | |
! ! | |
!WebServer methodsFor: 'request handling'! | |
handlePUT: aRequest respondTo: aResponse | |
| path stream | | |
path := '.', aRequest url asString. | |
stream := fs createWriteStream: path. | |
aRequest | |
setEncoding: 'utf8'; | |
on: 'data' do: [ :chunk | stream write: chunk ]; | |
on: 'end' do: [ | |
stream end. | |
self respondOKTo: aResponse ] | |
! | |
respondOKTo: aResponse | |
aResponse send: 'Success'. | |
! ! | |
!WebServer methodsFor: 'starting'! | |
start | |
app listen: port asString do: [ | |
console log: 'Listening on port: ', port asString ] | |
! ! | |
!WebServer class methodsFor: 'main'! | |
main | |
^self new start | |
! ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment