mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
Merge branch 'master' into rjNemo/gitpod-setup
This commit is contained in:
commit
a355a80274
6 changed files with 128 additions and 69 deletions
|
|
@ -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/
|
||||
|
|
|
|||
23
LICENSE.md
23
LICENSE.md
|
|
@ -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.
|
||||
|
|
|
|||
25
README.md
25
README.md
|
|
@ -10,12 +10,12 @@ Web app which returns the iconic Hello, World in various locales
|
|||
- [x] Select the locale by id
|
||||
- [x] Use with CLI
|
||||
- [x] Basic HTTP server
|
||||
- [ ] Create a frontend (html template rendering)
|
||||
- [ ] HTML frontend
|
||||
- [ ] Deployment
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
On macOS use HomeBrew to install `deno` then update to latest version.
|
||||
On macOS use HomeBrew to install `deno` then update to the latest version.
|
||||
|
||||
```shell
|
||||
brew install deno
|
||||
|
|
@ -51,29 +51,34 @@ 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
|
||||
|
||||
```shell
|
||||
make tests
|
||||
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
|
||||
|
|
|
|||
6
deps.ts
6
deps.ts
|
|
@ -5,7 +5,5 @@ 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";
|
||||
export type { RouterContext } from "https://deno.land/x/oak/mod.ts";
|
||||
|
|
|
|||
47
server.ts
47
server.ts
|
|
@ -1,32 +1,39 @@
|
|||
import { listenAndServe, ServerRequest } from "./deps.ts";
|
||||
import { Application, Router, RouterContext } from "./deps.ts";
|
||||
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
|
||||
import { htmlBody } from "./view.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()) });
|
||||
}
|
||||
type AppOpts = { port: number };
|
||||
const get_application = ({ port }: AppOpts): Application => {
|
||||
const router = new Router();
|
||||
|
||||
router.get("/", (ctx: RouterContext) => {
|
||||
ctx.response.body = htmlBody(JSON.stringify(sayRandomHello(), null, 2));
|
||||
}).get("/:id", (ctx: RouterContext) => {
|
||||
try {
|
||||
const value = parseInt(parseURL(req.url), 10);
|
||||
const id = new ID(value);
|
||||
return req.respond({ body: JSON.stringify(sayHello(id)) });
|
||||
const value = ctx.params.id;
|
||||
if (!!value) {
|
||||
const id = new ID(parseInt(value, 10));
|
||||
ctx.response.body = htmlBody(JSON.stringify(sayHello(id), null, 2));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return req.respond({ body: error.message });
|
||||
ctx.response.body = error.message;
|
||||
}
|
||||
});
|
||||
|
||||
const app = new Application();
|
||||
|
||||
app.use(router.routes());
|
||||
|
||||
app.addEventListener("listen", ({ port }) => {
|
||||
console.log(`Server listening on http://localhost:${port}/`);
|
||||
});
|
||||
return app;
|
||||
};
|
||||
|
||||
await serve(port);
|
||||
const app = get_application({ port });
|
||||
|
||||
await app.listen({ port });
|
||||
console.log(`Finished`);
|
||||
|
|
|
|||
44
view.ts
Normal file
44
view.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
export const htmlBody = (content: string) =>
|
||||
`<!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">${content}</pre>
|
||||
</body>
|
||||
</html>`;
|
||||
Loading…
Reference in a new issue