| 47 | } |
| 48 | |
| 49 | func New(conf *config.Config) *Proxy { |
| 50 | k := kite.New(Name, Version) |
| 51 | k.Config = conf |
| 52 | |
| 53 | p := &Proxy{ |
| 54 | Kite: k, |
| 55 | kites: make(map[string]url.URL), |
| 56 | readyC: make(chan bool), |
| 57 | closeC: make(chan bool), |
| 58 | mux: http.NewServeMux(), |
| 59 | } |
| 60 | |
| 61 | // third part kites are going to use this to register themself to |
| 62 | // proxy-kite and get a proxy url, which they use for register to kontrol. |
| 63 | p.Kite.HandleFunc("register", p.handleRegister) |
| 64 | |
| 65 | // create our websocketproxy http.handler |
| 66 | |
| 67 | p.websocketProxy = &websocketproxy.WebsocketProxy{ |
| 68 | Backend: p.backend, |
| 69 | Upgrader: &websocket.Upgrader{ |
| 70 | ReadBufferSize: 4096, |
| 71 | WriteBufferSize: 4096, |
| 72 | CheckOrigin: func(r *http.Request) bool { |
| 73 | // TODO: change this to publicdomain and also kites should add them to |
| 74 | return true |
| 75 | }, |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | p.httpProxy = &httputil.ReverseProxy{ |
| 80 | Director: p.director, |
| 81 | } |
| 82 | |
| 83 | p.mux.Handle("/", k) |
| 84 | p.mux.Handle("/proxy/", p) |
| 85 | |
| 86 | // OnDisconnect is called whenever a kite is disconnected from us. |
| 87 | k.OnDisconnect(func(r *kite.Client) { |
| 88 | k.Log.Info("Removing kite Id '%s' from proxy. It's disconnected", r.Kite.ID) |
| 89 | delete(p.kites, r.Kite.ID) |
| 90 | }) |
| 91 | |
| 92 | return p |
| 93 | } |
| 94 | |
| 95 | // ServeHTTP implements the http.Handler interface. |
| 96 | func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |