module IHP.Static (staticRouteShortcut) where

import Network.Wai
import qualified Data.ByteString.Char8 as ByteString

-- | Routes requests with a @/static/@ prefix directly to the static file server,
-- bypassing the full middleware stack (session, CORS, auto-refresh, etc.).
-- All other requests go through the normal middleware pipeline.
--
-- This improves performance for static assets since they only need to be
-- read from disk.
staticRouteShortcut :: Application -> Application -> Application
staticRouteShortcut :: Application -> Application -> Application
staticRouteShortcut Application
staticApp Application
fallbackApp Request
request Response -> IO ResponseReceived
respond =
    case Request
request.pathInfo of
        (Text
"static":[Text]
rest) ->
            let strippedRequest :: Request
strippedRequest = Request
request
                    { pathInfo = rest
                    , rawPathInfo = ByteString.drop (ByteString.length "/static") request.rawPathInfo
                    }
            in Application
staticApp Request
strippedRequest Response -> IO ResponseReceived
respond
        [Text]
_ -> Application
fallbackApp Request
request Response -> IO ResponseReceived
respond