Ich muss einen Web-Service erstellen, um zwischen verschiedenen Währungen mit dem scotty web framework in Haskell zu konvertieren.Scotty Web Service
Der Webservice sollte antworten, um Anfragen wie/convert/15? Zu = usd & von = eur zu erhalten.
Ich habe diesen Code so weit:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
functionxs :: a -> Int
functionxs a = 5
main = scotty 3000 $ do
get "/:word" $ do
name <- param "word"
html $ mconcat ["<h1>Hi, ", name, " what's up!</h1>"]
Wenn Sie also in der browswer ausführen: http://localhost:3000/Tony das Ergebnis ist: Hallo, Tony, was los ist! Das Problem ist, dass ich nicht weiß, wie man den Code ändert, um '/ convert/15? To = usd & from = eur.' als Anfrage und erhalten Sie die richtige Antwort.
Hoffentlich kann mir jeder helfen.
Vielen Dank im Voraus.
Edited mit Endlösung:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
import Data.Text.Lazy (Text, pack)
main = scotty 3000 $ do
get "/convert/:amount" $ do
money <- param "amount"
to <- param "to"
frm <- param "from"
html $ mconcat ["<h1>The result is: ", pack (show (convert
money to frm)), "</h1>"]
convert :: Double -> String -> String -> Double
convert a b c = if (b=="euro" && c=="usd")
then (a*1.091)
else if (b=="usd" && c=="euro")
then (a*0.915)
else 0
Sie haben /: convert /: amount, so dass Sie zwei Captures vor der Abfragezeichenfolge haben. Es muss "/ convert /: amount" sein, no **: ** vor convert, – sumo
Dies war definitiv die Lösung, vielen Dank! –