git.iko.soy/typeable/servant
on master help sign in
tutorial: make Javascript.lhs compile
commit487746f9e0c62647398b3ce48fc181286afd4338 authorSönke Hahn <soenkehahn@gmail.com> dateThu 28 Jan 2016, 18:44 (11 years ago) parent7445d56
3 files changed+11−11
doc/tutorial/Javascript.lhs+9−8 doc/tutorial/tinc.yaml−2 doc/tutorial/tutorial.cabal+2−1
doc/tutorial/Javascript.lhs+9−8
@@ -30,13 +30,14 @@ module Javascript where
3030import Control.Monad.IO.Class
3131import Data.Aeson
3232import Data.Proxy
33−import Data.Text (Text)
33+import Data.Text as T (Text)
34+import Data.Text.IO as T (writeFile, readFile)
3435import qualified Data.Text as T
3536import GHC.Generics
3637import Language.Javascript.JQuery
3738import Network.Wai
3839import Servant
39−import Servant.JQuery
40+import Servant.JS
4041import System.Random
4142```
4243
@@ -133,7 +134,7 @@ server' = server
133134 :<|> serveDirectory "tutorial/t9"
134135
135136app :: Application
136−app = serve api' server'
137+app = serve api' EmptyConfig server'
137138```
138139
139140Why two different API types, proxies and servers though? Simply because we don't want to generate javascript functions for the `Raw` part of our API type, so we need a `Proxy` for our API type `API'` without its `Raw` endpoint.
@@ -141,8 +142,8 @@ Why two different API types, proxies and servers though? Simply because we don't
141142Very similarly to how one can derive haskell functions, we can derive the javascript with just a simple function call to `jsForAPI` from `Servant.JQuery`.
142143
143144``` haskell
144−apiJS :: String
145−apiJS = jsForAPI api
145+apiJS :: Text
146+apiJS = jsForAPI api vanillaJS
146147```
147148
148149This `String` contains 2 Javascript functions:
@@ -175,9 +176,9 @@ Right before starting up our server, we will need to write this `String` to a fi
175176``` haskell
176177writeJSFiles :: IO ()
177178writeJSFiles = do
178− writeFile "getting-started/gs9/api.js" apiJS
179− jq <- readFile =<< Language.Javascript.JQuery.file
180− writeFile "getting-started/gs9/jq.js" jq
179+ T.writeFile "getting-started/gs9/api.js" apiJS
180+ jq <- T.readFile =<< Language.Javascript.JQuery.file
181+ T.writeFile "getting-started/gs9/jq.js" jq
181182```
182183
183184And we're good to go. Start the server with `dist/build/tutorial/tutorial 9` and go to `http://localhost:8081/`. Start typing in the name of one of the authors in our database or part of a book title, and check out how long it takes to approximate &pi; using the method mentioned above.
doc/tutorial/tinc.yaml−2
@@ -7,8 +7,6 @@ dependencies:
77 path: ../../servant-client
88 - name: servant-js
99 path: ../../servant-js
10− - name: servant-lucid
11− path: ../../servant-lucid
1210 - name: servant-docs
1311 path: ../../servant-docs
1412 - name: servant-foreign
doc/tutorial/tutorial.cabal+2−1
@@ -17,7 +17,7 @@ library
1717 exposed-modules: ApiType
1818 , Client
1919 , Docs
20− -- , Javascript
20+ , Javascript
2121 , Server
2222 -- other-modules:
2323 -- other-extensions:
@@ -32,6 +32,7 @@ library
3232 , servant-server == 0.5.*
3333 , servant-client == 0.5.*
3434 , servant-docs == 0.5.*
35+ , servant-js == 0.5.*
3536 , warp
3637 , http-media
3738 , lucid