on master help sign in
tutorial: make Javascript.lhs compile
487746fSönke Hahn
doc/tutorial/Javascript.lhs+9−8
@@ -30,13 +30,14 @@ module Javascript where
30import Control.Monad.IO.Class
30import Control.Monad.IO.Class
31import Data.Aeson
31import Data.Aeson
32import Data.Proxy
32import Data.Proxy
33−import Data.Text (Text)
33+import Data.Text as T (Text)
34+import Data.Text.IO as T (writeFile, readFile)
34import qualified Data.Text as T
35import qualified Data.Text as T
35import GHC.Generics
36import GHC.Generics
36import Language.Javascript.JQuery
37import Language.Javascript.JQuery
37import Network.Wai
38import Network.Wai
38import Servant
39import Servant
39−import Servant.JQuery
40+import Servant.JS
40import System.Random
41import System.Random
41```
42```
42
43
@@ -133,7 +134,7 @@ server' = server
133 :<|> serveDirectory "tutorial/t9"
134 :<|> serveDirectory "tutorial/t9"
134
135
135app :: Application
136app :: Application
136−app = serve api' server'
137+app = serve api' EmptyConfig server'
137```
138```
138
139
139Why 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.
140Why 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
141Very similarly to how one can derive haskell functions, we can derive the javascript with just a simple function call to `jsForAPI` from `Servant.JQuery`.
142Very similarly to how one can derive haskell functions, we can derive the javascript with just a simple function call to `jsForAPI` from `Servant.JQuery`.
142
143
143``` haskell
144``` haskell
144−apiJS :: String
145+apiJS :: Text
145−apiJS = jsForAPI api
146+apiJS = jsForAPI api vanillaJS
146```
147```
147
148
148This `String` contains 2 Javascript functions:
149This `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
175``` haskell
176``` haskell
176writeJSFiles :: IO ()
177writeJSFiles :: IO ()
177writeJSFiles = do
178writeJSFiles = do
178− writeFile "getting-started/gs9/api.js" apiJS
179+ T.writeFile "getting-started/gs9/api.js" apiJS
179− jq <- readFile =<< Language.Javascript.JQuery.file
180+ jq <- T.readFile =<< Language.Javascript.JQuery.file
180− writeFile "getting-started/gs9/jq.js" jq
181+ T.writeFile "getting-started/gs9/jq.js" jq
181```
182```
182
183
183And 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.
184And 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:
7 path: ../../servant-client
7 path: ../../servant-client
8 - name: servant-js
8 - name: servant-js
9 path: ../../servant-js
9 path: ../../servant-js
10− - name: servant-lucid
11− path: ../../servant-lucid
12 - name: servant-docs
10 - name: servant-docs
13 path: ../../servant-docs
11 path: ../../servant-docs
14 - name: servant-foreign
12 - name: servant-foreign
doc/tutorial/tutorial.cabal+2−1
@@ -17,7 +17,7 @@ library
17 exposed-modules: ApiType
17 exposed-modules: ApiType
18 , Client
18 , Client
19 , Docs
19 , Docs
20− -- , Javascript
20+ , Javascript
21 , Server
21 , Server
22 -- other-modules:
22 -- other-modules:
23 -- other-extensions:
23 -- other-extensions:
@@ -32,6 +32,7 @@ library
32 , servant-server == 0.5.*
32 , servant-server == 0.5.*
33 , servant-client == 0.5.*
33 , servant-client == 0.5.*
34 , servant-docs == 0.5.*
34 , servant-docs == 0.5.*
35+ , servant-js == 0.5.*
35 , warp
36 , warp
36 , http-media
37 , http-media
37 , lucid
38 , lucid