Created
September 14, 2016 02:47
-
-
Save psibi/f9fbe9fb434cdc9fce700dc7c399584b to your computer and use it in GitHub Desktop.
Code showing Subsite doesn't handle authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE PackageImports #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
{-# LANGUAGE RecordWildCards #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
module YesodServantExample where | |
import "text" Data.Text (Text) | |
import "wai" Network.Wai | |
import "servant-server" Servant hiding (Handler) | |
import "yesod" Yesod | |
import "yesod-core" Yesod.Core.Types | |
import Yesod.Auth | |
import Yesod.Auth.Dummy | |
-- import Foundation | |
type AppAPI = "items" :> Get '[JSON] Value | |
appAPIServerMock :: Server AppAPI | |
appAPIServerMock = return $ toJSON [ object [ "id" .= (1 :: Int) | |
, "name" .= ("one" :: Text) | |
] | |
, object [ "id" .= (2 :: Int) | |
, "name" .= ("two" :: Text) | |
] | |
, object [ "id" .= (3 :: Int) | |
, "name" .= ("three" :: Text) | |
] | |
] | |
appAPIProxy :: Proxy AppAPI | |
appAPIProxy = Proxy | |
data EmbeddedAPI = EmbeddedAPI { eapiApplication :: Application | |
} | |
instance RenderRoute EmbeddedAPI where | |
data Route EmbeddedAPI = EmbeddedAPIR ([Text], [(Text, Text)]) | |
deriving(Eq, Show, Read) | |
renderRoute (EmbeddedAPIR t) = t | |
instance ParseRoute EmbeddedAPI where | |
parseRoute t = Just (EmbeddedAPIR t) | |
instance (Yesod master, YesodAuth master) => YesodSubDispatch EmbeddedAPI (HandlerT master IO) where | |
yesodSubDispatch YesodSubRunnerEnv{..} req = resp | |
where | |
master = yreSite ysreParentEnv | |
site = ysreGetSub master | |
resp = eapiApplication site req | |
data App = App { appAPI :: EmbeddedAPI | |
} | |
mkYesod "App" [parseRoutes| | |
/ HomeR GET | |
/api/v1/ SubsiteR EmbeddedAPI appAPI | |
|] | |
getHomeR :: Handler Html | |
getHomeR = defaultLayout $ [whamlet| | |
<h1>Hello there! | |
<p> | |
Try testing our items API at | |
<a href=@{itemsApiRoute}>@{itemsApiRoute} | |
|] | |
where | |
itemsApiRoute = SubsiteR (EmbeddedAPIR (["items"], [])) | |
instance Yesod App where | |
authRoute _ = Just HomeR | |
isAuthorized HomeR _ = return Yesod.Core.Types.Authorized | |
isAuthorized (SubsiteR _) _ = return $ Yesod.Core.Types.Unauthorized "you must be admin" | |
isAuthorized _ _ = return $ Yesod.Core.Types.Unauthorized "you must be admin" | |
instance YesodAuth App where | |
type AuthId App = Text | |
getAuthId = error "getAuthId" | |
authenticate creds = error "authenticate" | |
maybeAuthId = error "maybeAuthId" | |
loginDest _ = HomeR | |
logoutDest _ = HomeR | |
authPlugins _ = error "authPlugins" | |
authHttpManager = error "authHttpManager" | |
instance RenderMessage App FormMessage where | |
renderMessage _ _ = defaultFormMessage | |
run :: Int -> IO () | |
run port = warp port (App (EmbeddedAPI api)) | |
where | |
api = serve appAPIProxy appAPIServerMock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment