{-# LANGUAGE OverloadedStrings #-}

import Air.Extra
import Control.Concurrent.STM
import Data.ByteString.Char8 (pack, unpack)
import Hack2 (httpHeaders, hackHeaders)
import Hack2.Contrib.Middleware.SimpleAccessLogger
import Hack2.Contrib.Middleware.XForwardedForToRemoteHost
import Hack2.Contrib.Response (set_content_type)
import Hack2.Contrib.Utils
import Hack2.Contrib.Utils (escape_html)
import Hack2.Handler.SnapServer
import Prelude ()
import Valkyria.Env
import Valkyria.Repo
import Valkyria.Server
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.Default as Default
import qualified Data.Map as M

middleware_stack = 
  [
  --  inspect
  --  \app env -> jailed (print env) >> app env
    x_forwarded_for_to_remote_host
  , simple_access_logger Nothing
  , text_sqlite
  ]



text_sqlite app = \env ->
  if env.path_info.unpack.starts_with "/repo/download/"
    then do
      r <- app env
      return - r.set_content_type "text/sqlite"
    else app env

run' = runWithConfig Default.def {port = 6001}

main = do
  putStrLn - "server started..."

  repo_state <- newTVarIO def
  run' - use middleware_stack - code_mushu_app repo_state


