<commit_before>{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeFamilies          #-}
import Yesod
import Yesod.Form
import Data.Text (Text)

data HelloWorld = HelloWorld

mkYesod "HelloWorld" [parseRoutes|
/ HomeR GET
/greet GreetR POST
|]

instance Yesod HelloWorld

instance RenderMessage HelloWorld FormMessage where
    renderMessage _ _ = defaultFormMessage

getHomeR :: Handler Html
getHomeR = do
    (personFormWidget, enctype) <- generateFormPost personForm
    defaultLayout $ do
        setTitle "Hello!"
        toWidget [whamlet|
            <h1>Hello, World!
            <form method=post action=@{GreetR} enctype=#{enctype}>
                ^{personFormWidget}
                <input type="submit"/>
        |]

data Person = Person { personName :: Text } deriving Show
personForm :: Html -> MForm Handler (FormResult Person, Widget)
personForm = renderDivs $ Person
    <$> areq textField "Name" Nothing

postGreetR :: Handler Html
postGreetR = do
    ((result, widget), enctype) <- runFormPost personForm
    case result of
        FormSuccess person -> defaultLayout [whamlet|Greetings, #{personName person}.|]
        FormFailure _ -> defaultLayout [whamlet|Hmm, form failure.|]
        _ -> defaultLayout [whamlet|Hmm, something was wrong.|]

main :: IO()
main = warp 3000 HelloWorld<commit_msg>Listen port is read from environment<commit_after>{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeFamilies          #-}
import Prelude hiding (length)
import Yesod
import Yesod.Form
import Data.Text (Text, length, pack)
import Data.Text.Read (decimal)
import System.Environment
import System.IO (hPutStrLn, stderr)
import System.Exit

data HelloWorld = HelloWorld

mkYesod "HelloWorld" [parseRoutes|
/ HomeR GET
/greet GreetR POST
|]

instance Yesod HelloWorld

instance RenderMessage HelloWorld FormMessage where
    renderMessage _ _ = defaultFormMessage

getHomeR :: Handler Html
getHomeR = do
    (personFormWidget, enctype) <- generateFormPost personForm
    defaultLayout $ do
        setTitle "Hello!"
        toWidget [whamlet|
            <h1>Hello, World!
            <form method=post action=@{GreetR} enctype=#{enctype}>
                ^{personFormWidget}
                <input type="submit"/>
        |]

data Person = Person { personName :: Text } deriving Show
personForm :: Html -> MForm Handler (FormResult Person, Widget)
personForm = renderDivs $ Person
    <$> areq textField "Name" Nothing

postGreetR :: Handler Html
postGreetR = do
    ((result, widget), enctype) <- runFormPost personForm
    case result of
        FormSuccess person -> defaultLayout [whamlet|Greetings, #{personName person}.|]
        FormFailure _ -> defaultLayout [whamlet|Hmm, form failure.|]
        _ -> defaultLayout [whamlet|Hmm, something was wrong.|]

readPort :: IO (Maybe Int)
readPort = do
    portStr <- fmap pack $ getEnv "PORT"
    let eitherPort = decimal portStr
    case eitherPort of
        Left error -> do
            hPutStrLn stderr ("Could not get PORT: " ++ error)
            return Nothing
        Right (port, remainder) -> do
            if (length remainder) > 0 then do
                hPutStrLn stderr "Port is not an integer"
                return Nothing
            else
                return $ Just port

main :: IO()
main = do
        maybePort <- readPort
        case maybePort of
            Nothing -> exitWith $ ExitFailure 1
            Just port -> do
                putStrLn $ "Listening on port " ++ show port
                warp port HelloWorld
{'fraction_non_alphanumeric': 0.09454061251664447, 'fraction_numerical': 0.0021304926764314247, 'mean_word_length': 2.707798617966436, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6045584', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1088, 'n_words': 331}