summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/src/Minilang/API.hs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud@pankzsoft.com>2025-11-04 22:49:12 +0100
committerArnaud Bailly <arnaud@pankzsoft.com>2025-11-04 22:49:12 +0100
commit8a2727a05e0b61d300bb46e8e913c9e60ebd6328 (patch)
tree5286eefedb79d4341a8157cc1af705e4f7da433a /lambda-calcul/haskell/src/Minilang/API.hs
parent958cd52392185fc31333c1b208948046d7a9e635 (diff)
downloadlambda-nantes-8a2727a05e0b61d300bb46e8e913c9e60ebd6328.tar.gz
Can retrieve first challenge when registered
Diffstat (limited to 'lambda-calcul/haskell/src/Minilang/API.hs')
-rw-r--r--lambda-calcul/haskell/src/Minilang/API.hs19
1 files changed, 16 insertions, 3 deletions
diff --git a/lambda-calcul/haskell/src/Minilang/API.hs b/lambda-calcul/haskell/src/Minilang/API.hs
index e9eef6a..c9cde53 100644
--- a/lambda-calcul/haskell/src/Minilang/API.hs
+++ b/lambda-calcul/haskell/src/Minilang/API.hs
@@ -16,7 +16,7 @@ import qualified Data.ByteString.Lazy as LBS
import Data.Text (Text)
import Data.Time (UTCTime)
import GHC.Generics (Generic)
-import Servant (Get, JSON, Raw, ReqBody, StdMethod (POST), Verb, (:<|>), (:>))
+import Servant (FromHttpApiData, Get, JSON, QueryParam, Raw, ReqBody, StdMethod (POST), ToHttpApiData, Verb, parseUrlPiece, toUrlPiece, (:<|>), (:>))
import System.Random (StdGen, uniformList)
data Score = Score
@@ -29,13 +29,26 @@ data Score = Score
newtype User = User {username :: Text}
deriving newtype (Eq, Show, ToJSON, FromJSON)
-data Token = Token {token :: Text}
- deriving (Eq, Show, Generic, ToJSON, FromJSON)
+newtype Token = Token {unToken :: Text}
+ deriving newtype (Eq, Ord, Show, ToJSON, FromJSON)
+
+instance ToHttpApiData Token where
+ toUrlPiece (Token t) = t
+
+instance FromHttpApiData Token where
+ parseUrlPiece t = Right (Token t)
newToken :: StdGen -> (ByteString, StdGen)
newToken = first LBS.pack . uniformList 16
+data Challenge = Challenge
+ { level :: Int,
+ expr :: Text
+ }
+ deriving (Eq, Show, Generic, ToJSON, FromJSON)
+
type LambdaAPI =
"api" :> "register" :> ReqBody '[JSON] User :> Verb 'POST 201 '[JSON] Token
:<|> "api" :> "leaderboard" :> Get '[JSON] [Score]
+ :<|> "api" :> "challenge" :> QueryParam "token" Token :> Get '[JSON] Challenge
:<|> Raw