summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/src/Minilang/API.hs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud@pankzsoft.com>2025-11-04 21:52:15 +0100
committerArnaud Bailly <arnaud@pankzsoft.com>2025-11-04 21:52:15 +0100
commitdb4ecd7a93086585937b9b45970f4f4c4e1838a3 (patch)
treefead8d8b7b14b835efe57771ee8b3491f57d61d0 /lambda-calcul/haskell/src/Minilang/API.hs
parentdf879037d135c0b300735185d5fdc0857ed797d6 (diff)
downloadlambda-nantes-db4ecd7a93086585937b9b45970f4f4c4e1838a3.tar.gz
Register user and return random token
Diffstat (limited to 'lambda-calcul/haskell/src/Minilang/API.hs')
-rw-r--r--lambda-calcul/haskell/src/Minilang/API.hs24
1 files changed, 22 insertions, 2 deletions
diff --git a/lambda-calcul/haskell/src/Minilang/API.hs b/lambda-calcul/haskell/src/Minilang/API.hs
index 2b8743f..0ae0784 100644
--- a/lambda-calcul/haskell/src/Minilang/API.hs
+++ b/lambda-calcul/haskell/src/Minilang/API.hs
@@ -1,13 +1,23 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE DerivingStrategies #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeOperators #-}
+{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
+
+{-# HLINT ignore "Use newtype instead of data" #-}
module Minilang.API where
import Data.Aeson (FromJSON, ToJSON)
+import Data.Bifunctor (first)
+import Data.ByteString.Lazy (ByteString)
+import qualified Data.ByteString.Lazy as LBS
+import Data.Text (Text)
import Data.Time (UTCTime)
import GHC.Generics (Generic)
-import Servant (Get, JSON, (:<|>), (:>), Raw)
+import Servant (Get, JSON, Raw, ReqBody, StdMethod (POST), Verb, (:<|>), (:>))
+import System.Random (StdGen, uniformList)
data Score = Score
{ name :: String,
@@ -16,6 +26,16 @@ data Score = Score
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)
+newtype User = User {username :: Text}
+ deriving newtype (Eq, Show, ToJSON, FromJSON)
+
+data Token = Token {token :: Text}
+ deriving (Eq, Show, Generic, ToJSON, FromJSON)
+
+newToken :: StdGen -> (ByteString, StdGen)
+newToken = first LBS.pack . uniformList 16
+
type LambdaAPI =
- "api" :> "leaderboard" :> Get '[JSON] [Score]
+ "api" :> "register" :> ReqBody '[JSON] User :> Verb 'POST 201 '[JSON] Token
+ :<|> "api" :> "leaderboard" :> Get '[JSON] [Score]
:<|> Raw