Skip to content

Commit

Permalink
Add examples (#78)
Browse files Browse the repository at this point in the history
* Add tiny example

* fix basic exmaple

* Add server/client exmaples

Co-authored-by: Emil Ivanichkov <[email protected]>

---------

Co-authored-by: Emil Ivanichkov <[email protected]>
  • Loading branch information
fox0430 and EmilIvanichkovv authored Mar 11, 2024
1 parent 223aade commit a9687dd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/basic.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pkg/presto/[route, server]

proc decodeString*(t: typedesc[string], value: string): RestResult[string] =
ok(value)

proc validate(pattern: string, value: string): int = 0

when isMainModule:
var router = RestRouter.init(validate)

router.api(MethodGet, "/") do () -> RestApiResponse:
RestApiResponse.response("Hello World", Http200, "textt/plain")

let restServer = RestServerRef.new(router, initTAddress("127.0.0.1:9000")).get
restServer.start()

runForever()
12 changes: 12 additions & 0 deletions examples/client.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pkg/presto/[route, client]
import ../tests/helpers

proc hello() {.async.} =
var restClient = RestClientRef.new(initTAddress("127.0.0.1:9000"))
proc helloCall(body: string): string {.
rest, endpoint: "/hello/world", meth: MethodPost.}
let res = await restClient.helloCall("Hello Server!", restContentType = "text/plain")
echo "Server response: ", res

when isMainModule:
waitFor hello()
18 changes: 18 additions & 0 deletions examples/server.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pkg/presto/[route, server]
import stew/byteutils

proc validate(pattern: string, value: string): int = 0

when isMainModule:
var router = RestRouter.init(validate)

router.api(MethodPost, "/hello/world") do (
contentBody: Option[ContentBody]) -> RestApiResponse:
echo "Client says: ", string.fromBytes(contentBody.get().data)

RestApiResponse.response("Hello Client, I am Server", Http200, "textt/plain")

let restServer = RestServerRef.new(router, initTAddress("127.0.0.1:9000")).get
restServer.start()

runForever()

0 comments on commit a9687dd

Please sign in to comment.