-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.hs
71 lines (52 loc) · 2.13 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Main where
import Data.Text (Text, pack)
import Data.Monoid ((<>))
import qualified Hilt
import qualified Hilt.Server as Server
import qualified Hilt.Channel as Channel
import qualified Hilt.Logger as Logger
import qualified Hilt.SocketServer as Websocket
showt :: Show a => a -> Text
showt t = pack $ show t
{-
To run this sample locally:
```
git clone [email protected]:supermario/hilt.git
cd hilt && stack build && stack exec hilt-example
# In another terminal window
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" localhost:8081/ws
```
The Hilt.Postgres service is not demonstrated as it needs an existing Postgres DB in the DATABASE_URL ENV var,
and table to query. The addition is simple however if you want it:
-- Add the db service next to the other services
db <- Hilt.Postgres.load
-- Use the handle within your program
Hilt.Postgres.query db "SELECT * FROM myTable WHERE x = ?" ["1"]
-}
main :: IO ()
main = Hilt.manage $ do
logger <- Logger.load
chan <- Channel.load
let onJoined :: Websocket.OnJoined
onJoined clientId clientCount = do
Logger.debug logger $ showt clientId <> " joined, " <> showt clientCount <> " connected."
pure $ Just "Hello client!"
onReceive :: Websocket.OnReceive
onReceive clientId text = do
Logger.debug logger $ showt clientId <> " said " <> showt text
Channel.write chan text
websocket <- Websocket.load onJoined onReceive
-- Now we can write our business logic using our services
Hilt.program $ do
Logger.debug logger "Starting up!"
-- Log all messages received, then broadcast back to all clients
let workHandler text = do
Logger.debug logger $ "[worker] got " <> text
Websocket.broadcast websocket text
-- Run our worker and our websocket server
Channel.worker chan workHandler
Server.runWebsocket websocket Server.defaultMiddlewares
-- Now we can pass services off to some other areas of our app
-- someMoreLogic logger chan
-- Or we can just use them here
Channel.write chan "Hello world!"