UNPKG

alpha-one

Version:

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

162 lines (134 loc) 7.8 kB
############################################################################################################ njs_path = require 'path' # njs_fs = require 'fs' #........................................................................................................... OPTIONS = require 'coffeenode-options' TYPES = require 'coffeenode-types' TRM = require 'coffeenode-trm' rpr = TRM.rpr.bind TRM badge = 'α1/main' 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 # #........................................................................................................... # ### https://github.com/goodeggs/teacup ### # teacup = require 'teacup' # # #........................................................................................................... templates = require './templates' express = require 'express' # flash = require 'express-flash' #........................................................................................................... RedisStore = ( require 'connect-redis' ) express #........................................................................................................... app_info = OPTIONS.get_app_info() ### Name used to store info on the `request` object: ### app_key = app_info[ 'name' ] #........................................................................................................... A1 = require './main' view = A1.get_view_for templates restrict_view = A1.get_restrict_view_for templates #........................................................................................................... milliseconds = 1 seconds = 1000 * milliseconds minutes = 60 * seconds hours = 60 * minutes days = 24 * hours weeks = 7 * days months = 30 * days years = 365 * days #----------------------------------------------------------------------------------------------------------- server_options = 'host': '127.0.0.1' 'port': 80 #----------------------------------------------------------------------------------------------------------- express_options = 'static-routes': [ 'public' 'common' ] '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 redis_options = 'host': '127.0.0.1' 'port': 6379 express_options[ 'redis' ] = redis_options express_options[ 'session' ] = 'secret': 'verysecretindeed' 'store': new RedisStore host: redis_options[ 'host' ], port: redis_options[ 'port' ], ttl: 2 * weeks 'cookie': maxAge: 2 * weeks ( require './validate-redis-is-running' ) redis_options # possible to define logger format: # express.logger.format 'mingkwai', ':method :url :status :res[content-length] - :response-time ms' #........................................................................................................... app = express() #----------------------------------------------------------------------------------------------------------- # Middleware #----------------------------------------------------------------------------------------------------------- app.use A1.get_request_counter() app.use express.json() app.use express.urlencoded() app.use express.cookieParser 'ztfgGHzqk3' app.use express.session express_options[ 'session' ] # app.use app.router # must be placed *after* cookieParser and session #........................................................................................................... # app.use A1.delay_headers() app.use A1.add_request_options() # app.use A1.page_modifier() app.use A1.flash_notifications() #........................................................................................................... app.use express.logger express_options[ 'logger' ][ 'format' ] #----------------------------------------------------------------------------------------------------------- # Endpoints #----------------------------------------------------------------------------------------------------------- # Public Static Files do -> for static_route in express_options[ 'static-routes' ] static_route = '/'.concat njs_path.basename static_route fs_route = njs_path.join app_info[ 'home' ], static_route # app.get url_route, express.static static_route app.use static_route, express.static fs_route info "static route:", ( TRM.gold static_route ), ( TRM.grey '->', fs_route ) #........................................................................................................... app.use A1.show_sid express_options[ 'session' ][ 'secret' ] app.all '*', A1.show_debug_info() #----------------------------------------------------------------------------------------------------------- # Dynamic Endpoints #........................................................................................................... # General Locations app.get '/', view 'homepage' #........................................................................................................... # Boilerplate Locations app.get '/contact', view 'contact' app.get '/imprint', view 'imprint' app.get '/privacy', view 'privacy' #........................................................................................................... # Login / Logout Locations app.get '/welcome', view 'welcome', 'remember-location': no app.get '/goodbye', view 'goodbye', 'remember-location': no app.get '/login', view 'login_get', 'remember-location': no app.post '/login', view 'login_post', 'remember-location': no app.post '/signup', view 'signup_post', 'remember-location': no app.all '/logout', view 'logout', 'remember-location': no #........................................................................................................... # Restricted Locations app.get '/restricted', restrict_view 'user', 'restricted' #........................................................................................................... # Fallback View app.use view 'not_found' #=========================================================================================================== # SERVING #----------------------------------------------------------------------------------------------------------- app.listen server_options[ 'port' ], ( error ) -> throw error if error? #......................................................................................................... # for static_route in static_routes # info "static route: #{static_route}" log TRM.green "listening to #{server_options[ 'host' ]}:#{server_options[ 'port' ]}"