-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (110 loc) · 2.51 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/sinha-abhishek/resttogremlin/gremlin"
"github.com/sinha-abhishek/resttogremlin/handlers"
)
var handlerMap map[string]handlers.HandlerConf
var client *gremlin.Client
var configurations *Configurations
func Init() (err error) {
configurations, err = GetConfigurations()
if err != nil {
log.Println(err)
return
}
handlerMap, err = handlers.GetHandlerConfigs()
if err != nil {
fmt.Println(err)
return
}
client = gremlin.NewClient(configurations.GremlinHosts)
//err = client.Connect()
if err != nil {
fmt.Println(err)
return
}
return
}
func test() {
handler := handlerMap["init"]
bMap := make(map[string]interface{})
bMap["uid"] = 1006
bMap["number2"] = "10030"
bMap["contact_name"] = "testnam2"
query := handler.Query
bindingMap := handler.Bindings
bindings := make(map[string]interface{})
for k, v := range bindingMap {
bindings[k] = bMap[v]
}
gr := client.NewGremlinRequest()
gr.SetQuery(query)
gr.AddBindings(bindings)
s, _ := client.SendRequest(gr)
fmt.Println(s)
}
func handleGremlinRequest(w http.ResponseWriter, r *http.Request) {
log.Println(strings.Split(r.URL.Path, "/")[2])
r.ParseForm()
// for key, values := range r.PostForm {
// // [...]
// // fmt.Println(key)
// // fmt.Println(values)
// }
method := strings.Split(r.URL.Path, "/")[2]
fmt.Println(method)
if handler, ok := handlerMap[method]; ok {
query := handler.Query
bindingMap := handler.Bindings
bindings := make(map[string]interface{})
requestMap := make(map[string]interface{})
for k := range r.PostForm {
requestMap[k] = r.PostFormValue(k)
}
for k, v := range bindingMap {
if rVal, ok := requestMap[v]; ok {
bindings[k] = rVal
} else {
http.Error(w, "values not sent : "+v, 400)
return
}
}
gr := client.NewGremlinRequest()
gr.SetQuery(query)
gr.AddBindings(bindings)
s, err2 := client.SendRequest(gr)
if err2 != nil {
http.Error(w, err2.Error(), 400)
return
}
fmt.Println(s)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(s); err != nil {
http.Error(w, "server error", 500)
panic(err)
}
} else {
http.Error(w, "method not found", 400)
return
}
}
func StartServer(port string) {
http.HandleFunc("/gremlin/", handleGremlinRequest)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Println(err)
panic(1)
}
}
func main() {
err := Init()
if err != nil {
return
}
StartServer(configurations.ServerPort)
}