module Controller.Root
( viewRoot
, viewRootHandler
, viewConstantsHandler
, viewRobotsTxtHandler
, notFoundResponseHandler
) where
import Control.Monad (when)
import qualified Data.Aeson.Types as JSON
import qualified Data.ByteString as BS
import Data.Maybe (isNothing)
import Data.Text (Text)
import Network.HTTP.Types (notFound404)
import Has
import Service.Types
import Action
import Controller.Angular
import View.Root
import Web.Constants
import View.Error (htmlNotFound)
viewRoot :: ActionRoute API
viewRoot = action GET pathAPI $ \api -> viewRootHandler api []
viewRootHandler :: API -> [(BS.ByteString, BS.ByteString)] -> Action
viewRootHandler api _ =
withAuth $ do
down <- peeks serviceDown
when (api == HTML && isNothing down) angular
case api of
JSON -> return $ okResponse [] JSON.emptyObject
HTML -> peeks $ okResponse [] . maybe htmlRoot htmlDown down
viewConstantsHandler :: [(BS.ByteString, BS.ByteString)] -> Action
viewConstantsHandler _ =
withoutAuth $ return $ okResponse [] $ JSON.pairs constantsJSON
viewRobotsTxtHandler :: [(BS.ByteString, BS.ByteString)] -> Action
viewRobotsTxtHandler _ =
withoutAuth $ return $ okResponse [] ("" :: Text)
notFoundResponseHandler :: [(BS.ByteString, BS.ByteString)] -> Action
notFoundResponseHandler _ = withoutAuth $ peeks $ response notFound404 [] . htmlNotFound