mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-24 07:23:13 +08:00
update: dependencies
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/oarkflow/jet"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
@@ -23,7 +24,7 @@ type Node interface {
|
||||
|
||||
type Result struct {
|
||||
ConditionStatus string
|
||||
Message string
|
||||
Payload json.RawMessage
|
||||
Error error
|
||||
}
|
||||
|
||||
@@ -40,8 +41,28 @@ type PageNode struct {
|
||||
}
|
||||
|
||||
func (n *PageNode) ProcessTask(ctx context.Context, task *Task) Result {
|
||||
contentWithTaskID := strings.ReplaceAll(n.Content, "{{taskID}}", task.ID)
|
||||
return Result{Message: contentWithTaskID}
|
||||
var data map[string]any
|
||||
if task.Payload != nil {
|
||||
err := json.Unmarshal(task.Payload, &data)
|
||||
if err != nil {
|
||||
return Result{Error: err}
|
||||
}
|
||||
}
|
||||
if data == nil {
|
||||
data = make(map[string]any)
|
||||
}
|
||||
parser := jet.NewWithMemory(jet.WithDelims("{{", "}}"))
|
||||
data["taskID"] = task.ID
|
||||
tmpl := fmt.Sprintf("%s<br><h1>Request</h1><p>{{request_data|writeJson}}</p>", n.Content)
|
||||
rs, err := parser.ParseTemplate(tmpl, map[string]any{
|
||||
"request_data": data,
|
||||
"taskID": task.ID,
|
||||
})
|
||||
fmt.Println(rs, err, data)
|
||||
if err != nil {
|
||||
return Result{Error: err}
|
||||
}
|
||||
return Result{Payload: []byte(rs)}
|
||||
}
|
||||
|
||||
func (n *PageNode) GetNodeType() string {
|
||||
@@ -153,8 +174,7 @@ func processNode(w http.ResponseWriter, r *http.Request, task *Task, tm *TaskMan
|
||||
result := node.ProcessTask(context.Background(), task)
|
||||
log.Printf("Node %s processed. Result ConditionStatus: %s", task.CurrentNodeID, result.ConditionStatus)
|
||||
if node.GetNodeType() == PageType {
|
||||
contentWithTaskID := strings.ReplaceAll(result.Message, "{{taskID}}", task.ID)
|
||||
fmt.Fprintf(w, contentWithTaskID)
|
||||
fmt.Fprintf(w, string(result.Payload))
|
||||
return
|
||||
}
|
||||
if result.Error != nil {
|
||||
@@ -268,8 +288,7 @@ func verifyHandler(w http.ResponseWriter, r *http.Request, tm *TaskManager) {
|
||||
http.Error(w, result.Error.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
contentWithTaskID := strings.ReplaceAll(result.Message, "{{taskID}}", task.ID)
|
||||
fmt.Fprintf(w, contentWithTaskID)
|
||||
fmt.Fprintf(w, string(result.Payload))
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -284,14 +303,14 @@ func main() {
|
||||
Func: func(task *Task) Result {
|
||||
var inputs map[string]string
|
||||
if err := json.Unmarshal(task.Payload, &inputs); err != nil {
|
||||
return Result{ConditionStatus: "customRegistration", Message: "Invalid input format"}
|
||||
return Result{ConditionStatus: "customRegistration", Payload: []byte("Invalid input format")}
|
||||
}
|
||||
|
||||
email, phone := inputs["email"], inputs["phone"]
|
||||
if !isValidEmail(email) || !isValidPhone(phone) {
|
||||
return Result{
|
||||
ConditionStatus: "customRegistration",
|
||||
Message: "Invalid email or phone number. Please try again.",
|
||||
Payload: []byte("Invalid email or phone number. Please try again."),
|
||||
}
|
||||
}
|
||||
return Result{ConditionStatus: "checkManualVerification"}
|
||||
@@ -302,7 +321,7 @@ func main() {
|
||||
Func: func(task *Task) Result {
|
||||
var inputs map[string]string
|
||||
if err := json.Unmarshal(task.Payload, &inputs); err != nil {
|
||||
return Result{ConditionStatus: "customRegistration", Message: "Invalid input format"}
|
||||
return Result{ConditionStatus: "customRegistration", Payload: []byte("Invalid input format")}
|
||||
}
|
||||
city := inputs["city"]
|
||||
if city != "Kathmandu" {
|
||||
|
||||
1
go.mod
1
go.mod
@@ -20,6 +20,7 @@ require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/oarkflow/jet v0.0.4 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.60.1 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -22,6 +22,8 @@ github.com/oarkflow/errors v0.0.6 h1:qTBzVblrX6bFbqYLfatsrZHMBPchOZiIE3pfVzh1+k8
|
||||
github.com/oarkflow/errors v0.0.6/go.mod h1:UETn0Q55PJ+YUbpR4QImIoBavd6QvJtyW/oeTT7ghZM=
|
||||
github.com/oarkflow/expr v0.0.11 h1:H6h+dIUlU+xDlijMXKQCh7TdE6MGVoFPpZU7q/dziRI=
|
||||
github.com/oarkflow/expr v0.0.11/go.mod h1:WgMZqP44h7SBwKyuGZwC15vj46lHtI0/QpKdEZpRVE4=
|
||||
github.com/oarkflow/jet v0.0.4 h1:rs0nTzodye/9zhrSX7FlR80Gjaty6ei2Ln0pmaUrdwg=
|
||||
github.com/oarkflow/jet v0.0.4/go.mod h1:YXIc47aYyx1xKpnmuz1Z9o88cxxa47r7X3lfUAxZ0Qg=
|
||||
github.com/oarkflow/json v0.0.13 h1:/ZKW924/v4U1ht34WY7rj/GC/qW9+10IiV5+MR2vO0A=
|
||||
github.com/oarkflow/json v0.0.13/go.mod h1:S5BZA4/rM87+MY8mFrga3jISzxCL9RtLE6xHSk63VxI=
|
||||
github.com/oarkflow/xid v1.2.5 h1:6RcNJm9+oZ/B647gkME9trCzhpxGQaSdNoD56Vmkeho=
|
||||
|
||||
Reference in New Issue
Block a user