-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (56 loc) · 1.44 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
)
func main() {
var err error
port := int64(8080)
if len(os.Args) == 2 {
port, err = strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
log.Fatal("invalid port")
}
}
targetURL, err := url.Parse("https://proxy.evmos.org/cosmos")
if err != nil {
log.Fatal("Error parsing target URL:", err)
}
cosmosProxy := httputil.NewSingleHostReverseProxy(targetURL)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
url := r.URL.RequestURI()
if strings.Contains(url, "/v2/vesting/") {
address := strings.Replace(url, "/v2/vesting/", "", 1)
getURL := fmt.Sprintf("https://proxy.evmos.org/evmos/vesting/v2/balances/%s", address)
resp, err := http.Get(getURL)
if err != nil {
http.Error(w, "Failed to fetch from server", http.StatusBadGateway)
return
}
defer resp.Body.Close()
// Copy the response from the special URL to the client
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
if err != nil {
log.Println("Error copying response body:", err)
}
return
}
cosmosProxy.ServeHTTP(w, r)
},
)
// start the server on port 8080
fmt.Printf("Server is running on http://localhost:%d\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
// log the error if server fails to start
fmt.Println("Error starting server:", err)
}
}