-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.go
99 lines (86 loc) · 2.58 KB
/
http.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
// simple http reverse proxy
// Copyright (C) 2017-2019 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/>.
// +build http
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strings"
)
var configFile = flag.String("conf", "conf.json", "configuration file")
var httpAddress = flag.String("http", ":8080", "http address")
var verbose = flag.Bool("verbose", false, "explain what is being done")
var config map[string]interface{}
func NewReverseProxy(scheme, host string) *httputil.ReverseProxy {
return httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: scheme,
Host: host,
})
}
func Register(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if *verbose {
log.Printf("request %s%s", r.RemoteAddr, r.RequestURI)
}
// w.Header().Set("Access-Control-Allow-Origin", "*")
// w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With")
p.ServeHTTP(w, r)
}
}
func main() {
flag.Usage = func() {
fmt.Printf("usage: %s [options]\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
folder, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalln(err)
}
file, err := os.Open(filepath.Join(folder, *configFile))
if err != nil {
log.Fatalln(err)
}
if err := json.NewDecoder(file).Decode(&config); err != nil {
log.Fatalln(err)
}
for path, host := range config["routes"].(map[string]interface{}) {
log.Printf("%s -> %s", path, host)
if strings.HasPrefix(path, "#") {
// skip comments
continue
}
u, err := url.Parse(host.(string))
if err != nil {
// skip invalid hosts
log.Println(err)
continue
}
http.HandleFunc(path, Register(NewReverseProxy(u.Scheme, u.Host)))
}
log.Printf("start http server on %s", *httpAddress)
if err := http.ListenAndServe(*httpAddress, nil); err != nil {
log.Fatalln(err)
}
}