Skip to content

Commit

Permalink
Merge pull request #72 from anton-k/hw-pragma
Browse files Browse the repository at this point in the history
Update hello world examples
  • Loading branch information
anton-k authored Nov 10, 2023
2 parents 093c74d + ebd2523 commit c284052
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ With the library [`mig-server`](https://hackage.haskell.org/package/mig-server)
a simple server with two routes:

```haskell
{-# Language OverloadedStrings #-}

import Mig.Json.IO

-- | Starts server on port 8085.
Expand All @@ -34,7 +36,7 @@ server =
hello :: Get (Resp Text)
hello = pure $ ok "Hello World"

-- | The handler definition as a function with a query parameter to ask for user name
-- | The handler definition as a function with a query parameter to ask for the user name
bye :: Query "user" Text -> Get (Resp Text)
bye (Query name) = pure $ ok ("Goodbye " <> name)
```
Expand Down
14 changes: 11 additions & 3 deletions docs/src/00-foreword.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ The main features of the mig library are:

Example of hello world server:


```haskell
{-# Language OverloadedStrings #-}

import Mig.Json.IO

-- | Starts server on port 8085.
Expand All @@ -29,13 +30,20 @@ main = runServer 8085 server

-- | The server definition
server :: Server IO
server = "api/v1/hello" /. hello
server =
"api/v1" /.
[ "hello" /. hello
, "bye" /. bye
]

-- | The handler definition as a function
hello :: Get (Resp Text)
hello = pure $ ok "Hello World"
```

-- | The handler with a query parameter to ask for the user name
bye :: Query "user" Text -> Get (Resp Text)
bye (Query name) = pure $ ok ("Goodbye " <> name)
```

## How to install library

Expand Down
41 changes: 19 additions & 22 deletions examples/mig-example-apps/HelloWorld/Main.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
{-| Most basic Hello world server.
It has only one route which outputs the greeting to the user
-}
module Main (
main,
) where
{-# LANGUAGE OverloadedStrings #-}

import Mig
-- \| Most basic Hello world server.
-- It has only two routes which output the text as JSON to the user
import Mig.Json.IO

{-| We can render the server and run it on port 8085.
It uses wai and warp.
-}
-- | Starts server on port 8085.
main :: IO ()
main = do
putStrLn ("The hello world server listens on port: " <> show port)
runServer port server
where
port = 8085
main = runServer 8085 (withSwagger def server)

{-| Init simple hello world server which
replies on a single route
-}
-- | The server definition with two routes
server :: Server IO
server = "api/v1/hello" /. hello
server =
"api/v1"
/. [ "hello" /. hello
, "bye" /. bye
]

-- | Handler takes no inputs and marked as Get HTTP-request that returns Text response as Json.
hello :: Get IO (Resp Json Text)
hello = pure $ ok "Hello World!"
-- | The handler definition as a function
hello :: Get (Resp Text)
hello = pure $ ok "Hello World"

-- | The handler definition as a function with a query parameter to ask for the user name
bye :: Query "user" Text -> Get (Resp Text)
bye (Query name) = pure $ ok ("Goodbye " <> name)

0 comments on commit c284052

Please sign in to comment.