Skip to content

Commit

Permalink
Merge pull request #77 from solaoi/feature_add-query-on-api-component
Browse files Browse the repository at this point in the history
add query on api component
  • Loading branch information
solaoi authored Apr 10, 2022
2 parents 10cafc7 + ab14212 commit 436915f
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ brew upgrade tuna
```sh
# wget または curl でインストール
## releasesタブの最新のバージョンを指定してください。
VERSION=v0.0.16
VERSION=v0.0.17
## 利用OSを指定してください。
OS=linux_amd64
## wget経由の場合
Expand Down
48 changes: 28 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,33 @@ TUNA-Mayonnaise is a CommandLineTool to generate and serve JSON/HTML on the node
<details>
<summary>CLICK</summary>

- [Why](#why)
- [Get Started](#get-started)
- [Install](#install)
- [Usage](#usage)
- [Tool Features](#tool-features)
- [Template Engine](#template-engine)
- [API](#api)
- [Database](#database)
- [API Features](#api-features)
- [Monitoring](#monitoring)
- [UseCases](#usecases)
- [1. Serve Static JSON](#1-serve-static-json)
- [2. Serve Static HTML](#2-serve-static-html)
- [3. Serve Dynamic JSON](#3-serve-dynamic-json)
- [4. Serve Dynamic HTML](#4-serve-dynamic-html)
- [Dependencies](#dependencies)
- [BACKEND Dependencies](#backend-dependencies)
- [FRONTEND Dependencies](#frontend-dependencies)
- [FRONTEND DEV Dependencies](#frontend-dev-dependencies)
- [!tuna-mayonnaise](#)
- [Table of Contents](#table-of-contents)
- [Why](#why)
- [Get Started](#get-started)
- [Install](#install)
- [For MacOS (Homebrew)](#for-macos-homebrew)
- [For Others (Binary Releases)](#for-others-binary-releases)
- [Usage](#usage)
- [1. Launch a tool on your browser](#1-launch-a-tool-on-your-browser)
- [2. Serve your JSON/HTML](#2-serve-your-jsonhtml)
- [Tool Features](#tool-features)
- [Template Engine](#template-engine)
- [API](#api)
- [Database](#database)
- [Connection Options](#connection-options)
- [TLS / SSL](#tls--ssl)
- [API Features](#api-features)
- [Monitoring](#monitoring)
- [UseCases](#usecases)
- [1. Serve Static JSON](#1-serve-static-json)
- [2. Serve Static HTML](#2-serve-static-html)
- [3. Serve Dynamic JSON](#3-serve-dynamic-json)
- [4. Serve Dynamic HTML](#4-serve-dynamic-html)
- [Dependencies](#dependencies)
- [BACKEND Dependencies](#backend-dependencies)
- [FRONTEND Dependencies](#frontend-dependencies)
- [FRONTEND DEV Dependencies](#frontend-dev-dependencies)

</details>
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -74,7 +82,7 @@ you can download a binary release [here](https://github.com/solaoi/tuna-mayonnai
```sh
# Install with wget or curl
## set the latest version on releases.
VERSION=v0.0.16
VERSION=v0.0.17
## set the OS you use.
OS=linux
## case you use wget
Expand Down
32 changes: 26 additions & 6 deletions command/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func isDBNode(name string) bool {
}

func isJSONNode(name string) bool {
return isDBNode(name) || name == "JSON" || name == "API" || name == "JSONManager" || name == "Request"
return isDBNode(name) || name == "JSON" || name == "DummyJSON" || name == "API" || name == "JSONManager" || name == "Request"
}

