Routes.hs 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
module API.Routes where

import Web.Spock.Safe
import qualified Hasql as H
import qualified Hasql.Postgres as HP
import qualified Database.Redis as R

import API.Controllers.Login
import API.Controllers.Register
Allele Dev's avatar
Allele Dev committed
11 12
import API.Database.Common
import API.Logging (mkLog, Log)
13 14 15 16 17 18 19 20 21

--------------------------------------------------------------------------------
main :: IO ()
main = do
  let psqlConf = HP.ParamSettings "localhost" 5432 "strangeloop" "cat" "strangeloop"
  let poolConf = maybe (error "Bad pool settings") id $ H.poolSettings 6 30
  sqlC :: Pool
    <- H.acquirePool psqlConf poolConf
  redisC <- R.connect R.defaultConnectInfo
Allele Dev's avatar
Allele Dev committed
22
  logger <- mkLog
23
  runSpock 3000 $ spockT id $
Allele Dev's avatar
Allele Dev committed
24
    do core sqlC redisC logger
25 26 27 28
       players
       games
       runs

Allele Dev's avatar
Allele Dev committed
29 30 31 32
core :: Pool -> R.Connection -> Log -> SpockT IO ()
core sqlC redisC logger =
  do post "register" (registerUser sqlC logger)
     post "login" (login sqlC redisC logger)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

players :: SpockT IO ()
players =
  do get ("players" <//> var) $ \x -> text x
     get "players" $ text ""
     put ("players" <//> var <//> "profile") $ \x -> text x
     delete ("players" <//> var) $ \x -> text x

games :: SpockT IO ()
games =
  do get "games" $ text ""
     get ("games" <//> var) $ \x -> text x

runs :: SpockT IO ()
runs =
  do get "runs" (text "")
     post ("games" <//> var <//> "runs") $ \x -> text x
     get ("runs" <//> var) $ \x -> text x

--------------------------------------------------------------------------------