Created
August 12, 2019 03:45
-
-
Save danstn/c29eb18174f8324f750ab43283b4acaa to your computer and use it in GitHub Desktop.
This file contains 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
newtype PrivateData = PrivateData { ssshhh :: Text } | |
deriving (Eq, Show, Generic) | |
newtype PublicData = PublicData { somedata :: Text } | |
deriving (Eq, Show, Generic) | |
instance ToJSON PrivateData | |
instance ToJSON PublicData | |
type PublicAPI = Get '[JSON] [PublicData] | |
type PrivateAPI = Get '[JSON] PrivateData | |
type BasicAPI = "public" :> PublicAPI | |
:<|> "private" :> BasicAuth "foo-realm" Text :> PrivateAPI | |
authCheck :: BasicAuthCheck Text | |
authCheck = | |
let check (BasicAuthData username password) = | |
if username == "bob" && password == "secret" | |
then return (Authorized "You're in!") | |
else return Unauthorized | |
in BasicAuthCheck check | |
basicAuthApi :: Proxy BasicAPI | |
basicAuthApi = Proxy | |
basicAuthServerContext :: Context (BasicAuthCheck Text ': '[]) | |
basicAuthServerContext = authCheck :. EmptyContext | |
basicAuthServer :: ServerT BasicAPI App | |
basicAuthServer = | |
let publicApi = return [PublicData "lol", PublicData "bro"] | |
privateApi (res :: Text) = return (PrivateData res) | |
in publicApi :<|> privateApi | |
app :: Config -> Application | |
app config = | |
serveWithContext | |
basicAuthApi | |
basicAuthServerContext | |
(hoistServerWithContext | |
basicAuthApi | |
(Proxy :: Proxy (BasicAuthCheck Text ': '[])) | |
(convertApp config) | |
basicAuthServer | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment