Created
December 3, 2017 19:24
-
-
Save begriffs/16023778f9dd1d1ba5cca172fe7f5152 to your computer and use it in GitHub Desktop.
RC "Database server"
This file contains 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
import Control.Concurrent.MVar | |
import qualified Data.Map as M | |
import Network.HTTP.Types.Status | |
import Web.Scotty | |
import Protolude hiding (get) | |
main :: IO () | |
main = do | |
dbSync <- newMVar M.empty | |
scotty 4000 $ do | |
get "/set" $ do | |
db <- liftIO $ takeMVar dbSync | |
newVals <- M.fromList <$> params | |
liftIO . putMVar dbSync $ M.union newVals db | |
get "/get" $ do | |
k <- param "key" | |
db <- liftIO $ readMVar dbSync | |
case M.lookup k db of | |
Nothing -> status status404 | |
(Just v) -> text v |
This file contains 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
name: rc | |
version: 0.0.0 | |
cabal-version: >= 1.8 | |
build-type: Simple | |
executable rc | |
hs-source-dirs: . | |
main-is: Main.hs | |
ghc-options: -Wall -threaded -O2 -rtsopts -with-rtsopts=-N | |
extensions: OverloadedStrings, NoImplicitPrelude | |
build-depends: base >= 4 && < 5 | |
, containers | |
, http-types | |
, protolude | |
, scotty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment