-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add todo example for nodejs
- Loading branch information
Showing
17 changed files
with
881 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.