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

View file

@ -2,20 +2,19 @@
Copyright (c) [2021] [Ruidy] Copyright (c) [2021] [Ruidy]
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy of
of this software and associated documentation files (the "Software"), to deal this software and associated documentation files (the "Software"), to deal in
in the Software without restriction, including without limitation the rights the Software without restriction, including without limitation the rights to
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
copies of the Software, and to permit persons to whom the Software is the Software, and to permit persons to whom the Software is furnished to do so,
furnished to do so, subject to the following conditions: subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE.

View file

@ -47,8 +47,8 @@ with the following content
## 🚀 Run ## 🚀 Run
See [Makefile](Makefile) for available scripts. See [Makefile](Makefile) for available scripts. The application can run either
The application can run either via CLI or a basic HTTP server. via CLI or a basic HTTP server.
## 🧪 Tests ## 🧪 Tests
@ -58,18 +58,23 @@ make tests
## Contributing ## 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 ## 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 ## Authors
- **Ruidy** - _Initial work_ - [Ruidy](https://github.com/rjNemo) - **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 ## 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, assertThrows,
} from "https://deno.land/std@0.87.0/testing/asserts.ts"; } 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 { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
export { export { Application, Router } from "https://deno.land/x/oak/mod.ts";
listenAndServe,
ServerRequest,
} from "https://deno.land/std@0.87.0/http/server.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"; import { ID, sayHello, sayRandomHello } from "./src/index.ts";
const port = 8000; const port = 8000;
const parseURL = (url: string) => { const router = new Router();
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()) });
}
router.get("/", (ctx: any) => {
ctx.response.body = JSON.stringify(sayRandomHello());
}).get<{ id: string }>("/:id", (ctx: any) => {
try { try {
const value = parseInt(parseURL(req.url), 10); const value = ctx.params.id;
const id = new ID(value); const id = new ID(value);
return req.respond({ body: JSON.stringify(sayHello(id)) }); ctx.response.body = JSON.stringify(sayHello(id));
} catch (error) { } catch (error) {
console.error(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`);