Last active
August 29, 2015 14:21
-
-
Save codedmart/69358a9cc9649ad8017d to your computer and use it in GitHub Desktop.
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
| 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 | |
| } |
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
| 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) |
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
| 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 |
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
| 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