Skip to content

Instantly share code, notes, and snippets.

@saurabhnanda
Created June 26, 2019 12:04
Show Gist options
  • Save saurabhnanda/f5eec669767b69cc7f9fa5c50a5786d9 to your computer and use it in GitHub Desktop.
Save saurabhnanda/f5eec669767b69cc7f9fa5c50a5786d9 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Try where
import Servant
import Servant.Server
import Data.Text
import Data.Proxy
import Network.Wai.Handler.Warp as Warp
import Debug.Trace
appContext = ("abc" :: Text) :. EmptyContext
data EnsureAuth
data User
instance ( HasServer api context
, HasContextEntry context Text) => HasServer (EnsureAuth :> api) context where
type ServerT (EnsureAuth :> api) m = ServerT api m
-- hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt . s
route Proxy context subserver = traceShow (getContextEntry context :: Text) $ route (Proxy :: Proxy api) context subserver
type Api = EnsureAuth :> (
"test1" :> Capture "params" Text :> Get '[JSON] String
:<|> "test2" :> Get '[JSON] String
)
test1 :: Text -> Handler String
test1 x = pure "test1"
test2 :: Handler String
test2 = pure "test2"
serveApp = Warp.run 8001 $ serveWithContext (Proxy :: Proxy Api) appContext (test1 :<|> test2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment