Skip to content

Commit

Permalink
update: dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Oarkflow committed Nov 23, 2024
1 parent 78b523a commit 76a4838
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 5 deletions.
10 changes: 5 additions & 5 deletions examples/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ func main() {
flow := dag.NewDAG("Multi-Step Form", "multi-step-form", func(taskID string, result mq.Result) {
fmt.Printf("Final result for task %s: %s\n", taskID, string(result.Payload))
})
flow.AddNode(dag.Page, "FormStep1", "FormStep1", &FormStep1{})
flow.AddNode(dag.Page, "FormStep2", "FormStep2", &FormStep2{})
flow.AddNode(dag.Page, "FormResult", "FormResult", &FormResult{})
flow.AddNode(dag.Page, "Form Step1", "FormStep1", &FormStep1{})
flow.AddNode(dag.Page, "Form Step2", "FormStep2", &FormStep2{})
flow.AddNode(dag.Page, "Form Result", "FormResult", &FormResult{})

// Define edges
flow.AddEdge(dag.Simple, "FormStep1", "FormStep1", "FormStep2")
flow.AddEdge(dag.Simple, "FormStep2", "FormStep2", "FormResult")
flow.AddEdge(dag.Simple, "Form Step1", "FormStep1", "FormStep2")
flow.AddEdge(dag.Simple, "Form Step2", "FormStep2", "FormResult")

// Start the flow
if flow.Error != nil {
Expand Down
44 changes: 44 additions & 0 deletions examples/form.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "Multi-Step Form",
"key": "multi-step-form",
"nodes": [
{
"name": "Form Step1",
"id": "FormStep1",
"node": "Page",
"data": {
"template": "<html><body><form method=\\\"post\\\" action=\\\"/process?task_id={{task_id}}&next=true\\\"><label>Name:</label><input type=\\\"text\\\" name=\\\"name\\\" required><label>Age:</label><input type=\\\"number\\\" name=\\\"age\\\" required><button type=\\\"submit\\\">Next</button></form></body></html>",
"mapping": {}
},
"first_node": true
},
{
"name": "Form Step2",
"id": "FormStep2",
"node": "Page",
"data": {
"template": "<html><body><form method=\\\"post\\\" action=\\\"/process?task_id={{task_id}}&next=true\\\">{{ if show_voting_controls }}<label>Do you want to register to vote?</label><input type=\\\"checkbox\\\" name=\\\"register_vote\\\"><button type=\\\"submit\\\">Next</button>{{ else }}<p>You are not eligible to vote.</p>{{ end }}</form></body></html>",
"mapping": {}
}
},
{
"name": "Form Result",
"id": "FormResult",
"node": "Page",
"data": {
"template": "<html><body><h1>Form Summary</h1><p>Name: {{ name }}</p><p>Age: {{ age }}</p>{{ if register_vote }}<p>You have registered to vote!</p>{{ else }}<p>You did not register to vote.</p>{{ end }}</body></html>",
"mapping": {}
}
}
],
"edges": [
{
"source": "FormStep1",
"target": ["FormStep2"]
},
{
"source": "FormStep2",
"target": ["FormResult"]
}
]
}
158 changes: 158 additions & 0 deletions examples/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package main

import (
"context"
"encoding/json"
"fmt"
"github.com/oarkflow/jet"
"github.com/oarkflow/mq"
"github.com/oarkflow/mq/consts"
"github.com/oarkflow/mq/dag"
"log"
)

type Data struct {
Template string `json:"template"`
Mapping map[string]string `json:"mapping"`
}

type Node struct {
Name string `json:"name"`
ID string `json:"id"`
Node string `json:"node"`
Data Data `json:"data"`
FirstNode bool `json:"first_node"`
}

type Edge struct {
Source string `json:"source"`
Target []string `json:"target"`
}

type Handler struct {
Name string `json:"name"`
Key string `json:"key"`
Nodes []Node `json:"nodes"`
Edges []Edge `json:"edges"`
}

func CreateDAGFromJSON(config string) (*dag.DAG, error) {
var handler Handler
err := json.Unmarshal([]byte(config), &handler)
if err != nil {
return nil, fmt.Errorf("failed to parse JSON: %v", err)
}

flow := dag.NewDAG(handler.Name, handler.Key, func(taskID string, result mq.Result) {
fmt.Printf("Final result for task %s: %s\n", taskID, string(result.Payload))
})

nodeMap := make(map[string]mq.Processor)

for _, node := range handler.Nodes {
op := &HTMLProcessor{
Template: node.Data.Template,
Mapping: node.Data.Mapping,
}
nodeMap[node.ID] = op
flow.AddNode(dag.Page, node.Name, node.ID, op, node.FirstNode)
}

for _, edge := range handler.Edges {
for _, target := range edge.Target {
flow.AddEdge(dag.Simple, edge.Source, edge.Source, target)
}
}

if flow.Error != nil {
return nil, flow.Error
}
return flow, nil
}

type HTMLProcessor struct {
dag.Operation
Template string
Mapping map[string]string
}

func (p *HTMLProcessor) ProcessTask(ctx context.Context, task *mq.Task) mq.Result {
parser := jet.NewWithMemory(jet.WithDelims("{{", "}}"))
data := map[string]interface{}{
"task_id": ctx.Value("task_id"),
}

for key, value := range p.Mapping {
data[key] = value
}

rs, err := parser.ParseTemplate(p.Template, data)
if err != nil {
return mq.Result{Error: err, Ctx: ctx}
}

ctx = context.WithValue(ctx, consts.ContentType, consts.TypeHtml)
response := map[string]interface{}{
"html_content": rs,
}
payload, _ := json.Marshal(response)

return mq.Result{Payload: payload, Ctx: ctx}
}

func main() {
// JSON configuration
jsonConfig := `{
"name": "Multi-Step Form",
"key": "multi-step-form",
"nodes": [
{
"name": "Form Step1",
"id": "FormStep1",
"node": "Page",
"data": {
"template": "<html>\n<body>\n<form method=\"post\" action=\"/process?task_id={{task_id}}&next=true\">\n <label>Name:</label>\n <input type=\"text\" name=\"name\" required>\n <label>Age:</label>\n <input type=\"number\" name=\"age\" required>\n <button type=\"submit\">Next</button>\n</form>\n</body>\n</html>",
"mapping": {}
},
"first_node": true
},
{
"name": "Form Step2",
"id": "FormStep2",
"node": "Page",
"data": {
"template": "<html>\n<body>\n<form method=\"post\" action=\"/process?task_id={{task_id}}&next=true\">\n {{ if show_voting_controls }}\n <label>Do you want to register to vote?</label>\n <input type=\"checkbox\" name=\"register_vote\">\n <button type=\"submit\">Next</button>\n {{ else }}\n <p>You are not eligible to vote.</p>\n {{ end }}\n</form>\n</body>\n</html>",
"mapping": {
"show_voting_controls": "conditional"
}
}
},
{
"name": "Form Result",
"id": "FormResult",
"node": "Page",
"data": {
"template": "<html>\n<body>\n<h1>Form Summary</h1>\n<p>Name: {{ name }}</p>\n<p>Age: {{ age }}</p>\n{{ if register_vote }}\n <p>You have registered to vote!</p>\n{{ else }}\n <p>You did not register to vote.</p>\n{{ end }}\n</body>\n</html>",
"mapping": {}
}
}
],
"edges": [
{
"source": "FormStep1",
"target": ["FormStep2"]
},
{
"source": "FormStep2",
"target": ["FormResult"]
}
]
}`

flow, err := CreateDAGFromJSON(jsonConfig)
if err != nil {
log.Fatalf("Error creating DAG: %v", err)
}

flow.Start(context.Background(), "0.0.0.0:8082")
}

0 comments on commit 76a4838

Please sign in to comment.