-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (75 loc) · 2.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/google/cel-go/cel"
)
// Request structure to handle incoming POST data with expression and input
type Request struct {
Expression string `json:"expression"`
Input map[string]interface{} `json:"input"`
}
// Response structure to return result
type Response struct {
Result interface{} `json:"result"`
Error string `json:"error,omitempty"`
}
// evaluateExpression evaluates the expression using cel-go with input data
func evaluateExpression(expression string, input map[string]interface{}) (interface{}, error) {
// Create a new CEL environment
env, err := cel.NewEnv()
if err != nil {
return nil, err
}
// Parse the expression
parsedExpr, issues := env.Parse(expression)
if issues != nil && issues.Err() != nil {
return nil, issues.Err()
}
// Create an evaluator for the parsed expression
prg, err := env.Program(parsedExpr)
if err != nil {
return nil, err
}
// Evaluate the expression with the input data as context
result, _, err := prg.Eval(input)
if err != nil {
return nil, err
}
return result, nil
}
// handler function to process incoming POST requests with expression and input
func handler(w http.ResponseWriter, r *http.Request) {
// Ensure the request method is POST
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// Parse the JSON request body
var req Request
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Evaluate the expression with the provided input
result, err := evaluateExpression(req.Expression, req.Input)
response := Response{Result: result}
if err != nil {
// If error, return it in the response
response.Error = err.Error()
}
// Set content type as JSON and return the response
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
gi
func main() {
// Set up the HTTP server
http.HandleFunc("/api/evaluate", handler)
// Start the server on port 3002
fmt.Println("Server started at http://localhost:3002")
log.Fatal(http.ListenAndServe(":3002", nil))
}