Created
August 8, 2018 00:26
-
-
Save ronanyeah/95da2665ffff2b8c4f168339217225a4 to your computer and use it in GitHub Desktop.
Psc server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app :: | |
| Rf.Ref (Object Chat) -> | |
| Rf.Ref (Object Unit) -> | |
| Request -> | |
| Response -> | |
| Effect Unit | |
| app chats sockets req res = do | |
| setHeader res "Access-Control-Allow-Origin" "*" | |
| let outputStream = responseAsStream res | |
| let method = requestMethod req | |
| let url = requestURL req | |
| case method of | |
| "POST" -> do | |
| chatId <- randomString | |
| aId <- randomString | |
| bId <- randomString | |
| _ <- Rf.modify (insert chatId ({ a: aId | |
| , b: bId | |
| })) chats | |
| setStatusCode res 200 | |
| setHeader res "Content-Type" "application/json" | |
| _ <- writeString outputStream UTF8 ("{\"connId\": \"" <> bId <> "\", \"chatId\": \"" <> chatId <> "\"}") (pure unit) | |
| end outputStream (pure unit) | |
| "GET" -> do | |
| let id = drop 6 url | |
| cs <- Rf.read chats | |
| case lookup id cs of | |
| Just { a } -> do | |
| setStatusCode res 200 | |
| setHeader res "Content-Type" "application/json" | |
| _ <- writeString outputStream UTF8 ("{\"aId\": \"" <> a <> "\", \"chatId\": \"" <> id <> "\"}") (pure unit) | |
| end outputStream (pure unit) | |
| Nothing -> do | |
| setStatusCode res 400 | |
| end outputStream (pure unit) | |
| _ -> do | |
| setStatusCode res 400 | |
| end outputStream (pure unit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment