1
\$\begingroup\$

I was wondering if below is the correct convention for combining Wai with a Database pool.

What I basically do is, create a pool, partially apply a function of type Pool -> Application and use it to pass it to Warp's run. Does it look ok, or shall I refactor it?

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)
import Database.MySQL.Simple
import Data.Pool (Pool, createPool, withResource)
newConn = connect defaultConnectInfo 
 { connectHost = "db"
 , connectUser = "root"
 , connectPassword = "secret"
 , connectDatabase = "test" }
getPool = createPool newConn close 1 10 5
app :: Pool Connection -> Application
app pool _ respond = do
 withResource pool $ \c -> query_ c "SELECT 1" :: IO [Only Int]
 respond $ responseLBS
 status200
 [("Content-Type", "text/plain")]
 "Hello, Web!"
main :: IO ()
main = do
 putStrLn $ "http://localhost:8080/"
 pool <- getPool
 run 8080 $ app $ pool
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 25, 2018 at 23:28
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Review. Asking for advice on code yet to be written or implemented is off-topic for this site. See What topics can I ask about? for reference. Once you have written working code, you're welcome to ask a new question here and we can then help you improve it! \$\endgroup\$ Commented Jul 26, 2018 at 0:20
  • \$\begingroup\$ Updated with working, written code. \$\endgroup\$ Commented Jul 26, 2018 at 23:15
  • \$\begingroup\$ OK good, I've flagged your question to be reopened. \$\endgroup\$ Commented Jul 26, 2018 at 23:36

1 Answer 1

1
\$\begingroup\$

I'm afraid there isn't much to remove. The $ after putStrLn and between app and pool are superfluous. You could write the last two lines run 8080 . app <$> getPool. I'd inline getPool. You may be interested in https://github.com/alevy/simple/blob/master/simple/src/Web/Simple.hs#L134 for boilerplate reduction.

answered Aug 7, 2018 at 8:04
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.