scotty

Haskell Persistent: how get entity from db by key if i have key in integer variable?

时间秒杀一切 提交于 2019-12-04 02:17:03
I use Persistent orm with scotty web framework. I want to get value from db by id. These id are coming to me from GET request There are "get" function that takes "Key Entity" variable and returns "Maybe Entity". I use following code to get value from db k <- keyFromValues $ [(PersistInt64 myOwnIntVarFromRequest)] case k of Left _ -> {-some processing-} Right x -> do t <- liftIO . runDb $ get (x::Key Post) --Post is one of my models case t of Nothing -> {-processing-} Just x -> {-processing-} These code is extremly ugly. But i don't know how to do it better So my question is how obtain variable

Use StateT within Web.Scotty

我是研究僧i 提交于 2019-12-03 13:46:20
I'm trying to make a silly webserver that stores data as State . I'm using Web.Scotty . I've used ReaderT before with scotty to access config , but following the same approach doesn't work here. It resets the state on every request. I want to set the initial state when the program starts, then have that same state stick around for the whole life of the program. How can I make this work? (The following creates a new state every request) {-# LANGUAGE OverloadedStrings #-} import Web.Scotty.Trans import Control.Monad.State (StateT, evalStateT, lift) import qualified Control.Monad.State as S

Scotty: connection pool as monad reader

微笑、不失礼 提交于 2019-11-28 18:48:45
There are trillions of monad tutorial including the reader and it seems all clear when you read about it. But when you actually need to write, it becomes a different matter. I'v never used the Reader, just never got to it in practice. So I don't know how to go about it although I read about it. I need to implement a simple database connection pool in Scotty so every action can use the pool. The pool must be "global" and accessible by all action functions. I read that the way to do it is the Reader monad. If there are any other ways please let me know. Can you please help me and show how to do

Scotty: connection pool as monad reader

孤人 提交于 2019-11-27 11:40:34
问题 There are trillions of monad tutorial including the reader and it seems all clear when you read about it. But when you actually need to write, it becomes a different matter. I'v never used the Reader, just never got to it in practice. So I don't know how to go about it although I read about it. I need to implement a simple database connection pool in Scotty so every action can use the pool. The pool must be "global" and accessible by all action functions. I read that the way to do it is the