Skip to content

Instantly share code, notes, and snippets.

@begriffs
Created December 3, 2017 19:24
Show Gist options
  • Save begriffs/16023778f9dd1d1ba5cca172fe7f5152 to your computer and use it in GitHub Desktop.
Save begriffs/16023778f9dd1d1ba5cca172fe7f5152 to your computer and use it in GitHub Desktop.
RC "Database server"
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
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