feat: [wip] - Implement html node

This commit is contained in:
sujit
2024-11-18 20:37:24 +05:45
parent 3aaffe7d07
commit c96eef945d
7 changed files with 110 additions and 58 deletions

View File

@@ -165,7 +165,7 @@ func (tm *DAG) Render(w http.ResponseWriter, r *http.Request) {
ctx := mq.SetHeaders(r.Context(), map[string]string{consts.AwaitResponseKey: "true", "request_type": "render"})
ctx = context.WithValue(ctx, "query_params", r.URL.Query())
rs := tm.Process(ctx, nil)
content, err := jsonparser.GetString(rs.Payload, "content")
content, err := jsonparser.GetString(rs.Payload, "html_content")
if err != nil {
http.Error(w, "Failed to read request body", http.StatusBadRequest)
return

View File

@@ -10,6 +10,7 @@ import (
"github.com/oarkflow/mq"
"github.com/oarkflow/mq/consts"
"github.com/oarkflow/mq/jsonparser"
"github.com/oarkflow/mq/storage"
"github.com/oarkflow/mq/storage/memory"
)
@@ -24,7 +25,7 @@ const (
)
type Result struct {
Ctx context.Context
Ctx context.Context `json:"-"`
Data json.RawMessage
Error error
Status TaskStatus
@@ -188,6 +189,59 @@ func (tm *DAG) ProcessTask(ctx context.Context, payload []byte) Result {
return <-resultCh
}
func (tm *DAG) render(w http.ResponseWriter, request *http.Request) {
ctx, data, err := parse(request)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
result := tm.ProcessTask(ctx, data)
if contentType, ok := result.Ctx.Value(consts.ContentType).(string); ok && contentType == consts.TypeHtml {
w.Header().Set(consts.ContentType, consts.TypeHtml)
data, err := jsonparser.GetString(result.Data, "content")
if err != nil {
return
}
w.Write([]byte(data))
}
}
func (tm *DAG) submit(ctx context.Context, payload []byte) Result {
var taskID string
userCtx := UserContext(ctx)
if val := userCtx.Get("task_id"); val != "" {
taskID = val
} else {
taskID = mq.NewID()
}
ctx = context.WithValue(ctx, "task_id", taskID)
userContext := UserContext(ctx)
next := userContext.Get("next")
manager, ok := tm.taskManager.Get(taskID)
resultCh := make(chan Result, 1)
if !ok {
manager = NewTaskManager(tm, resultCh)
tm.taskManager.Set(taskID, manager)
} else {
manager.resultCh = resultCh
}
if next == "true" {
nodes, err := tm.GetNextNodes(manager.currentNode)
if err != nil {
return Result{Error: err}
}
if len(nodes) > 0 {
ctx = context.WithValue(ctx, "initial_node", nodes[0].ID)
}
}
firstNode, err := tm.parseInitialNode(ctx)
if err != nil {
return Result{Error: err}
}
manager.ProcessTask(ctx, taskID, firstNode, payload)
return <-resultCh
}
func (tm *DAG) formHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
http.ServeFile(w, r, "webroot/form.html")
@@ -221,25 +275,35 @@ func (tm *DAG) taskStatusHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"message": "Invalid TaskID"}`, http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set(consts.ContentType, consts.TypeJson)
json.NewEncoder(w).Encode(manager.taskStates)
}
func (tm *DAG) Start(addr string) {
http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
ctx, data, err := parse(request)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
result := tm.ProcessTask(ctx, data)
if contentType, ok := result.Ctx.Value(consts.ContentType).(string); ok && contentType == consts.TypeHtml {
w.Header().Set(consts.ContentType, consts.TypeHtml)
w.Write(result.Data)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
tm.render(w, r)
} else if r.Method == "POST" {
ctx, data, err := parse(r)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
result := tm.submit(ctx, data)
if result.Ctx == nil {
fmt.Println("Ctrl not set")
return
}
if contentType, ok := result.Ctx.Value(consts.ContentType).(string); ok && contentType == consts.TypeHtml {
w.Header().Set(consts.ContentType, consts.TypeHtml)
data, err := jsonparser.GetString(result.Data, "content")
if err != nil {
return
}
w.Write([]byte(data))
}
}
})
http.HandleFunc("/form", tm.formHandler)
http.HandleFunc("/result", tm.resultHandler)
http.HandleFunc("/task-result", tm.taskStatusHandler)
http.ListenAndServe(addr, nil)
}

View File

@@ -200,6 +200,7 @@ func (tm *TaskManager) aggregateResults(parentNode string, taskID string) {
} else if state.targetResults.Size() == 1 {
state.Result = state.targetResults.Values()[0]
}
tm.resultCh <- state.Result
tm.processFinalResult(taskID, state)
}

View File

@@ -116,7 +116,7 @@ type Final struct {
func (e *Final) ProcessTask(ctx context.Context, task *mq.Task) mq.Result {
rs := map[string]any{
"content": `<strong>Processed successfully!</strong>`,
"html_content": `<strong>Processed successfully!</strong>`,
}
bt, _ := json.Marshal(rs)
return mq.Result{Payload: bt, Ctx: ctx}

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/oarkflow/jet"
@@ -12,45 +13,23 @@ import (
)
func Form(ctx context.Context, payload json.RawMessage) v2.Result {
template := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Data Form</title>
</head>
<body>
<h1>Enter Your Information</h1>
<form action="/?task_id={{task_id}}&next=true" method="POST">
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" value="s.baniya.np@gmail.com" required><br><br>
<label for="age">Age:</label><br>
<input type="number" id="age" name="age" value="18" required><br><br>
<label for="gender">Gender:</label><br>
<select id="gender" name="gender" required>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`
bt, err := os.ReadFile("webroot/form.html")
if err != nil {
return v2.Result{Error: err, Ctx: ctx}
}
parser := jet.NewWithMemory(jet.WithDelims("{{", "}}"))
rs, err := parser.ParseTemplate(template, map[string]any{
rs, err := parser.ParseTemplate(string(bt), map[string]any{
"task_id": ctx.Value("task_id"),
})
if err != nil {
return v2.Result{Error: err, Ctx: ctx}
}
ctx = context.WithValue(ctx, consts.ContentType, consts.TypeHtml)
return v2.Result{Data: []byte(rs), Ctx: ctx}
data := map[string]any{
"content": rs,
}
bt, _ = json.Marshal(data)
return v2.Result{Data: bt, Ctx: ctx}
}
func NodeA(ctx context.Context, payload json.RawMessage) v2.Result {
@@ -84,18 +63,26 @@ func NodeC(ctx context.Context, payload json.RawMessage) v2.Result {
}
func Result(ctx context.Context, payload json.RawMessage) v2.Result {
bt, err := os.ReadFile("webroot/result.html")
if err != nil {
return v2.Result{Error: err, Ctx: ctx}
}
var data map[string]any
if err := json.Unmarshal(payload, &data); err != nil {
return v2.Result{Error: err, Ctx: ctx}
}
if templateFile, ok := data["html_content"].(string); ok {
if bt != nil {
parser := jet.NewWithMemory(jet.WithDelims("{{", "}}"))
rs, err := parser.ParseTemplate(templateFile, data)
rs, err := parser.ParseTemplate(string(bt), data)
if err != nil {
return v2.Result{Error: err, Ctx: ctx}
}
ctx = context.WithValue(ctx, consts.ContentType, consts.TypeHtml)
return v2.Result{Data: []byte(rs), Ctx: ctx}
data := map[string]any{
"content": rs,
}
bt, _ := json.Marshal(data)
return v2.Result{Data: bt, Ctx: ctx}
}
return v2.Result{Data: payload, Ctx: ctx}
}

View File

@@ -7,7 +7,7 @@
</head>
<body>
<h1>Enter Your Information</h1>
<form action="/form" method="POST">
<form action="/?task_id={{task_id}}&next=true" method="POST">
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" value="s.baniya.np@gmail.com" required><br><br>
@@ -24,4 +24,4 @@
<input type="submit" value="Submit">
</form>
</body>
</html>
</html>

View File

@@ -94,7 +94,7 @@
}
// Fetch the task result
const taskID = new URLSearchParams(window.location.search).get('taskID'); // Get taskID from URL
const taskID = new URLSearchParams(window.location.search).get('task_id'); // Get taskID from URL
if (taskID) {
fetch(`/task-result?taskID=${taskID}`)
.then(response => response.json())
@@ -111,13 +111,13 @@
<tr>
<th>Task ID</th>
<th>Status</th>
<th>Timestamp</th>
<th>UpdatedAt</th>
<th>Result Data</th>
</tr>
<tr>
<td>${taskID}</td>
<td class="${getStatusClass(data.Result.Status)}">${data.Result.Status}</td>
<td>${formatDate(data.Result.Timestamp)}</td>
<td>${formatDate(data.Result.UpdatedAt)}</td>
<td><pre>${JSON.stringify(data.Result.Result.Data, null, 2)}</pre></td>
</tr>
</table>
@@ -129,7 +129,7 @@
<tr>
<th>Node ID</th>
<th>Status</th>
<th>Timestamp</th>
<th>UpdatedAt</th>
<th>Node Result Data</th>
</tr>
`;
@@ -140,7 +140,7 @@
<tr>
<td>${node.NodeID}</td>
<td class="${getStatusClass(node.Status)}">${node.Status}</td>
<td>${formatDate(node.Timestamp)}</td>
<td>${formatDate(node.UpdatedAt)}</td>
<td><pre>${JSON.stringify(node.Result.Data, null, 2)}</pre></td>
</tr>
`;