This repository was archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
108 lines (89 loc) · 2.72 KB
/
endpoint.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"reflect"
"strings"
)
// curl -X GET 'http://localhost:4001/api/products'
// curl -X GET 'http://localhost:4001/api/products?id=1&sub=4'
func getHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
urlParts := strings.Split(r.RequestURI, "?")
endpoint := strings.Replace(urlParts[0], config.Vertex.Prefix, "", -1)
// extracts query params
requestQueryParams := make(map[string]string)
for param := range r.URL.Query() {
if reflect.TypeOf(param).Kind() == reflect.String {
requestQueryParams[param] = r.URL.Query().Get(param)
}
}
// convert query params into json
requestQueryParamsJSON, err := json.Marshal(r.URL.Query())
if err != nil {
log.Println(err)
}
// read file
source, err := ioutil.ReadFile(restAPIRoutes[endpoint+":get"].Filepath)
if err != nil {
panic(err)
}
// replaces unsupported methods
routeSource := transcode(string(source))
// executes script
vm.Set("queryParams", string(requestQueryParamsJSON))
response, err := vm.Run(routeSource)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(response.String()))
return
}
// curl -X POST 'http://localhost:4001/api/products' --data '{"username":"xyz","password":"xyz"}'
// curl -X PUT 'http://localhost:4001/api/products' --data '{"username":"xyz","password":"xyz"}'
// curl -X DELETE 'http://localhost:4001/api/products' --data '{"username":"xyz","password":"xyz"}'
func postPutDeleteHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
urlParts := strings.Split(r.RequestURI, "?")
endpoint := strings.Replace(urlParts[0], config.Vertex.Prefix, "", -1)
// extracts query params
requestQueryParams := make(map[string]string)
for param := range r.URL.Query() {
if reflect.TypeOf(param).Kind() == reflect.String {
requestQueryParams[param] = r.URL.Query().Get(param)
}
}
// convert query params into json
requestQueryParamsJSON, err := json.Marshal(r.URL.Query())
if err != nil {
log.Println(err)
}
// read file
source, err := ioutil.ReadFile(restAPIRoutes[endpoint+":post"].Filepath)
if err != nil {
panic(err)
}
// replaces unsupported methods
routeSource := transcode(string(source))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
// executes script
vm.Set("body", string(body))
vm.Set("queryParams", string(requestQueryParamsJSON))
response, err := vm.Run(routeSource)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(response.String()))
return
}