Skip to content

Instantly share code, notes, and snippets.

@codedmart
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save codedmart/69358a9cc9649ad8017d to your computer and use it in GitHub Desktop.

Select an option

Save codedmart/69358a9cc9649ad8017d to your computer and use it in GitHub Desktop.
data ApiInfo = ApiInfo {
name :: T.Text,
version :: T.Text
} deriving (Eq, Generic)
instance FromJSON ApiInfo
instance ToJSON ApiInfo
data Config = Config {
apiInfo :: ApiInfo
, dbHost :: HostName
, port :: Int
}
data TnConfig = TnConfig {
pool :: (Pool RethinkDBHandle)
, config :: Config
}
type API = Get '[JSON] ApiInfo
:<|> "users" :> Get '[JSON] [SecureUser]
api :: Proxy API
api = Proxy
readerToEither' :: TnConfig -> forall a. Reader TnConfig a -> EitherT ServantErr IO a
readerToEither' t r = return (runReader r t)
-- Tried this but not sure what to do
{-readerTToEither' :: TnConfig -> forall a. ReaderT TnConfig a -> EitherT ServantErr IO a-}
readerTToEither' t r = return (runReaderT r t)
readerToEither :: TnConfig -> Reader TnConfig :~> EitherT ServantErr IO
readerToEither t = Nat $ readerToEither' t
readerServerT :: TnConfig -> ServerT API (Reader TnConfig)
readerServerT t = getApiInfo :<|> getUsers
where
getApiInfo :: Reader TnConfig ApiInfo
getApiInfo = info
getUsers :: Reader TnConfig [SecureUser]
getUsers = readerTToEither' t list
readerServer :: TnConfig -> Server API
readerServer t = enter (readerToEither t) (readerServerT t)
runPool :: (Expr q, Result r) => Pool RethinkDBHandle -> q -> IO r
runPool p q = withResource p $ \h -> run h q
-- Obviously this does not work
runDB :: (Result r, Expr q) => q -> Reader TnConfig r
runDB q = do
TnConfig p _ <- ask
r <- runPool p q
return r
data User = User {
id :: Maybe UUID
, firstName :: Text
, lastName :: Text
, email :: Text
, apiKeys :: Maybe [Text]
, hashedPassword :: Maybe Text
, createdAt :: Maybe UTCTime
, updatedAt :: Maybe UTCTime
} deriving (Show, Generic)
instance FromJSON User where
instance ToJSON User where
instance FromDatum User
instance ToDatum User where
-- Used for returning a user without the hashedPassword
newtype SecureUser = SecureUser User deriving (Show, Generic)
instance FromJSON SecureUser
instance ToJSON SecureUser where
toJSON (SecureUser user) = Object $ HashMap.delete "hashedPassword" obj
where (Object obj) = toJSON user
instance FromDatum SecureUser
instance ToDatum SecureUser where
table = R.table "users"
list :: Reader TnConfig [SecureUser]
list = runDB table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment