]> Sergey Matveev's repositories - godlighty.git/blob - rc/example.cfg/proxied-host.go
Small comments
[godlighty.git] / rc / example.cfg / proxied-host.go
1 // Reverse proxy
2
3 package cfg
4
5 import (
6         "fmt"
7         "io"
8         "net/http"
9
10         "go.stargrave.org/godlighty"
11 )
12
13 func init() {
14         host := "some-proxied-host.com"
15         godlighty.Hosts[host] = &godlighty.HostCfg{
16                 Hooks: []godlighty.Hook{
17                         func(w http.ResponseWriter, r *http.Request) bool {
18                                 r.URL.Scheme = "http"
19                                 r.URL.Host = "[dead::beaf]"
20                                 r.RequestURI = ""
21                                 r.Host = host
22                                 resp, err := http.DefaultClient.Do(r)
23                                 if err != nil {
24                                         fmt.Printf("%s %s \"%s %+q %s\" %d \"%s\" \"%s\"\n",
25                                                 r.RemoteAddr, host, r.Method, godlighty.PathWithQuery(r.URL),
26                                                 r.Proto, http.StatusBadGateway, err.Error(),
27                                                 r.Header.Get("User-Agent"),
28                                         )
29                                         http.Error(w, err.Error(), http.StatusBadGateway)
30                                         return true
31                                 }
32                                 for k, vs := range resp.Header {
33                                         for _, v := range vs {
34                                                 w.Header().Add(k, v)
35                                         }
36                                 }
37                                 w.WriteHeader(resp.StatusCode)
38                                 size, _ := io.Copy(w, resp.Body)
39                                 resp.Body.Close()
40                                 fmt.Printf("%s %s \"%s %+q %s\" %d %d \"%s\"\n",
41                                         r.RemoteAddr, host, r.Method, godlighty.PathWithQuery(r.URL),
42                                         r.Proto, resp.StatusCode, size,
43                                         r.Header.Get("User-Agent"),
44                                 )
45                                 return true
46                         },
47                 },
48         }
49 }