use oak framework

This commit is contained in:
Ruidy 2021-02-22 17:18:33 +01:00
parent be02f8e5a3
commit 0f54f17f18
6 changed files with 135 additions and 71 deletions

View file

@ -1,20 +1,25 @@
# Contributing
When contributing to this repository, please first discuss the change you wish to make via issue,
email, or any other method with the owners of this repository before making a change.
When contributing to this repository, please first discuss the change you wish
to make via issue, email, or any other method with the owners of this repository
before making a change.
Please note we have a code of conduct, please follow it in all your interactions with the project.
Please note we have a code of conduct, please follow it in all your interactions
with the project.
## Pull Request Process
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
build.
2. Update the README.md with details of changes to the interface, this includes new environment
variables, exposed ports, useful file locations and container parameters.
3. Increase the version numbers in any examples files and the README.md to the new version that this
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
do not have permission to do that, you may request the second reviewer to merge it for you.
1. Ensure any install or build dependencies are removed before the end of the
layer when doing a build.
2. Update the README.md with details of changes to the interface, this includes
new environment variables, exposed ports, useful file locations and container
parameters.
3. Increase the version numbers in any examples files and the README.md to the
new version that this Pull Request would represent. The versioning scheme we
use is [SemVer](http://semver.org/).
4. You may merge the Pull Request in once you have the sign-off of two other
developers, or if you do not have permission to do that, you may request the
second reviewer to merge it for you.
## Code of Conduct
@ -23,9 +28,9 @@ Please note we have a code of conduct, please follow it in all your interactions
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.
size, disability, ethnicity, gender identity and expression, level of
experience, nationality, personal appearance, race, religion, or sexual identity
and orientation.
### Our Standards
@ -55,11 +60,11 @@ Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
Project maintainers have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, or to ban temporarily or permanently any
contributor for other behaviors that they deem inappropriate, threatening,
offensive, or harmful.
### Scope
@ -76,8 +81,9 @@ Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at [ruidy.nemausat@gmail.com]. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
obligated to maintain confidentiality with regard to the reporter of an
incident. Further details of specific enforcement policies may be posted
separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
@ -85,8 +91,8 @@ members of the project's leadership.
### Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/

View file

@ -2,20 +2,19 @@
Copyright (c) [2021] [Ruidy]
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:
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.
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.

View file

@ -47,8 +47,8 @@ with the following content
## 🚀 Run
See [Makefile](Makefile) for available scripts.
The application can run either via CLI or a basic HTTP server.
See [Makefile](Makefile) for available scripts. The application can run either
via CLI or a basic HTTP server.
## 🧪 Tests
@ -58,18 +58,23 @@ make tests
## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of
conduct, and the process for submitting pull requests to us.
## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/rjNemo/deno_hello/tags).
We use [SemVer](http://semver.org/) for versioning. For the versions available,
see the [tags on this repository](https://github.com/rjNemo/deno_hello/tags).
## Authors
- **Ruidy** - _Initial work_ - [Ruidy](https://github.com/rjNemo)
See also the list of [contributors](https://github.com/rjNemo/deno_hello/contributors) who participated in this project.
See also the list of
[contributors](https://github.com/rjNemo/deno_hello/contributors) who
participated in this project.
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
This project is licensed under the MIT License - see the
[LICENSE.md](LICENSE.md) file for details

View file

@ -5,7 +5,4 @@ export {
assertThrows,
} from "https://deno.land/std@0.87.0/testing/asserts.ts";
export { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
export {
listenAndServe,
ServerRequest,
} from "https://deno.land/std@0.87.0/http/server.ts";
export { Application, Router } from "https://deno.land/x/oak/mod.ts";

60
index.html Normal file
View file

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
max-width: 650px;
margin: 40px auto;
padding: 0 10px;
font: 18px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
color: #444
}
h1, h2, h3 {
line-height: 1.2
}
@media (prefers-color-scheme: dark) {
body {
color: white;
background: #444
}
a:link {
color: #5bf
}
a:visited {
color: #ccf
}
}
</style>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="description" content="Simple client for Hello deno back end">
<title>Hello client</title>
</head>
<body>
<h1>Hello client</h1>
<pre id="result"></pre>
</body>
<script>
const consoleSignatureStyle = "font-size: 16px;" +
"background: linear-gradient(to right, #e66465, #9198e5);" +
"color: white;" +
"text-align: center;" +
"padding: 10px 15px;" +
"width: 100%;" +
"border-radius: 20px;";
const welcomeMessage = "Welcome to Hello client ⚙️"
console.log(`%c ${welcomeMessage}`, consoleSignatureStyle)
let data = document.querySelector("#result")
fetch("http://localhost:8000/")
.then(res => res.json())
.then(json => data.innerHTML = JSON.stringify(json, null, 2))
</script>
</html>

View file

@ -1,32 +1,29 @@
import { listenAndServe, ServerRequest } from "./deps.ts";
import { Application, Router } from "./deps.ts";
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
const port = 8000;
const parseURL = (url: string) => {
const parts = url.slice(1).split("/");
return parts[0];
};
const serve = async (port: number) => {
console.log(`Server listening on http://localhost:${port}/`);
await listenAndServe({ port }, (req: ServerRequest) => {
console.log(`Request on address '${req.url}'`);
if (parseURL(req.url) === "") {
return req.respond({ body: JSON.stringify(sayRandomHello()) });
}
const router = new Router();
router.get("/", (ctx: any) => {
ctx.response.body = JSON.stringify(sayRandomHello());
}).get<{ id: string }>("/:id", (ctx: any) => {
try {
const value = parseInt(parseURL(req.url), 10);
const value = ctx.params.id;
const id = new ID(value);
return req.respond({ body: JSON.stringify(sayHello(id)) });
ctx.response.body = JSON.stringify(sayHello(id));
} catch (error) {
console.error(error);
return req.respond({ body: error.message });
ctx.response.body = error.message;
}
});
};
});
await serve(port);
const app = new Application();
app.use(router.routes());
app.addEventListener("listen", ({ port }: any) => {
console.log(`Server listening on http://localhost:${port}/`);
});
await app.listen({ port });
console.log(`Finished`);