-- | Stolen from rack:
--   The Rack::Static middleware intercepts requests for static files
--   (javascript files, images, stylesheets, etc) based on the url prefixes
--   passed in the options, and serves them using a Rack::File object. This
--   allows a Rack stack to serve both static and dynamic content.
module Hack.Contrib.Middleware.Static (static) where

import Hack
import Hack.Contrib.Utils
import Hack.Contrib.Middleware.File (file)

import MPSUTF8
import Prelude hiding ((.), (^), (>), (+))
import Data.Maybe
import List (find, isPrefixOf)

static :: Maybe String -> [String] -> Middleware
static root urls app = \env -> do
  let my_urls = if urls.null then ["/favicon.ico"] else urls

  let path = env.path_info .unescape_uri

  let can_serve = my_urls.find ( `isPrefixOf` path ) .isJust
  if can_serve
    then file root app env
    else app env