checkPieces ss@StaticSettings {..} pieces req = do
    res <- lookupResult
    case res of
        LRNotFound -> return NotFound
        LRFile file -> serveFile ss req file
        LRFolder folder -> serveFolder ss pieces req folder
  where
    lookupResult :: IO LookupResult
    lookupResult = do
      nonIndexResult <- ssLookupFile pieces
      case nonIndexResult of
          LRFile{} -> return nonIndexResult
          _ -> do
              indexResult <- lookupIndices (map (\ index -> pieces ++ [index]) ssIndices)
              return $ case indexResult of
                  LRNotFound -> nonIndexResult
                  _ -> indexResult

    lookupIndices :: [Pieces] -> IO LookupResult
    lookupIndices (x : xs) = do
        res <- ssLookupFile x
        case res of
            LRNotFound -> lookupIndices xs
            _ -> return res
    lookupIndices [] = return LRNotFound
checkPieces ss@StaticSettings {..} pieces req = do
    res <- lookupResult
    case res of
        LRNotFound -> return NotFound
        LRFile file -> serveFile ss req file
        LRFolder folder -> serveFolder ss pieces req folder
  where
    lookupResult :: IO LookupResult
    lookupResult = do
      nonIndexResult <- ssLookupFile pieces
      case nonIndexResult of
          LRFile{} -> return nonIndexResult
          _ -> do
              indexResult <- lookupIndices (map (\ index -> pieces ++ [index]) ssIndices)
              return $ case indexResult of
                  LRNotFound -> nonIndexResult
                  _ -> indexResult

    lookupIndices :: [Pieces] -> IO LookupResult
    lookupIndices (x : xs) = do
        res <- ssLookupFile x
        case res of
            LRNotFound -> lookupIndices xs
            _ -> return res
    lookupIndices [] = return LRNotFound
checkPieces ss@StaticSettings {..} pieces req = do
    res <- lookupResult
    case res of
        LRNotFound -> return NotFound
        LRFile file -> serveFile ss req file
        LRFolder folder -> serveFolder ss pieces req folder
  where
    lookupResult :: IO LookupResult
    lookupResult = do
      nonIndexResult <- ssLookupFile pieces
      case nonIndexResult of
          LRFile{} -> return nonIndexResult
          _ -> do
              indexResult <- lookupIndices (map (\ index -> pieces ++ [index]) ssIndices)
              return $ case indexResult of
                  LRNotFound -> nonIndexResult
                  _ -> indexResult

    lookupIndices :: [Pieces] -> IO LookupResult
    lookupIndices (x : xs) = do
        res <- ssLookupFile x
        case res of
            LRNotFound -> lookupIndices xs
            _ -> return res
    lookupIndices [] = return LRNotFound