-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (132 loc) · 3.64 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"gopkg.in/yaml.v2"
)
type anyService struct {
ID string
name string
baseURL string
secretKey string
secretValue string
}
var pointToAddressService anyService
var distanceService anyService
type anyClient struct {
ID string
name string
clientToken string
pathPrefix string
}
var myClient anyClient = anyClient{
ID: "01",
name: "client one",
clientToken: "",
pathPrefix: "api1/v1/",
}
func readConfig() {
//todo : yaml file has not been read correctly
confContent, err := ioutil.ReadFile("config.yml")
if err != nil {
log.Fatal(err)
}
conf := make(map[string]anyService)
err2 := yaml.Unmarshal(confContent, &conf)
if err2 != nil {
log.Fatal(err2)
}
pointToAddressService = conf["pointToAddressCon"]
distanceService = conf["distanceCon"]
fmt.Println(pointToAddressService)
fmt.Println(distanceService)
/*
pointToAddressService.ID = "01"
pointToAddressService.name = "Address API"
pointToAddressService.baseURL = "https://api.neshan.org/v5/reverse"
pointToAddressService.secretKey = "Api-Key"
distanceService.ID = "02"
distanceService.name = "Distance API"
distanceService.baseURL = "https://api.neshan.org/v1/distance-matrix"
distanceService.secretKey = "Api-Key"
*/
}
func callService(authKey string, authValue string, serviceID string, baseU string, params string) (out string) {
url := baseU + "?" + params
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add(authKey, authValue)
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
out = string(body)
return
}
func getSecret(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/secret" {
http.Error(w, "404 not found.", http.StatusNotFound)
return
}
switch r.Method {
case "GET":
http.ServeFile(w, r, "secret.html")
case "POST":
// Call ParseForm() to parse the raw query and update r.PostForm and r.Form.
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
fmt.Fprintf(w, "Post from website! r.PostFrom = %v\n", r.PostForm)
pointToAddressService.secretValue = r.FormValue("address_secret")
distanceService.secretValue = r.FormValue("distance_secret")
fmt.Fprintf(w, "Address API secret = %s\n", pointToAddressService.secretValue)
fmt.Fprintf(w, "Distance API secret = %s\n", distanceService.secretValue)
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
}
func clientHandler(w http.ResponseWriter, r *http.Request) {
var realPath string = strings.ReplaceAll(r.URL.Path, myClient.pathPrefix, "")
switch realPath {
case "/distance/":
{
w.WriteHeader(http.StatusOK)
w.Write([]byte(callService(distanceService.secretKey,
distanceService.secretValue,
distanceService.ID,
distanceService.baseURL,
r.URL.RawQuery)))
}
case "/address/":
{
w.WriteHeader(http.StatusOK)
w.Write([]byte(callService(pointToAddressService.secretKey,
pointToAddressService.secretValue,
pointToAddressService.ID,
pointToAddressService.baseURL,
r.URL.RawQuery)))
}
default:
{
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Asset not found\n"))
}
}
}
func main() {
http.HandleFunc("/secret", getSecret)
http.HandleFunc("/api1/v1/address/", clientHandler)
http.HandleFunc("/api1/v1/distance/", clientHandler)
readConfig()
fmt.Printf("Starting server for testing HTTP POST...\n")
if err := http.ListenAndServe(":11111", nil); err != nil {
log.Fatal(err)
}
}