-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (49 loc) · 1.26 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"github.com/gorilla/mux"
)
type Route struct {
Path string `json:"path"`
Target string `json:"target"`
}
var routes []Route
func main() {
loadRoutesFromJSON("routes.json")
r := mux.NewRouter()
for _, route := range routes {
fmt.Println(route.Path, route.Target)
// addReverseProxyRoute(r, route.Path, route.Target)
}
port := 8080
fmt.Printf("Reverse proxy server is running on port %d\n", port)
http.ListenAndServe(fmt.Sprintf(":%d", port), r)
}
func loadRoutesFromJSON(filename string) {
file, err := os.Open(filename)
if err != nil {
log.Fatalf("Error opening JSON file: %v", err)
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&routes)
if err != nil {
log.Fatalf("Error decoding JSON: %v", err)
}
}
func addReverseProxyRoute(r *mux.Router, path string, targetURL string) {
target, _ := url.Parse(targetURL)
r.PathPrefix(path).Handler(http.StripPrefix(path, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
proxy := httputil.NewSingleHostReverseProxy(target)
req.URL.Host = target.Host
req.URL.Scheme = target.Scheme
req.Header.Set("X-Real-IP", req.RemoteAddr)
proxy.ServeHTTP(w, req)
})))
}