Can I modify my state using IO via the Scotty API? Currently, I have a state transformer inside IO monad to modify state with user input. But I want to achieve this via the Scotty API.
This is my state transformer types I currently have that I use to keep modifying the state type and allow for IO actions.
-- State Transformer type inside IO monad
type STIO st a = STM st IO a
-- State transformer inside a monad
newtype STM st m a = S (st -> m (a, st))
... and with this lift functions to lift an action into the IO monad:
lift :: Monad m => m a -> STM st m a
lift mx = S (\s -> do
x <- mx
return (x, s))
This is my current bare bones Scotty server:
port = 8080
main = server
server :: IO ()
server = do
print ("Starting Server at port " ++ show port)
scotty port $ do
get "/start" $ do
json ("{starting: "++"True"++"}")
In my head I am wanting something along these lines, but unsure how to implement it:
type State = Int
server :: STIO State ()
server = do
print ("Starting Server at port " ++ show port)
lift $ scotty port $ do
get "/start" $ do
updateCounterByOneInState
counter <- getCounterFromState
json $ "{count: " ++ counter ++ "}"
Is something like this even possible, or am I just getting confused?