1 {-# LANGUAGE CPP, OverloadedStrings #-} 2 module Databrary.Controller.Root 3 ( viewRoot 4 , viewRootHandler 5 , viewConstantsHandler 6 , viewRobotsTxtHandler 7 , notFoundResponseHandler 8 ) where 9 10 import Control.Monad (when) 11 import qualified Data.Aeson.Types as JSON 12 import qualified Data.ByteString as BS 13 import Data.Maybe (isNothing) 14 import Data.Text (Text) 15 import Network.HTTP.Types (notFound404) 16 17 import Databrary.Has 18 -- import qualified Databrary.JSON as JSON 19 import Databrary.Service.Types 20 import Databrary.Action 21 import Databrary.Controller.Angular 22 import Databrary.View.Root 23 import Databrary.Web.Constants 24 import Databrary.View.Error (htmlNotFound) 25 26 -- TODO: remove when View.Template actionLink replaced 27 viewRoot :: ActionRoute API 28 viewRoot = action GET pathAPI $ \api -> viewRootHandler api [] 29 30 -- NEW HANDLERS 31 {- 32 getApiOrFail :: [(BS.ByteString, BS.ByteString)] -> Handler API 33 getApiOrFail params = 34 case params of 35 [] -> pure HTML 36 ("api", "api"):_ -> pure JSON 37 _ -> undefined -- TODO: action m error 38 -} 39 40 viewRootHandler :: API -> [(BS.ByteString, BS.ByteString)] -> Action 41 viewRootHandler api _ = -- TOOD: ensure GET 42 withAuth $ do 43 down <- peeks serviceDown 44 when (api == HTML && isNothing down) angular 45 case api of 46 JSON -> return $ okResponse [] JSON.emptyObject 47 HTML -> peeks $ okResponse [] . maybe htmlRoot htmlDown down 48 49 viewConstantsHandler :: [(BS.ByteString, BS.ByteString)] -> Action 50 viewConstantsHandler _ = -- TODO: ensure GET 51 withoutAuth $ return $ okResponse [] $ JSON.pairs constantsJSON 52 53 viewRobotsTxtHandler :: [(BS.ByteString, BS.ByteString)] -> Action 54 viewRobotsTxtHandler _ = -- TODO: ensure GET 55 withoutAuth $ return $ okResponse [] ("" :: Text) 56 -- NOTE: DEVEL/SANDBOX behavior wasn't copied here 57 58 notFoundResponseHandler :: [(BS.ByteString, BS.ByteString)] -> Action 59 notFoundResponseHandler _ = withoutAuth $ peeks $ response notFound404 [] . htmlNotFound