Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienfilion committed Dec 28, 2020
1 parent 66674f3 commit a74aa17
Show file tree
Hide file tree
Showing 13 changed files with 750 additions and 205 deletions.
16 changes: 16 additions & 0 deletions .github/document.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export FL_TITLE="Functional HTTP Server"
export FL_DESCRIPTION="A simple HTTP server inspired by Express and in tune with Functional Programming principles in \
JavaScript for Deno."
export FL_GITHUB_URL="https://github.com/sebastienfilion/functional-http-server"
export FL_DENO_URL="https://deno.land/x/functional_http_server"
export FL_VERSION="v0.3.1"

deno run --allow-all --unstable ../@functional:generate-documentation/cli.js document \
"$FL_TITLE" \
"$FL_DESCRIPTION" \
$FL_GITHUB_URL \
$FL_DENO_URL \
$FL_VERSION \
./.github/readme-fragment-usage.md \
./library/*.js \
./.github/readme-fragment-license.md
File renamed without changes
20 changes: 20 additions & 0 deletions .github/readme-fragment-license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Contributing

We appreciate your help! Please, [read the guidelines](./CONTRIBUTING.md).

## License

Copyright © 2020 - Sebastien Filion

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
100 changes: 100 additions & 0 deletions .github/readme-fragment-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## Usage

Functional HTTP Server is optimized to write elegant and powerful point-free functions. This example uses the Ramda
library - for simplification - but you should be able to use any library that implements the Fantasy-land
specifications.

This example showcase how to create an endpoint handler for `POST /hoge` that writes to a local file and to Redis
simultaneously the content of the request's body and, replies with `201`.

```js
import Task from "https://deno.land/x/[email protected]/library/Task.js";
import {
decodeRaw,
encodeText,
evert,
safeExtract
} from "https://deno.land/x/[email protected]/library/utilities.js";
import File from "https://deno.land/x/[email protected]/library/File.js";
import Request from "https://deno.land/x/[email protected]/library/Request.js";
import Response from "https://deno.land/x/[email protected]/library/Response.js";
import { fetch } from "https://deno.land/x/[email protected]/library/browser_safe.js";
import { writeFile } from "https://deno.land/x/[email protected]/library/fs.js";
import RedisRequest from "https://deno.land/x/[email protected]/library/RedisRequest.js";
import { $$rawPlaceholder } from "https://deno.land/x/[email protected]/library/Symbol.js";
import { executeRedisCommandWithSession } from "https://deno.land/x/[email protected]/library/client.js";

import { handlers, route } from "https://deno.land/x/[email protected]/library/route.js";
import startHTTPServer from "https://deno.land/x/[email protected]/library/server.js";

startHTTPServer(
{ port: 8080 },
route(
handlers.post(
"/hoge",
compose(
map(_ => Response.Created({ 'content-type': "text/plain" }, encodeText("Created!"))),
converge(
(...tasks) => evert(Task, tasks),
[
compose(
executeRedisCommandWithSession({ port: 6379 }),
concat(RedisRequest("SET", new Uint8Array([]), [ "hoge", $$rawPlaceholder ]))
),
compose(
writeFile({}),
concat(File.fromPath(`${Deno.cwd()}/hoge`))
)
]
)
)
)
)
);

const container = await fetch(
Request(
{
headers: {
'accept': 'text/plain',
'content-type': 'text/plain'
},
method: 'POST',
url: 'http://localhost:8080/hoge'
},
encodeText("Hello, Hoge!")
)
).run()

const response = safeExtract("Failed to unpack the response", container);

assert(Response.Success.is(response));
assertEquals(response.headers.status, 201);

server.close();
```

## Simple HTTP server

The fastest way to start a HTTP server is to use the `startHTTPServer` function.
The function takes two arguments; the first argument is the options, and the second is a unary
function that takes a `Request` and return a `Task` of a `Response`.

```js
import Task from "https://deno.land/x/[email protected]/library/Task.js";
import Response from "https://deno.land/x/[email protected]/library/Response.js";
import startHTTPServer from "https://deno.land/x/[email protected]/library/server.js";

startHTTPServer({ port: 8080 }, request => Task.of(Response.OK({}, request.raw)));
```

You can test this simple server by executing it your file

```bash
$ deno run --allow-net server.js
```

```bash
$ curl localhost:8080 -d "Hello, Hoge!"
> Hello, Hoge!
```
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.DS_Store
.data/
.docs/
.dump/
.idea/
.nyc_output/
.sass-cache/
coverage/
journal/
node_modules/
out/
scratch/

*.db
*.iml
*.log
*.rdb
*.zip

.todo.md

dmfx
.dmfx
*.dmfx.js
42 changes: 42 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Contributor guidelines

## What do I need to know to help?

If you are looking to help to with a code contribution our project uses JavaScript to run on Deno and modern browsers.
If you don't feel ready to make a code contribution yet, no problem! You can also check out
[the issues](https://github.com/sebastienfilion/functional/issues).

Never made an open source contribution before? Wondering how contributions work in the in our project? Here's a quick
rundown!

1. Find an issue that you are interested in addressing, or a feature that you would like to add;
2. Fork the repository associated with the issue to your local GitHub organization. This means that you will have a
copy of the repository under `github-username/repository-name`;
3. Clone the repository to your local machine using git clone https://github.com/github-username/repository-name.git;
4. Create a new branch for your fix using `git checkout -b branch-name-here`. The preferred pattern is to prefix the
branch name, i.e.: `fix/[issue-number|*]`, `document/*` or, `implement/[issue-number|*]`;
5. Make the appropriate changes for the issue you are trying to address, or the feature that you want to implement;
6. Use git to commit your changes with a descriptive message, you can refer to
[this article](https://dev.to/jacobherrington/how-to-write-useful-commit-messages-my-commit-message-template-20n9)
to learn how to write a good commit message;
7. Push the changes to the remote repository using git push origin branch-name-here;
8. Submit a pull request to the upstream repository;
9. Title the pull request with a short description of the changes made and the issue or bug number associated with
your change. For example, you can title an issue like so "Add log messages #4352";
10. In the description of the pull request, explain the changes that you made, any issues you think exist with the
pull request you made, and any questions you have for the maintainer. It's OK if your pull request is not perfect
(no pull request is), the reviewer will be able to help you fix any problems and improve it!
11. Wait for the pull request to be reviewed by a maintainer;
12. Make changes to the pull request if the reviewing maintainer recommends them.
13. Celebrate your success after your pull request is merged!

## Where can I go for help?

If you need help, you can ask questions [on Discord](https://discord.gg/gp83e8dr).

## What does the Code of Conduct mean for me?

Our Code of Conduct means that you are responsible for treating everyone on the project with respect and courtesy
regardless of their identity. If you are the victim of any inappropriate behavior or comments as described in our
Code of Conduct, we are here for you and will do the best to ensure that the abuser is reprimanded appropriately,
per our code.
Loading

0 comments on commit a74aa17

Please sign in to comment.