-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (61 loc) · 1.8 KB
/
main.go
File metadata and controls
72 lines (61 loc) · 1.8 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
// Beispiel-Methode, die ausgeführt werden soll
func exampleMethod(params map[string]interface{}) string {
param := params["variable"].([]interface{})
return fmt.Sprintf("Hello, %s!", strings.Join(convertToStringSlice(param), ", "))
}
// Another example method
func anotherExampleMethod(params map[string]interface{}) string {
param := params["variable"].([]interface{})
return fmt.Sprintf("Goodbye, %s!", strings.Join(convertToStringSlice(param), ", "))
}
// Helper function to convert interface{} slice to string slice
func convertToStringSlice(slice []interface{}) []string {
strSlice := make([]string, len(slice))
for i, v := range slice {
strSlice[i] = fmt.Sprintf("%v", v)
}
return strSlice
}
// Handler-Funktion für die HTTP-Anfrage
func handler(w http.ResponseWriter, r *http.Request) {
var params map[string]interface{}
// JSON-Parameter auslesen
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(¶ms); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Methodenname holen
method, ok := params["method"].(string)
if !ok {
http.Error(w, "Missing 'method' parameter", http.StatusBadRequest)
return
}
// Ergebnis initialisieren
var result string
// Entscheiden, welche Methode aufgerufen werden soll
switch method {
case "exampleMethod":
result = exampleMethod(params)
case "anotherExampleMethod":
result = anotherExampleMethod(params)
default:
http.Error(w, "Invalid method", http.StatusBadRequest)
return
}
// Ergebnis als JSON zurückgeben
response := map[string]string{"result of go": result}
json.NewEncoder(w).Encode(response)
}
func main() {
http.HandleFunc("/execute", handler)
fmt.Println("Starting server on port 8080...")
http.ListenAndServe(":8080", nil)
}