tutorial: make Javascript.lhs compile
This commit is contained in:
parent
7445d56c68
commit
487746f9e0
3 changed files with 11 additions and 11 deletions
|
@ -30,13 +30,14 @@ module Javascript where
|
||||||
import Control.Monad.IO.Class
|
import Control.Monad.IO.Class
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
import Data.Text (Text)
|
import Data.Text as T (Text)
|
||||||
|
import Data.Text.IO as T (writeFile, readFile)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
import Language.Javascript.JQuery
|
import Language.Javascript.JQuery
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
import Servant
|
import Servant
|
||||||
import Servant.JQuery
|
import Servant.JS
|
||||||
import System.Random
|
import System.Random
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -133,7 +134,7 @@ server' = server
|
||||||
:<|> serveDirectory "tutorial/t9"
|
:<|> serveDirectory "tutorial/t9"
|
||||||
|
|
||||||
app :: Application
|
app :: Application
|
||||||
app = serve api' server'
|
app = serve api' EmptyConfig server'
|
||||||
```
|
```
|
||||||
|
|
||||||
Why 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.
|
Why 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
|
||||||
Very similarly to how one can derive haskell functions, we can derive the javascript with just a simple function call to `jsForAPI` from `Servant.JQuery`.
|
Very similarly to how one can derive haskell functions, we can derive the javascript with just a simple function call to `jsForAPI` from `Servant.JQuery`.
|
||||||
|
|
||||||
``` haskell
|
``` haskell
|
||||||
apiJS :: String
|
apiJS :: Text
|
||||||
apiJS = jsForAPI api
|
apiJS = jsForAPI api vanillaJS
|
||||||
```
|
```
|
||||||
|
|
||||||
This `String` contains 2 Javascript functions:
|
This `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
|
||||||
``` haskell
|
``` haskell
|
||||||
writeJSFiles :: IO ()
|
writeJSFiles :: IO ()
|
||||||
writeJSFiles = do
|
writeJSFiles = do
|
||||||
writeFile "getting-started/gs9/api.js" apiJS
|
T.writeFile "getting-started/gs9/api.js" apiJS
|
||||||
jq <- readFile =<< Language.Javascript.JQuery.file
|
jq <- T.readFile =<< Language.Javascript.JQuery.file
|
||||||
writeFile "getting-started/gs9/jq.js" jq
|
T.writeFile "getting-started/gs9/jq.js" jq
|
||||||
```
|
```
|
||||||
|
|
||||||
And 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 π using the method mentioned above.
|
And 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 π using the method mentioned above.
|
||||||
|
|
|
@ -7,8 +7,6 @@ dependencies:
|
||||||
path: ../../servant-client
|
path: ../../servant-client
|
||||||
- name: servant-js
|
- name: servant-js
|
||||||
path: ../../servant-js
|
path: ../../servant-js
|
||||||
- name: servant-lucid
|
|
||||||
path: ../../servant-lucid
|
|
||||||
- name: servant-docs
|
- name: servant-docs
|
||||||
path: ../../servant-docs
|
path: ../../servant-docs
|
||||||
- name: servant-foreign
|
- name: servant-foreign
|
||||||
|
|
|
@ -17,7 +17,7 @@ library
|
||||||
exposed-modules: ApiType
|
exposed-modules: ApiType
|
||||||
, Client
|
, Client
|
||||||
, Docs
|
, Docs
|
||||||
-- , Javascript
|
, Javascript
|
||||||
, Server
|
, Server
|
||||||
-- other-modules:
|
-- other-modules:
|
||||||
-- other-extensions:
|
-- other-extensions:
|
||||||
|
@ -32,6 +32,7 @@ library
|
||||||
, servant-server == 0.5.*
|
, servant-server == 0.5.*
|
||||||
, servant-client == 0.5.*
|
, servant-client == 0.5.*
|
||||||
, servant-docs == 0.5.*
|
, servant-docs == 0.5.*
|
||||||
|
, servant-js == 0.5.*
|
||||||
, warp
|
, warp
|
||||||
, http-media
|
, http-media
|
||||||
, lucid
|
, lucid
|
||||||
|
|
Loading…
Reference in a new issue