Skip to content

Commit

Permalink
examples: add todo example for nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 16, 2020
1 parent e9b201c commit a6261b6
Show file tree
Hide file tree
Showing 17 changed files with 881 additions and 105 deletions.
47 changes: 47 additions & 0 deletions examples/go/hello_world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,51 @@ $ go run main.go
$ http://localhost:3000/hello
Hello world!
```

```toml
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "GET /hello"
service = "hello_world"
```

```go
package main

import (
"github.com/lithdew/flatend"
"os"
"os/signal"
)

func check(err error) {
if err != nil {
panic(err)
}
}

func helloWorld(ctx *flatend.Context) {
ctx.WriteHeader("Content-Type", "text/plain; charset=utf-8")
ctx.Write([]byte("Hello world!"))
}

func main() {
node := &flatend.Node{
Services: map[string]flatend.Handler{
"hello_world": helloWorld,
},
}
check(node.Start("127.0.0.1:9000"))

ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch

node.Shutdown()
}
```
47 changes: 47 additions & 0 deletions examples/go/pipe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,51 @@ $ go run main.go
2020/06/17 01:07:17 You are now connected to 127.0.0.1:9000. Services: []
$ POST /pipe (1.6MiB GIF)
```

```toml
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "POST /pipe"
service = "pipe"
```

```go
package main

import (
"github.com/lithdew/flatend"
"io"
"os"
"os/signal"
)

func check(err error) {
if err != nil {
panic(err)
}
}

func pipe(ctx *flatend.Context) {
_, _ = io.Copy(ctx, ctx.Body)
}

func main() {
node := &flatend.Node{
Services: map[string]flatend.Handler{
"pipe": pipe,
},
}
check(node.Start("127.0.0.1:9000"))

ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch

node.Shutdown()
}
```
23 changes: 23 additions & 0 deletions examples/nodejs/hello_world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,27 @@ Successfully dialed 127.0.0.1:9000. Services: []
$ http://localhost:3000/hello
Hello world!
```

```toml
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "GET /hello"
service = "hello_world"
```

```js
const {Node} = require("flatend");

const main = async () => {
const node = new Node();
node.register('hello_world', ctx => ctx.send("Hello world!"));
await node.dial("127.0.0.1:9000");
}

main().catch(err => console.error(err));
```
23 changes: 23 additions & 0 deletions examples/nodejs/pipe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,27 @@ $ node index.js
Successfully dialed 127.0.0.1:9000. Services: []
$ POST /pipe (1.6MiB GIF)
```

```toml
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "POST /pipe"
service = "pipe"
```

```js
const {Node} = require("flatend");

const main = async () => {
const node = new Node();
node.register('pipe', ctx => ctx.pipe(ctx));
await node.dial("127.0.0.1:9000");
}

main().catch(err => console.error(err));
```
8 changes: 8 additions & 0 deletions examples/nodejs/pipe/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "POST /pipe"
service = "pipe"
80 changes: 80 additions & 0 deletions examples/nodejs/todo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# todo

Ye 'ole todo list example using SQLite.

```
$ flatend
2020/06/17 01:27:03 Listening for microservices on '127.0.0.1:9000'.
2020/06/17 01:27:03 Listening for HTTP requests on '[::]:3000'.
2020/06/17 01:27:10 ??? has connected to you. Services: [all_todos add_todo remove_todo done_todo]
$ node index.js
Successfully dialed 127.0.0.1:9000. Services: []
$ http://localhost:3000/
```

```toml
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "GET /"
static = "./public"

[[http.routes]]
path = "GET /todos"
service = "all_todos"

[[http.routes]]
path = "GET /todos/add/:content"
service = "add_todo"

[[http.routes]]
path = "GET /todos/remove/:id"
service = "remove_todo"

[[http.routes]]
path = "GET /todos/done/:id"
service = "done_todo"
```

```js
const {Node} = require("flatend");
const Database = require("better-sqlite3");

const db = new Database(":memory:");
db.exec(`CREATE TABLE todo (id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT, state TEXT)`);

const all = async ctx => ctx.json(await db.prepare(`SELECT id, content, state FROM todo`).all());

const add = async ctx => {
const content = ctx.headers["params.content"];
ctx.json(await db.prepare(`INSERT INTO todo (content, state) VALUES (?, '')`).run(content));
}

const remove = async ctx => {
const id = ctx.headers["params.id"]
ctx.json(await db.prepare(`DELETE FROM todo WHERE id = ?`).run(id))
}

const done = async ctx => {
const id = ctx.headers["params.id"]
ctx.json(await db.prepare(`UPDATE todo SET state = 'done' WHERE id = ?`).run(id));
}

const main = async () => {
const node = new Node();

node.register('all_todos', all);
node.register('add_todo', add);
node.register('remove_todo', remove);
node.register('done_todo', done);

await node.dial("127.0.0.1:9000");
}

main().catch(err => console.error(err));
```
24 changes: 24 additions & 0 deletions examples/nodejs/todo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
addr = "127.0.0.1:9000"

[[http]]
addr = ":3000"

[[http.routes]]
path = "GET /"
static = "./public"

[[http.routes]]
path = "GET /todos"
service = "all_todos"

[[http.routes]]
path = "GET /todos/add/:content"
service = "add_todo"

[[http.routes]]
path = "GET /todos/remove/:id"
service = "remove_todo"

[[http.routes]]
path = "GET /todos/done/:id"
service = "done_todo"
35 changes: 35 additions & 0 deletions examples/nodejs/todo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const {Node} = require("flatend");
const Database = require("better-sqlite3");

const db = new Database(":memory:");
db.exec(`CREATE TABLE todo (id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT, state TEXT)`);

const all = async ctx => ctx.json(await db.prepare(`SELECT id, content, state FROM todo`).all());

const add = async ctx => {
const content = ctx.headers["params.content"];
ctx.json(await db.prepare(`INSERT INTO todo (content, state) VALUES (?, '')`).run(content));
}

const remove = async ctx => {
const id = ctx.headers["params.id"]
ctx.json(await db.prepare(`DELETE FROM todo WHERE id = ?`).run(id))
}

const done = async ctx => {
const id = ctx.headers["params.id"]
ctx.json(await db.prepare(`UPDATE todo SET state = 'done' WHERE id = ?`).run(id));
}

const main = async () => {
const node = new Node();

node.register('all_todos', all);
node.register('add_todo', add);
node.register('remove_todo', remove);
node.register('done_todo', done);

await node.dial("127.0.0.1:9000");
}

main().catch(err => console.error(err));
10 changes: 10 additions & 0 deletions examples/nodejs/todo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "todo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"better-sqlite3": "^7.1.0",
"flatend": "^0.0.1"
}
}
15 changes: 15 additions & 0 deletions examples/nodejs/todo/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Flatend Demo - Todo</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.8.0/css/bulma.min.css">
<link rel="stylesheet" href="/style.css">
</head>
<body>
<div id="root"></div>
<script src="/main.js"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions examples/nodejs/todo/public/main.js

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions examples/nodejs/todo/public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
form {
display: flex;
padding: 10px;
}

.wrapper {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="4" height="4" viewBox="0 0 4 4"%3E%3Cpath fill="%239C92AC" fill-opacity="0.4" d="M1 3h1v1H1V3zm2-2h1v1H3V1z"%3E%3C/path%3E%3C/svg%3E');
}

.input {
margin-right: 10px;
}

.frame {
width: 40vw;
max-width: 400px;
}

.header {
display: inline;
text-align: center;
}

.list-wrapper {
max-height: 200px;
overflow-y: auto;
}
Loading

0 comments on commit a6261b6

Please sign in to comment.