(proxyPath string)
| 12 | ) |
| 13 | |
| 14 | func NewMultipleHostReverseProxy(proxyPath string) *httputil.ReverseProxy { |
| 15 | cleanReg := regexp.MustCompile(`:\d*`) |
| 16 | lastUpdate := time.Now() |
| 17 | proxyMap := &proxy.ProxyMap{} |
| 18 | |
| 19 | /* |
| 20 | Yes, we are reading a file here and use it as a cheap database |
| 21 | I expect to do a few requests every few hours so we don't need anything |
| 22 | better right now. <insert dealwithit.gif> |
| 23 | */ |
| 24 | |
| 25 | err := proxyMap.LoadFromConfig(proxyPath) |
| 26 | if err != nil { |
| 27 | log.Infoln("Could not read config file, starting with empty state.", err) |
| 28 | log.Infoln(proxyMap) |
| 29 | } |
| 30 | |
| 31 | info, err := os.Stat(proxyPath) |
| 32 | if err != nil { |
| 33 | log.Debugln("Could not get last modification time, setting to now") |
| 34 | } else { |
| 35 | lastUpdate = info.ModTime() |
| 36 | } |
| 37 | |
| 38 | director := func(req *http.Request) { |
| 39 | log.Debugln("Incoming request from:", req.Host) |
| 40 | log.Debugln("Getting last modifcation time") |
| 41 | |
| 42 | info, err := os.Stat(proxyPath) |
| 43 | if err != nil { |
| 44 | log.Debugln("Could not get last modification time, breaking") |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | log.Debugln("Modifcation time is", info.ModTime()) |
| 49 | log.Debugln("Our last update was", lastUpdate) |
| 50 | |
| 51 | if info.ModTime().After(lastUpdate) { |
| 52 | err := proxyMap.LoadFromConfig(proxyPath) |
| 53 | if err != nil { |
| 54 | log.Infoln("Could not read config file, starting with empty state.", err) |
| 55 | } |
| 56 | log.Println("Modification time was after our last, updating proxy map:", proxyMap) |
| 57 | lastUpdate = time.Now() |
| 58 | } |
| 59 | |
| 60 | domain := cleanReg.ReplaceAllString(req.Host, "") |
| 61 | m := *proxyMap |
| 62 | if dom, ok := m[domain]; ok { |
| 63 | p := path.Join(dom.Path, req.URL.Path) |
| 64 | scheme := dom.Scheme |
| 65 | h := dom.Host |
| 66 | req.URL.Scheme = scheme |
| 67 | req.URL.Host = h |
| 68 | req.URL.Path = p |
| 69 | log.Debugln("Request string", req.URL.String()) |
| 70 | log.Debugln("Raw Query", req.URL.RawQuery) |
| 71 | log.Debugln("Host", h) |
no test coverage detected