Commit 56f7ea07 authored by Oscar Leijendekker's avatar Oscar Leijendekker

Implementing for ch.5, set up a db and connected to it.

parent 6d6d8fe1
Pipeline #92307260 failed with stage
in 11 minutes and 56 seconds
FROM postgres:latest
ENV POSTGRES_USER "Haskell-student"
ENV POSTGRES_PASSWORD "Why-are-you-putting-credentials-in-code?-You-absolute-potato!"
......@@ -25,6 +25,7 @@ dependencies:
- wai >= 3.2.2.1 && < 4
- http-types >= 0.12.3 && < 0.13
- utf8-string >= 1.0.1.1 && < 2
- hasql >= 1.3.0.6 && < 2
library:
source-dirs: src
......
......@@ -7,10 +7,14 @@ module Lib
import Network.Wai.Handler.Warp (run)
import Network.Wai (Request, Response, ResponseReceived, responseLBS)
import Network.HTTP.Types.Status (status200)
import qualified Data.ByteString.Lazy.UTF8 as UTF8 (fromString)
import qualified Data.ByteString.Lazy.UTF8 as UTF8 (fromString, toString)
import qualified Data.ByteString.UTF8 as StrictUTF8 (fromString, toString)
import qualified Hasql.Connection
someFunc :: IO ()
someFunc = run 8080 requestHandler
someFunc = do
connectToDB
run 8080 requestHandler
requestHandler :: Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
requestHandler request respond =
......@@ -19,9 +23,28 @@ requestHandler request respond =
response = responseLBS status200 [] htmlPage
in
do
putStrLn "Received an HTTP request!"
respond response
connectToDB :: IO ()
connectToDB =
let
connectionSettings :: Hasql.Connection.Settings
connectionSettings =
Hasql.Connection.settings
"localhost"
(toEnum 5432)
"Haskell-student"
"Why-are-you-putting-credentials-in-code?-You-absolute-potato!"
""
in do
connectionResult <- Hasql.Connection.acquire connectionSettings
case connectionResult of
Left (Just errMsg) -> error $ StrictUTF8.toString errMsg
Left Nothing -> error "Unspecified connection error"
Right connection ->
Hasql.Connection.withLibPQConnection connection (\_ -> putStrLn "Acquired connection!")
data TaskStatus = Done | NotDone
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment