Log = require '../../util/log'

class ProxyDispatcher
  constructor: (@site)->
    unless @site.backend?
      throw new Error("Backend cannot be null.") 
    @backend = @site.backend;
    @cache = @site.cache;
    
  dispatch: (req, res, next)->
    Log.info "Proxying for \"#{req.method.toUpperCase()}: #{req.url}\""
    if @site.canCache(req)
      @cache.update req.url, "Cache Miss", (resource)-> resource.dispatchTo(res);
    else
      @backend.pipe(req, res)
    
module.exports = ProxyDispatcher;