UNPKG

alpha-one

Version:

ideas about recurring tasks in Web- and Backend-Application building

250 lines (195 loc) 10.3 kB
############################################################################################################ # ERROR = require 'coffeenode-stacktrace' # njs_util = require 'util' njs_path = require 'path' # njs_fs = require 'fs' #........................................................................................................... TYPES = require 'coffeenode-types' TRM = require 'coffeenode-trm' rpr = TRM.rpr.bind TRM badge = 'using-express' log = TRM.get_logger 'plain', badge info = TRM.get_logger 'info', badge whisper = TRM.get_logger 'whisper', badge alert = TRM.get_logger 'alert', badge debug = TRM.get_logger 'debug', badge warn = TRM.get_logger 'warn', badge help = TRM.get_logger 'help', badge echo = TRM.echo.bind TRM # rainbow = TRM.rainbow.bind TRM # suspend = require 'coffeenode-suspend' # step = suspend.step # after = suspend.after # eventually = suspend.eventually # immediately = suspend.immediately # every = suspend.every # TEXT = require 'coffeenode-text' #........................................................................................................... ### https://github.com/goodeggs/teacup ### teacup = require 'teacup' #........................................................................................................... templates = require './templates' express = require 'express' authom = require 'authom' TRM.dir authom #----------------------------------------------------------------------------------------------------------- write_ok_html_header = ( request, response ) -> status_code = 200 #......................................................................................................... headers = 'Content-Type': 'text/html' 'Connection': 'keep-alive' 'Transfer-Encoding': 'chunked' #......................................................................................................... return response.writeHead status_code, headers #----------------------------------------------------------------------------------------------------------- respond = ( request, response, next ) -> write_ok_html_header request, response response.write """<link rel='shortcut icon' href='/public/favicon.ico?v=#{1 * new Date()}'>""" response.write "<div>RQ##{request_count}</div>" response.end "<div>hello world</div>" #----------------------------------------------------------------------------------------------------------- request_counter_middleware = ( request, response, next ) -> request[ 'count' ] = ( request_count += 1 ) next() #----------------------------------------------------------------------------------------------------------- request_count = 0 #----------------------------------------------------------------------------------------------------------- login = ( user, password, handler ) -> debug "authenticating #{user}:#{password}" handler null, true #----------------------------------------------------------------------------------------------------------- server_options = 'host': 'foo.bar' 'port': 80 #----------------------------------------------------------------------------------------------------------- express_options = 'logger': 'format': 'short' # ':remote-addr - :method :url HTTP/:http-version :status :res[content-length] - :response-time ms' 'format': 'tiny' # ':method :url :status :res[content-length] - :response-time ms' 'format': 'default' # ':remote-addr - - [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"' 'format': 'mingkwai' # own format 'format': 'dev' # concise output colored by response status for development use # possible to define logger format: express.logger.format 'mingkwai', ':method :url :status :res[content-length] - :response-time ms' #----------------------------------------------------------------------------------------------------------- app = express() app.use request_counter_middleware app.use express.logger express_options[ 'logger' ][ 'format' ] # app.use express.basicAuth login # TRM.dir 'teacup', teacup # app.configure -> app.engine "coffee", teacup.render # app.engine 'coffee', teacup.render ### https://github.com/wdavidw/node-connect-coffee-script ### ### http://learnboost.github.io/stylus/docs/middleware.html ### # stylus.middleware(options) # Options # Return Connect middleware with the given options. # `serve` Serve the stylus files from `dest` [true] # `force` Always re-compile # `src` Source directory used to find .styl files # `dest` Destination directory used to output .css files # when undefined defaults to `src`. # `compile` Custom compile function, accepting the arguments # `(str, path)`. # `compress` Whether the output .css files should be compressed # `firebug` Emits debug infos in the generated css that can # be used by the FireStylus Firebug plugin # `linenos` Emits comments in the generated css indicating # the corresponding stylus line ############################################################################################################ # create servers for the services you'll be using # authom.createServer {} # facebook credentials # authom.createServer {} # github credentials # authom.createServer {} # google credentials # authom.createServer {} # twitter credentials # ... et cetera service_credential_records = [ service: "github" id: "7e38d12b740a339b2d31" secret: "116e41bd4cd160b7fae2fe8cc79c136a884928c3" ] # debug __filename for service_credentials in service_credential_records TRM.log TRM.rainbow service_credentials authom.createServer service_credentials app.get "/", (req, res) -> debug 'app.get', "/" res.send "<html>" + "<body style='font: 300% sans-serif'>" + # "<div><a href='/auth/37signals'>Who am I on 37Signals?</a></div>" + # "<div><a href='/auth/dwolla'>Who am I on Dwolla?</a></div>" + "<div><a href='/auth/github'>Who am I on Github?</a></div>" + # "<div><a href='/auth/google'>Who am I on Google?</a></div>" + # "<div><a href='/auth/facebook'>Who am I on Facebook?</a></div>" + # "<div><a href='/auth/fitbit'>Who am I on Fitbit?</a></div>" + # "<div><a href='/auth/foursquare'>Who am I on Foursquare?</a></div>" + # "<div><a href='/auth/gowalla'>Who am I on Gowalla?</a></div>" + # "<div><a href='/auth/instagram'>Who am I on Instagram?</a></div>" + # "<div><a href='/auth/linkedin'>Who am I on LinkedIn?</a></div>" + # "<div><a href='/auth/meetup'>Who am I on Meetup?</a></div>" + # "<div><a href='/auth/soundcloud'>Who am I on SoundCloud?</a></div>" + # "<div><a href='/auth/twitter'>Who am I on Twitter?</a></div>" + # "<div><a href='/auth/windowslive'>Who am I on Windows Live?</a></div>" + # "<div><a href='/auth/dropbox'>Who am I on Dropbox?</a></div>" + # "<div><a href='/auth/bitbucket'>Who am I on Bitbucket?</a></div>" + # "<div><a href='/auth/vkontakte'>Who am I on Vkontakte?</a></div>" + "</body>" + "</html>" authom.on "auth", (req, res, data) -> debug 'authom.on', "auth" res.send "<html>" + "<body>" + "<div style='font: 300% sans-serif'>You are " + data.id + " on " + data.service + ".</div>" + "<pre><code>" + JSON.stringify(data, null, 2) + "</code></pre>" + "</body>" + "</html>" authom.on "error", (req, res, data) -> debug 'authom.on', "error" res.error "An error occurred: " + JSON.stringify(data) # # called when a user is authenticated on any service # authom.on 'auth', ( request, response, data ) -> # debug 'auth', data # # called when an error occurs during authentication # authom.on 'error', ( request, response, data ) -> # debug 'error', data # TRM.dir app app.get '/auth/:service', authom.app # app.listen 1024, -> # console.log("listening at http://authom.jedschmidt.com/") authom.listen app #=========================================================================================================== app.get "/auth/:service", authom.app authom.on "auth", (req, res, data) -> req.session.user = data res.redirect "/dashboard" filter = (req, res, next) -> unless not req.session.user next() else res.redirect "/" app.get "/dashboard", filter, (req, res) -> res.render "dashboard", title: "dashboard" ############################################################################################################ # TEST MIDDLEWARE (see http://howtonode.org/connect-it) test_middleware = -> debug "setting up test_middleware" return ( request, response, next ) -> debug 'test' write_ok_html_header request, response response.write templates.test 'request-count': request[ 'count' ] response.end() # Calling the handler gives us a place to handle startup matters: app.use '/test', test_middleware() # ############################################################################################################ # # TEST RENDER (see http://stackoverflow.com/questions/14115893/nodejs-render-raw-html-with-express, # # https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x) # render_foo = ( filename, options, handler ) -> # debug filename # debug options # return handler "rendering foo" # app.engine 'foo', render_foo # app.render 'foo', render_foo ############################################################################################################ static_route = njs_path.join __dirname, '..', 'public' info "static route: #{static_route}" app.use '/public', express.static static_route app.use respond app.listen server_options[ 'port' ] log TRM.green "listening to #{server_options[ 'host' ]}:#{server_options[ 'port' ]}"