|
| 1 | +## Usage |
| 2 | + |
| 3 | +Functional HTTP Server is optimized to write elegant and powerful point-free functions. This example uses the Ramda |
| 4 | +library - for simplification - but you should be able to use any library that implements the Fantasy-land |
| 5 | +specifications. |
| 6 | + |
| 7 | +This example showcase how to create an endpoint handler for `POST /hoge` that writes to a local file and to Redis |
| 8 | +simultaneously the content of the request's body and, replies with `201`. |
| 9 | + |
| 10 | +```js |
| 11 | +import Task from "https://deno.land/x/[email protected]/library/Task.js"; |
| 12 | +import { |
| 13 | + decodeRaw, |
| 14 | + encodeText, |
| 15 | + evert, |
| 16 | + safeExtract |
| 17 | +} from "https://deno.land/x/[email protected]/library/utilities.js"; |
| 18 | +import File from "https://deno.land/x/[email protected]/library/File.js"; |
| 19 | +import Request from "https://deno.land/x/[email protected]/library/Request.js"; |
| 20 | +import Response from "https://deno.land/x/[email protected]/library/Response.js"; |
| 21 | +import { fetch } from "https://deno.land/x/[email protected]/library/browser_safe.js"; |
| 22 | +import { writeFile } from "https://deno.land/x/[email protected]/library/fs.js"; |
| 23 | +import RedisRequest from "https://deno.land/x/[email protected]/library/RedisRequest.js"; |
| 24 | +import { $$ rawPlaceholder } from "https://deno.land/x/[email protected]/library/Symbol.js"; |
| 25 | +import { executeRedisCommandWithSession } from "https://deno.land/x/[email protected]/library/client.js"; |
| 26 | + |
| 27 | +import { handlers, route } from "https://deno.land/x/[email protected]/library/route.js"; |
| 28 | +import startHTTPServer from "https://deno.land/x/[email protected]/library/server.js"; |
| 29 | + |
| 30 | +startHTTPServer( |
| 31 | + { port: 8080 }, |
| 32 | + route( |
| 33 | + handlers.post( |
| 34 | + "/hoge", |
| 35 | + compose( |
| 36 | + map(_ => Response.Created({ 'content-type': "text/plain" }, encodeText("Created!"))), |
| 37 | + converge( |
| 38 | + (...tasks) => evert(Task, tasks), |
| 39 | + [ |
| 40 | + compose( |
| 41 | + executeRedisCommandWithSession({ port: 6379 }), |
| 42 | + concat(RedisRequest("SET", new Uint8Array([]), [ "hoge", $$rawPlaceholder ])) |
| 43 | + ), |
| 44 | + compose( |
| 45 | + writeFile({}), |
| 46 | + concat(File.fromPath(`${Deno.cwd()}/hoge`)) |
| 47 | + ) |
| 48 | + ] |
| 49 | + ) |
| 50 | + ) |
| 51 | + ) |
| 52 | + ) |
| 53 | +); |
| 54 | + |
| 55 | +const container = await fetch( |
| 56 | + Request( |
| 57 | + { |
| 58 | + headers: { |
| 59 | + 'accept': 'text/plain', |
| 60 | + 'content-type': 'text/plain' |
| 61 | + }, |
| 62 | + method: 'POST', |
| 63 | + url: 'http://localhost:8080/hoge' |
| 64 | + }, |
| 65 | + encodeText("Hello, Hoge!") |
| 66 | + ) |
| 67 | +).run() |
| 68 | + |
| 69 | +const response = safeExtract("Failed to unpack the response", container); |
| 70 | + |
| 71 | +assert(Response.Success.is(response)); |
| 72 | +assertEquals(response.headers.status, 201); |
| 73 | + |
| 74 | +server.close(); |
| 75 | +``` |
| 76 | + |
| 77 | +## Simple HTTP server |
| 78 | + |
| 79 | +The fastest way to start a HTTP server is to use the `startHTTPServer` function. |
| 80 | +The function takes two arguments; the first argument is the options, and the second is a unary |
| 81 | +function that takes a `Request` and return a `Task` of a `Response`. |
| 82 | + |
| 83 | +```js |
| 84 | +import Task from "https://deno.land/x/[email protected]/library/Task.js"; |
| 85 | +import Response from "https://deno.land/x/[email protected]/library/Response.js"; |
| 86 | +import startHTTPServer from "https://deno.land/x/[email protected]/library/server.js"; |
| 87 | + |
| 88 | +startHTTPServer({ port: 8080 }, request => Task.of(Response.OK({}, request.raw))); |
| 89 | +``` |
| 90 | + |
| 91 | +You can test this simple server by executing it your file |
| 92 | + |
| 93 | +```bash |
| 94 | +$ deno run --allow-net server.js |
| 95 | +``` |
| 96 | + |
| 97 | +```bash |
| 98 | +$ curl localhost:8080 -d "Hello, Hoge!" |
| 99 | +> Hello, Hoge! |
| 100 | +``` |
0 commit comments