func isJSONorHTMLNode(name string) bool {
Expand Down Expand Up @@ -213,9 +213,17 @@ func contentBuilder(contents map[int]map[string]map[string]interface{}) func(req
url := fmt.Sprintf("%v", content["url"])
cached := fmt.Sprintf("%v", content["cached"])
method := fmt.Sprintf("%v", content["method"])
query := ""
for _, v1 := range c[i+1] {
if v1["parent"] == k {
if v1["name"] != "DummyJSON" {
query = v1["content"].(string)
}
}
}

if cached == "true" {
v, ok := gache.Get(url)
v, ok := gache.Get(url+query)
if ok {
apiResponses = append(apiResponses, apiResponse{method, url, "cached"})
c[i][k]["content"] = v
Expand All @@ -224,6 +232,18 @@ func contentBuilder(contents map[int]map[string]map[string]interface{}) func(req
}

req, _ := http.NewRequest(method, url, nil)
if(query != ""){
params := request.URL.Query()
tmp := map[string]string{}
err := json.Unmarshal([]byte(query), &tmp)
if err != nil {
log.Fatal(err)
}
for k, v := range tmp {
params.Add(k,v)
}
req.URL.RawQuery = params.Encode()
}
resp, err := client.Do(req)
if err == nil && resp.StatusCode >= 400 {
apiResponses = append(apiResponses, apiResponse{method, url, strconv.Itoa(resp.StatusCode)})
Expand All @@ -247,13 +267,13 @@ func contentBuilder(contents map[int]map[string]map[string]interface{}) func(req
if cached == "true" {
cacheTime := fmt.Sprintf("%v", content["cacheTime"])
if cacheTime == "" {
gache.Set(url, res)
gache.Set(url+query, res)
} else {
cacheTime, err := strconv.Atoi(cacheTime)
if err != nil {
gache.Set(url, res)
log.Fatal(err)
} else {
gache.SetWithExpire(url, res, time.Second*time.Duration(cacheTime))
gache.SetWithExpire(url+query, res, time.Second*time.Duration(cacheTime))
}
}
}
Expand Down Expand Up @@ -318,7 +338,7 @@ func contentBuilder(contents map[int]map[string]map[string]interface{}) func(req
if v1["parent"] == k {
if v1["name"] == "SQL" {
query = v1["content"].(string)
} else if v1["name"] == "JSON" {
} else if v1["name"] == "DummyJSON" {
dummyJSON = v1["content"].(string)
}
}
Expand Down
12 changes: 8 additions & 4 deletions ui/src/rete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import AutoArrangePlugin from "rete-auto-arrange-plugin";
import HistoryPlugin from "rete-history-plugin";

import { JsonComponent } from "./rete/components/input/JsonComponent";
import { DummyJsonComponent } from "./rete/components/input/DummyJsonComponent";
import { HtmlComponent } from "./rete/components/input/HtmlComponent";
import { SqlComponent } from "./rete/components/input/SqlComponent";
import { TemplateComponent } from "./rete/components/template/TemplateComponent";
Expand Down Expand Up @@ -39,6 +40,8 @@ export async function createEditor(container) {
rawJsonSocket.combineWith(errorSocket);
rawJsonSocket.combineWith(jsonSocket);
rawJsonSocket.combineWith(stringSocket);
// dummyJson
const dummyJsonSocket = new Rete.Socket("DummyJson value");
// template
const templateSocket = new Rete.Socket("Template value");
templateSocket.combineWith(stringSocket);
Expand All @@ -62,15 +65,16 @@ export async function createEditor(container) {
new EndpointWithErrorComponent(stringSocket, errorSocket),
new JsonManagerComponent(jsonSocket),
new JsonComponent(rawJsonSocket),
new DummyJsonComponent(dummyJsonSocket),
new HtmlComponent(rawHtmlSocket),
new TemplateComponent(jsonSocket, templateSocket, htmlSocket),
new HandlebarsComponent(handlebarsSocket),
new PugComponent(pugSocket),
new SqlComponent(sqlSocket),
new ApiComponent(jsonSocket),
new MySQLComponent(jsonSocket, sqlSocket),
new PostgreSQLComponent(jsonSocket, sqlSocket),
new SQLiteComponent(jsonSocket, sqlSocket),
new ApiComponent(jsonSocket, dummyJsonSocket),
new MySQLComponent(jsonSocket, dummyJsonSocket, sqlSocket),
new PostgreSQLComponent(jsonSocket, dummyJsonSocket, sqlSocket),
new SQLiteComponent(jsonSocket, dummyJsonSocket, sqlSocket),
new RequestComponent(jsonSocket),
new RedirectEndpointComponent(),
];
Expand Down
14 changes: 9 additions & 5 deletions ui/src/rete/components/ApiComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ import { SelectControl } from "../controls/SelectControl";
export class ApiComponent extends Rete.Component {
path = ["New"];

constructor(jsonSocket) {
constructor(jsonSocket, dummyJsonSocket) {
super("API");
this.data.component = DefaultNode; // optional
this.jsonSocket = jsonSocket;
this.dummyJsonSocket = dummyJsonSocket;
}

builder(node) {
const jsonInput = new Rete.Input(
const queryInput = new Rete.Input("query", "Query (JSON)", this.jsonSocket);
const dummyJsonInput = new Rete.Input(
"json",
"Dummy Output (JSON)",
this.jsonSocket
"Output (DummyJSON)",
this.dummyJsonSocket
);
const out = new Rete.Output("json", "JSON", this.jsonSocket);

return node
.addInput(jsonInput)
.addInput(queryInput)
.addInput(dummyJsonInput)
.addControl(
new SelectControl(this.editor, "method", node, false, "Method", [
"GET",
Expand Down Expand Up @@ -67,6 +70,7 @@ export class ApiComponent extends Rete.Component {
}

worker(node, inputs, outputs) {
outputs.query = inputs.query.length ? inputs.query[0] : node.data.query;
outputs.json = inputs.json.length ? inputs.json[0] : node.data.json;
outputs.url = node.data.url;
outputs.cached = node.data.cached;
Expand Down
11 changes: 6 additions & 5 deletions ui/src/rete/components/MySQLComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@ import { DbNode } from "../nodes/DbNode";
export class MySQLComponent extends Rete.Component {
path = ["New"];

constructor(jsonSocket, sqlSocket) {
constructor(jsonSocket, dummyJsonSocket, sqlSocket) {
super("MySQL");
this.data.component = DbNode; // optional
this.dummyJsonSocket = dummyJsonSocket;
this.jsonSocket = jsonSocket;
this.sqlSocket = sqlSocket;
}

builder(node) {
const jsonInput = new Rete.Input(
const dummyJsonInput = new Rete.Input(
"json",
"Dummy Output (JSON)",
this.jsonSocket
"Output (DummyJSON)",
this.dummyJsonSocket
);
const out = new Rete.Output("json", "JSON", this.jsonSocket);
const sqlInput = new Rete.Input("sql", "SQL", this.sqlSocket);

return node
.addInput(sqlInput)
.addInput(jsonInput)
.addInput(dummyJsonInput)
.addControl(
new SelectControl(this.editor, "tls", node, false, "TLS", [
"false",
Expand Down
11 changes: 6 additions & 5 deletions ui/src/rete/components/PostgreSQLComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@ import { DbNode } from "../nodes/DbNode";
export class PostgreSQLComponent extends Rete.Component {
path = ["New"];

constructor(jsonSocket, sqlSocket) {
constructor(jsonSocket, dummyJsonSocket, sqlSocket) {
super("PostgreSQL");
this.data.component = DbNode; // optional
this.dummyJsonSocket = dummyJsonSocket;
this.jsonSocket = jsonSocket;
this.sqlSocket = sqlSocket;
}

builder(node) {
const jsonInput = new Rete.Input(
const dummyJsonInput = new Rete.Input(
"json",
"Dummy Output (JSON)",
this.jsonSocket
"Output (DummyJSON)",
this.dummyJsonSocket
);
const out = new Rete.Output("json", "JSON", this.jsonSocket);
const sqlInput = new Rete.Input("sql", "SQL", this.sqlSocket);

return node
.addInput(sqlInput)
.addInput(jsonInput)
.addInput(dummyJsonInput)
.addControl(
new SelectControl(this.editor, "tls", node, false, "SSLMODE", [
"disable",
Expand Down
11 changes: 6 additions & 5 deletions ui/src/rete/components/SQLiteComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ import { SQLiteNode } from "../nodes/SQLiteNode";
export class SQLiteComponent extends Rete.Component {
path = ["New"];

constructor(jsonSocket, sqlSocket) {
constructor(jsonSocket, dummyJsonSocket, sqlSocket) {
super("SQLite");
this.data.component = SQLiteNode; // optional
this.dummyJsonSocket = dummyJsonSocket;
this.jsonSocket = jsonSocket;
this.sqlSocket = sqlSocket;
}

builder(node) {
const jsonInput = new Rete.Input(
const dummyJsonInput = new Rete.Input(
"json",
"Dummy Output (JSON)",
this.jsonSocket
"Output (DummyJSON)",
this.dummyJsonSocket
);
const out = new Rete.Output("json", "JSON", this.jsonSocket);
const sqlInput = new Rete.Input("sql", "SQL", this.sqlSocket);

return node
.addInput(sqlInput)
.addInput(jsonInput)
.addInput(dummyJsonInput)
.addControl(
new TextControl(
this.editor,
Expand Down
23 changes: 23 additions & 0 deletions ui/src/rete/components/input/DummyJsonComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Rete from "rete";
import { JsonControl } from "../../controls/JsonControl";

export class DummyJsonComponent extends Rete.Component {
path = ["New"];

constructor(socket) {
super("DummyJSON");
this.socket = socket;
}

builder(node) {
const out = new Rete.Output("json", "JSON", this.socket);

return node
.addControl(new JsonControl(this.editor, "json", node))
.addOutput(out);
}

worker(node, inputs, outputs) {
outputs.json = node.data.json;
}
}

0 comments on commit 436915f

Please sign in to comment.