var path = require('path')
var mapeoserver = require('mapeo-server')
var ecstatic = require('ecstatic')
var osmserver = require('osm-p2p-server')
var http = require('http')

module.exports = function (osm, media) {
  var osmrouter = osmserver(osm)
  var staticRoot = path.dirname(require.resolve('mapeo-styles'))
  var mapeo = mapeoserver(osm, media, {staticRoot})

  var server = http.createServer(function (req, res) {
    var staticHandler = ecstatic({
      root: path.join(__dirname, '..', 'static'),
      baseDir: 'static',
      handleError: false
    })

    function next () {
      var m = osmrouter.handle(req, res) || mapeo.handle(req, res)
      if (!m) {
        res.statusCode = 404
        res.end('Not Found')
      }
    }
    staticHandler(req, res, next)
  })
  server.mapeo = mapeo
  return server
}

@@ -526,68 +526,17 @@
 tic'
-,%0A      handleError: false%0A    %7D)%0A%0A    function next () %7B%0A  
+%0A    %7D)%0A%0A
     
@@ -600,18 +600,16 @@
 es)%0A    
-  
 if (!m) 
@@ -606,24 +606,107 @@
   if (!m) %7B%0A
+      staticHandler(req, res, function (err) %7B%0A        if (err) console.error(err)%0A
         res.
@@ -762,49 +762,16 @@
    %7D
+)
 %0A    %7D%0A
-    staticHandler(req, res, next)%0A
   %7D)

handle errors within esctatic after mapeo server and osm-p2p-server have been called