Skip to main content
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

How to use MySLQ Using MySQL with Wai and Warp

Post Reopened by Phrancis, Mathieu Guindon, Comintern, Sᴀᴍ Onᴇᴌᴀ, Ludisposed
deleted 249 characters in body
Source Link
599644
  • 119
  • 4

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
    c <- connect defaultConnectInfo 
        { connectHost = "db"
        , connectUser = "root"
        , connectPassword = "secret"
        , connectDatabase = "test" }
    x <- (query_ c "SELECT 1") :: IO [Only Int]
    run 8080 $ app $ pool

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
    c <- connect defaultConnectInfo 
        { connectHost = "db"
        , connectUser = "root"
        , connectPassword = "secret"
        , connectDatabase = "test" }
    x <- (query_ c "SELECT 1") :: IO [Only Int]
    run 8080 $ app $ pool

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
fix typo
Source Link

I was wondering if below is the correct conventationconvention 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
    c <- connect defaultConnectInfo 
        { connectHost = "db"
        , connectUser = "root"
        , connectPassword = "secret"
        , connectDatabase = "test" }
    x <- (query_ c "SELECT 1") :: IO [Only Int]
    run 8080 $ app $ pool

I was wondering if below is the correct conventation 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
    c <- connect defaultConnectInfo 
        { connectHost = "db"
        , connectUser = "root"
        , connectPassword = "secret"
        , connectDatabase = "test" }
    x <- (query_ c "SELECT 1") :: IO [Only Int]
    run 8080 $ app $ pool

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
    c <- connect defaultConnectInfo 
        { connectHost = "db"
        , connectUser = "root"
        , connectPassword = "secret"
        , connectDatabase = "test" }
    x <- (query_ c "SELECT 1") :: IO [Only Int]
    run 8080 $ app $ pool
added 67 characters in body
Source Link
599644
  • 119
  • 4
Loading
Post Closed as "Not suitable for this site" by Phrancis, Stephen Rauch, Jamal
Source Link
599644
  • 119
  • 4
Loading