2016-04-12 4 views
0

Wie kann ich Shakespeare (von jaod) für Servant Webservices APIs verwenden?Serving mit Yesod Shakespeare (Hamlet, Julius, Lucius)

Ich versuche:

type TestAPI 
    = "tests" :> Get '[JSON] [Test] 
    :<|> "Test.html" :> Get '[HTML] Html 

serverTestAPI :: ServerT TestAPI AppM 
serverTestAPI = tests 
      :<|> test 
      :<|> testHtml 

tests :: AppM [Test] 
tests = do return [ Test 1 "Test 1" 
        , Test 2 "Test 2" 
        ] 

testHtml = [hamlet| 
       $doctype 5 
       ......... 
       |] 

Aber ich bekomme Fehler!

+0

Welche Fehler angezeigt wird? – KittMedia

+0

Wenn man sich die Dokumentation von 'Shakespeare' anschaut, sieht es so aus, als würde das' Hamlet' QQ Dinge vom Typ 'HtmlUrl' erzeugen, aber' Shamlet' produziert Dinge vom Typ 'Html'. Wird 'shamlet' anstelle von' hamlet' verwendet? – kosmikus

+0

Fehler im Display: – QSpider

Antwort

1

Wie @Carsten darauf hinweist, ist in diesem Fall, was Sie wollen, shamlet. Der Schlüssel ist die Implementierung der richtigen Instanzen der Klassenklasse ToMarkup. Ich empfehle Ihnen, dieses excellent introduction auf Shakespeare-Vorlagen zu lesen. Ein Arbeitsbeispiel:

data Person = Person 
    { firstName :: String 
    , lastName :: String 
    } deriving Generic 

instance ToJSON Person 

type PersonAPI = "persons" :> Get '[JSON, HTML] [Person] 

people :: [Person] 
people = 
    [ Person "Isaac" "Newton" 
    , Person "Albert" "Einstein" 
    ] 

instance ToMarkup Person where 
    toMarkup person = showPerson person 

    -- this isn't properly implemented 
    preEscapedToMarkup p = showPerson p 

-- HTML serialization of a list of persons 
instance ToMarkup [Person] where 
    toMarkup persons = showPersons persons 

    preEscapedToMarkup p = showPersons p 

showPerson :: Person -> Html 
showPerson p = [shamlet| 
<body> 
    <p>This is my page. 
    <h1>#{firstName p} 
|] 

showPersons :: [Person] -> Html 
showPersons p = [shamlet| 
<body> 
    <p>This is my page. 
    $forall person <- p 
     <h1>#{firstName person} 
|] 
1

Hier ist ein kleines vollständiges Beispiel, das funktioniert für mich:

{-# LANGUAGE DataKinds, PolyKinds, TypeOperators, DeriveGeneriC#-} 
{-# LANGUAGE QuasiQuotes #-} 
module Main where 

import Data.Aeson 
import Data.Proxy 
import GHC.Generics 
import Network.Wai.Handler.Warp 
import Servant.API 
import Servant.HTML.Blaze 
import Servant.Server 
import Text.Blaze.Html 
import Text.Hamlet 

data Test = Test Int String 
    deriving (Generic) 

instance ToJSON Test 

type TestAPI 
    = "tests" :> Get '[JSON] [Test] 
    :<|> "Test.html" :> Get '[HTML] Html 

main :: IO() 
main = run 8080 (serve (Proxy :: Proxy TestAPI) serverTestAPI) 

serverTestAPI :: Server TestAPI 
serverTestAPI = tests :<|> testHtml 

tests = return [ Test 1 "Test 1" 
       , Test 2 "Test 2" 
       ] 

testHtml = return [shamlet| 
    $doctype 5 
    <html> 
    <head> 
     <title>This is a title 
    <body> 
     <p>This is text 
    |] 
0

Vielen Dank für Ihre Hilfe!
wie folgt umgesetzt:

<code>type TestAPI 
    = "tests" :> Get '[JSON] [Test] 
    :<|> "test" :> Get '[JSON] Test 
    :<|> "TestHTML.html" :> Get '[HTML] Page_TestHTML 

serverTestAPI :: ServerT TestAPI AppM 
serverTestAPI = tests 
      :<|> test 
      :<|> testHtml 

data Page_TestHTML = Page_TestHTML 

instance ToMarkup Page_TestHTML where 
    toMarkup Page_TestHTML = builderHtml 

testHtml = return Page_TestHTML 

builderHtml = [shamlet| 
       $doctype 5 
       <html> 
        <head> 
         <title>Greeting2 
       <body> 
        <h2> Hello world HTML Qqqqq |]</code>