From 72b1de1a28c23cf694ff5f982f0f637afd4ba5bc Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Wed, 29 Jul 2020 15:57:28 +0200 Subject: Stub server --- Makefile | 3 +++ server/cafp.cabal | 21 +++++++++++++++------ server/lib/Cafp/Main/GenerateElmTypes.hs | 17 +++++++++++++++++ server/lib/Cafp/Main/Server.hs | 29 +++++++++++++++++++++++++++++ server/src/GenerateElmTypes.hs | 13 ++----------- server/src/Server.hs | 4 ++++ 6 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 server/lib/Cafp/Main/GenerateElmTypes.hs create mode 100644 server/lib/Cafp/Main/Server.hs create mode 100644 server/src/Server.hs diff --git a/Makefile b/Makefile index 9260540..677f1b6 100644 --- a/Makefile +++ b/Makefile @@ -5,5 +5,8 @@ ELM_MESSAGES_MODULE=client/src/Messages.elm build: cd server && stack build +server: build + cd server && stack exec cafp-server + $(ELM_MESSAGES_MODULE): $(HS_SOURCES) (cd server && stack exec cafp-generate-elm-types) >$(ELM_MESSAGES_MODULE) diff --git a/server/cafp.cabal b/server/cafp.cabal index fc52b4f..805979f 100644 --- a/server/cafp.cabal +++ b/server/cafp.cabal @@ -17,18 +17,27 @@ Library Exposed-modules: Cafp.Messages + Cafp.Main.GenerateElmTypes + Cafp.Main.Server Build-depends: - base >= 4.9 && < 5, - elm-bridge >= 0.5 && < 0.6 + base >= 4.9 && < 5, + elm-bridge >= 0.5 && < 0.6, + scotty >= 0.11 && < 0.12, + stm >= 2.5 && < 2.6, + text >= 1.2 && < 1.3, + unordered-containers >= 0.2 && < 0.3 Executable cafp-generate-elm-types Hs-source-dirs: src Main-is: GenerateElmTypes.hs Default-language: Haskell2010 Ghc-options: -Wall + Build-depends: base, cafp - Build-depends: - base >= 4.9 && < 5, - cafp, - elm-bridge >= 0.5 && < 0.6 +Executable cafp-server + Hs-source-dirs: src + Main-is: Server.hs + Default-language: Haskell2010 + Ghc-options: -Wall + Build-depends: base, cafp diff --git a/server/lib/Cafp/Main/GenerateElmTypes.hs b/server/lib/Cafp/Main/GenerateElmTypes.hs new file mode 100644 index 0000000..0bd43c1 --- /dev/null +++ b/server/lib/Cafp/Main/GenerateElmTypes.hs @@ -0,0 +1,17 @@ +{-# OPTIONS_GHC -fno-warn-orphans #-} +{-# LANGUAGE TemplateHaskell #-} +module Cafp.Main.GenerateElmTypes + ( main + ) where + +import Cafp.Messages +import Elm.Derive +import Elm.Module +import Data.Proxy + +deriveBoth defaultOptions ''ServerMessage + +main :: IO () +main = putStrLn $ makeElmModule "Messages" + [ DefineElm (Proxy :: Proxy ServerMessage) + ] diff --git a/server/lib/Cafp/Main/Server.hs b/server/lib/Cafp/Main/Server.hs new file mode 100644 index 0000000..a2561a3 --- /dev/null +++ b/server/lib/Cafp/Main/Server.hs @@ -0,0 +1,29 @@ +{-# LANGUAGE OverloadedStrings #-} +module Cafp.Main.Server + ( main + ) where + +import Cafp.Messages +import Control.Concurrent.STM (STM, TVar, newTVar) +import Control.Monad (when) +import qualified Data.HashMap.Strict as HMS +import qualified Data.Text as T +import qualified Data.Text.Lazy as TL +import qualified Web.Scotty as Scotty + +type RoomId = T.Text + +data Server = Server + { serverRooms :: TVar (HMS.HashMap RoomId ()) + } + +newServer :: STM Server +newServer = Server <$> newTVar HMS.empty + +main :: IO () +main = Scotty.scotty 3000 $ do + Scotty.get "/rooms/:id" $ do + roomId <- Scotty.param "id" + when (T.length roomId < 6) $ + Scotty.raise "Room ID should be at least 6 characters" + Scotty.html $ "