Skip to content

Instantly share code, notes, and snippets.

@danstn
Created August 12, 2019 03:45
Show Gist options
  • Save danstn/c29eb18174f8324f750ab43283b4acaa to your computer and use it in GitHub Desktop.
Save danstn/c29eb18174f8324f750ab43283b4acaa to your computer and use it in GitHub Desktop.
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