Created
October 31, 2016 22:36
-
-
Save seanhess/375ee022761c402cfc46b076c36fb4bd to your computer and use it in GitHub Desktop.
Scotty Microservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
import Control.Monad.IO.Class (liftIO) | |
import Data.Aeson (ToJSON, Value) | |
import Data.Time.Clock (UTCTime, getCurrentTime) | |
import Data.Text.Lazy (Text) | |
-- import Data.Monoid (mconcat) | |
import GHC.Generics (Generic) | |
import Text.XML (Document, def) | |
import qualified Text.XML as XML | |
import Text.XML.Writer (XML, element) | |
import qualified Text.XML.Writer as XML | |
import Web.Scotty (scotty, get, post, text, json, jsonData, ScottyM, raw, setHeader) | |
import Web.Scotty.Internal.Types (ActionT) | |
-- { "heartbeat": Date } | |
data Heartbeat = Heartbeat | |
{ heartbeat :: UTCTime | |
} deriving (Show, Eq, Generic) | |
instance ToJSON Heartbeat | |
data Env = Env | |
main :: IO () | |
main = do | |
env <- loadEnvironment | |
scotty 3000 (routes env) | |
routes :: Env -> ScottyM () | |
routes env = do | |
get "/heartbeat" $ getHeartbeat | |
post "/report" $ (sendReport env) | |
loadEnvironment :: IO Env | |
loadEnvironment = return Env | |
getHeartbeat :: ActionT Text IO () | |
getHeartbeat = do | |
time <- liftIO $ getCurrentTime | |
let h = Heartbeat { heartbeat = time } | |
json h | |
sendReport :: Env -> ActionT Text IO () | |
sendReport env = do | |
val <- jsonData | |
liftIO $ print val | |
let doc = reportXML env val | |
body = XML.renderLBS def doc | |
-- TODO use Network.Wreq to send bytestring to server | |
-- res <- Wreq.post "http://aaa" body | |
-- case res of | |
-- Left err -> do | |
-- status status500 | |
-- text "Oh no bad error" | |
-- Right _ -> do | |
-- text "YAY you win" | |
setHeader "Content-Type" "text/xml" | |
raw $ body | |
reportXML :: Env -> Value -> Document | |
reportXML env val = do | |
XML.document "application" $ do | |
element "LoginInfo" (loginXML env) | |
element "ApplicationInfo" (valueToXML val) | |
valueToXML :: Value -> XML | |
valueToXML (Object hm) = List.map pairToXML $ HM.toList hm | |
valueToXML (String s) = XML.content s | |
valueToXML (Array a) = ... | |
-- valueToXML (String hm) = | |
pairToXML :: (Text, Value) -> XML | |
pairToXML (key, val) = | |
XML.element key (valueToXML val) | |
loginXML :: Env -> XML | |
loginXML = undefined | |
-- automatically convert Value -> XML | |
-- add some security information | |
-- <?xml> | |
-- <root> | |
-- <ApplicationInfo>...</ApplicationInfo> | |
-- <LoginInfo>...</LoginInfo> | |
{- | |
{ | |
"ApplicationID": "IDlogictestnick1", | |
"MobileNumber": "6783136412", | |
"FirstName": "Elaine", | |
"LastName": "Bennett", | |
"EmailAddress": "[email protected]", | |
"IPAddress": "64.238.114.61", | |
"DLNumber": "b5302137466406", | |
"DLNumberIssueState": "WI", | |
"DateOfBirth": "05/04/1974", | |
"Address1": "402 Dayton Street", | |
"Address2": null, | |
"City": "Blue River", | |
"State": "WI", | |
"Zip": "53518", | |
"Country": "US", | |
"MonthsAtAddress": "600", | |
"AlternateZip": null, | |
"HomePhone": "6085373660", | |
"SSN": "395840122", | |
"SSNIssueState": "WI", | |
"AccountABA": "075900575", | |
"AccountDDA": "2008088300", | |
"AccountType": "C", | |
"EmployerName": "Kwik", | |
"LengthOfEmployment": null, | |
"MonthlyIncome": "2958.00", | |
"PayrollType": "D", | |
"PayrollFrequency": "S", | |
"PayrollGarnishment": "N", | |
"HasBankruptcy": "N", | |
"RequestedLoanAmount": "300.00", | |
"RequestedEffectiveDate": "11/5/2015", | |
"RequestedDueDate": "12/16/2015", | |
"LeadType": null, | |
"LeadSource": null, | |
"ProductType": "REN", | |
"ECOACode": "A", | |
"PointOfOrigination": "R", | |
"PortfolioType": "S", | |
"SecuritizationType": "U", | |
"BlackBox": null, | |
"detail": true | |
} | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment