-- | 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 Data.Maybe
import Hack
import Hack.Contrib.Middleware.File (file)
import Hack.Contrib.Utils
import List (find, isPrefixOf)
import MPS.Light
import Prelude hiding ((.), (^), (>), (+))

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