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