forked from geosoft1/reverseproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (71 loc) · 2.28 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
// simple http reverse proxy
// Copyright (C) 2017 geosoft1 [email protected]
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
)
func NewReverseProxy(target string) *httputil.ReverseProxy {
return httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "http",
Host: target,
})
}
func Handle(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("request:", r.RemoteAddr, "want", r.RequestURI)
//Many webservers are configured to not serve pages if a request doesn’t appear from the same host.
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With")
p.ServeHTTP(w, r)
}
}
var Config map[string]interface{}
func main() {
log.Print("init logger")
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
pwd, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
os.Exit(1)
}
log.Print("load configuration")
f, err := os.Open(filepath.ToSlash(pwd + "/conf.json"))
if err != nil {
log.Println(err.Error())
return
}
if err := json.NewDecoder(f).Decode(&Config); err != nil {
log.Println(err.Error())
return
}
log.Print("init routes")
for Path, Target := range Config["routes"].(map[string]interface{}) {
// avoid add comments as route
if Path != "#" {
http.HandleFunc(Path, Handle(NewReverseProxy(Target.(string))))
log.Printf("%s > %s", Path, Target)
}
}
Address := fmt.Sprintf("%s:%s", Config["ip"], Config["port"])
log.Print("start listening on " + Address)
http.ListenAndServe(Address, nil)
}