 http = require "http"
 util = require "util"
 
-# This middleware captures the relevant (and unrelevant) data when
-# the request comes in and attaches the data to the request so it
-# can later be used by whoever might needs this. At the moment it
-# just captures some rudimentary data, such as timestamp and UUID.
-module.exports.capture = (kernel) ->
-    (request, response, next) ->
-        sent = request.headersSent
-        request.uuid = uuid.v1()
-        request.date = new Date
-        request.kernel = kernel
-        next() unless sent
+{RedisStorage} = require "../exposure/session"
 
-# This middleware is a little handy utility that merges the set
-# of parameters, specifically the ones transferred via query or
-# via body mechanism into one object that can be used to easily
-# access the parameters without thinking about transfer mechanism.
-module.exports.params = (kernel) ->
-    (request, response, next) ->
-        body = request.body or {}
-        query = request.query or {}
-        request.params = Object.create {}
-        _.extend request.params, query
-        _.extend request.params, body
-        next() unless request.headersSent
+# This middleware is really a wrapper around the `Connect` logger
+# that pipes all the request logs to the `Winston` instances that
+# is used throughout the framework to provide logging capabilities.
+# The format is takes from the `NConf` config or the default `dev`.
+module.exports.logger = (kernel) ->
+    levelKey = "log:request:level"
+    formatKey = "log:request:format"
+    format = nconf.get(formatKey) or "dev"
+    level = nconf.get(levelKey) or "debug"
+    filter = (string) -> string.replace "\n", ""
+    writer = (data) -> logger.log level, filter data
+    options = stream: write: writer
+    options.format = format
+    connect.logger options
 
-# A middleware that adds a `redirect` method to the response object.
-# This redirects to the supplied URL with the 302 status code and
-# the corresponding reason phrase. This method also sets some of the
-# necessary headers, such as nullary `Content-Length` and some other.
-module.exports.redirect = (kernel) ->
-    (request, response, next) ->
-        response.redirect = (url, status) ->
-            relocated = status or 302
-            codes = http.STATUS_CODES
-            message = codes[relocated]
-            response.setHeader "Location", url
-            response.setHeader "Content-Length", 0
-            response.writeHead relocated, message
-            response.end undefined
-        next() unless request.headersSent
+# This middleware is really a wrapper around the `Connect` session
+# middleware. The reason it wraps it is to automatically configure
+# the session storage using the kernel and scoping configuration
+# data. It is automatically connected by the kernel instance.
+module.exports.session = (kernel) ->
+    options = nconf.get "session"
+    redis = _.isObject nconf.get "redis"
+    noSession = "No session settings in scope"
+    useRedis = "Using Redis session storage engine"
+    options.store = RedisStorage.obtain() if redis
+    assert _.isObject(options), noSession
+    logger.info useRedis.blue if redis
+    return connect.session options
 
 # A middleware that adds a `send` method to the response object.
 # This allows for automatic setting of `Content-Type` headers

            _.find merged, handles
        next() unless request.headersSent

# This middleware is really a wrapper around the `Connect` logger
# that pipes all the request logs to the `Winston` instances that
# is used throughout the framework to provide logging capabilities.
# The format is takes from the `NConf` config or the default `dev`.
module.exports.logger = (kernel) ->
    levelKey = "log:request:level"
    formatKey = "log:request:format"
    format = nconf.get(formatKey) or "dev"
    level = nconf.get(levelKey) or "debug"
    filter = (string) -> string.replace "\n", ""
    writer = (data) -> logger.log level, filter data
    options = stream: write: writer
    options.format = format
    connect.logger options

# This middleware is really a wrapper around the `Connect` session
# middleware. The reason it wraps it is to automatically configure
# the session storage using the kernel and scoping configuration
# data. It is automatically connected by the kernel instance.
module.exports.session = (kernel) ->
    options = nconf.get "session"
    defined = _.isObject options
    noSession = "No session settings in scope"
    throw new Error noSession unless defined
    connect.session options

###

_ = require "lodash"
assert = require "assert"
uuid = require "node-uuid"
asciify = require "asciify"
connect = require "connect"
