2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2014-11-25 19:42:52 +01:00
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
2016-07-05 17:34:30 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Monad.IO.Class
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Proxy
|
|
|
|
import GHC.Generics
|
|
|
|
import Network.Wai.Handler.Warp (run)
|
|
|
|
import Servant
|
|
|
|
import Servant.JS
|
|
|
|
import qualified Servant.JS as SJS
|
|
|
|
import qualified Servant.JS.Angular as NG
|
|
|
|
import System.FilePath
|
2014-11-25 19:42:52 +01:00
|
|
|
|
|
|
|
-- * A simple Counter data type
|
|
|
|
newtype Counter = Counter { value :: Int }
|
|
|
|
deriving (Generic, Show, Num)
|
|
|
|
|
|
|
|
instance ToJSON Counter
|
|
|
|
|
|
|
|
-- * Shared counter operations
|
|
|
|
|
|
|
|
-- Creating a counter that starts from 0
|
|
|
|
newCounter :: IO (TVar Counter)
|
|
|
|
newCounter = newTVarIO 0
|
|
|
|
|
|
|
|
-- Increasing the counter by 1
|
|
|
|
counterPlusOne :: MonadIO m => TVar Counter -> m Counter
|
|
|
|
counterPlusOne counter = liftIO . atomically $ do
|
|
|
|
oldValue <- readTVar counter
|
|
|
|
let newValue = oldValue + 1
|
|
|
|
writeTVar counter newValue
|
|
|
|
return newValue
|
|
|
|
|
|
|
|
currentValue :: MonadIO m => TVar Counter -> m Counter
|
|
|
|
currentValue counter = liftIO $ readTVarIO counter
|
|
|
|
|
|
|
|
-- * Our API type
|
2015-07-17 23:36:38 +02:00
|
|
|
type TestApi = "counter" :> Post '[JSON] Counter -- endpoint for increasing the counter
|
|
|
|
:<|> "counter" :> Get '[JSON] Counter -- endpoint to get the current value
|
2014-11-25 19:42:52 +01:00
|
|
|
|
2015-07-22 19:23:31 +02:00
|
|
|
type TestApi' = TestApi
|
2015-08-17 23:56:29 +02:00
|
|
|
:<|> Raw -- used for serving static files
|
2015-07-22 19:23:31 +02:00
|
|
|
|
|
|
|
-- this proxy only targets the proper endpoints of our API,
|
|
|
|
-- not the static file serving bit
|
2014-11-25 19:42:52 +01:00
|
|
|
testApi :: Proxy TestApi
|
|
|
|
testApi = Proxy
|
|
|
|
|
2015-07-22 19:23:31 +02:00
|
|
|
-- this proxy targets everything
|
|
|
|
testApi' :: Proxy TestApi'
|
|
|
|
testApi' = Proxy
|
|
|
|
|
2014-11-25 19:42:52 +01:00
|
|
|
-- * Server-side handler
|
|
|
|
|
|
|
|
-- where our static files reside
|
|
|
|
www :: FilePath
|
|
|
|
www = "examples/www"
|
|
|
|
|
2015-07-22 19:23:31 +02:00
|
|
|
-- defining handlers of our endpoints
|
2014-11-25 19:42:52 +01:00
|
|
|
server :: TVar Counter -> Server TestApi
|
|
|
|
server counter = counterPlusOne counter -- (+1) on the TVar
|
|
|
|
:<|> currentValue counter -- read the TVar
|
2015-07-22 19:23:31 +02:00
|
|
|
|
|
|
|
-- the whole server, including static file serving
|
|
|
|
server' :: TVar Counter -> Server TestApi'
|
|
|
|
server' counter = server counter
|
|
|
|
:<|> serveDirectory www -- serve static files
|
2014-11-25 19:42:52 +01:00
|
|
|
|
|
|
|
runServer :: TVar Counter -- ^ shared variable for the counter
|
|
|
|
-> Int -- ^ port the server should listen on
|
|
|
|
-> IO ()
|
2015-07-22 19:23:31 +02:00
|
|
|
runServer var port = run port (serve testApi' $ server' var)
|
|
|
|
|
|
|
|
writeServiceJS :: FilePath -> IO ()
|
|
|
|
writeServiceJS fp =
|
|
|
|
writeJSForAPI testApi
|
|
|
|
(angularServiceWith (NG.defAngularOptions { NG.serviceName = "counterSvc" })
|
|
|
|
(defCommonGeneratorOptions { SJS.moduleName = "counterApp" })
|
|
|
|
)
|
|
|
|
fp
|
2015-08-17 23:56:29 +02:00
|
|
|
|
2014-11-25 19:42:52 +01:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
-- write the JS code to www/api.js at startup
|
2015-07-22 19:23:31 +02:00
|
|
|
writeJSForAPI testApi jquery (www </> "jquery" </> "api.js")
|
|
|
|
|
|
|
|
writeJSForAPI testApi vanillaJS (www </> "vanilla" </> "api.js")
|
|
|
|
|
|
|
|
writeJSForAPI testApi (angular defAngularOptions) (www </> "angular" </> "api.js")
|
|
|
|
|
2016-07-05 17:34:30 +02:00
|
|
|
writeJSForAPI testApi (axios defAxiosOptions) (www </> "axios" </> "api.js")
|
2015-07-27 16:34:00 +02:00
|
|
|
|
2015-07-22 19:23:31 +02:00
|
|
|
writeServiceJS (www </> "angular" </> "api.service.js")
|
2014-11-25 19:42:52 +01:00
|
|
|
|
|
|
|
-- setup a shared counter
|
|
|
|
cnt <- newCounter
|
|
|
|
|
|
|
|
-- listen to requests on port 8080
|
|
|
|
runServer cnt 8080
|