NewWithConfig builds a new kite value for the given configuration.
(name, version string, cfg *config.Config)
| 166 | |
| 167 | // NewWithConfig builds a new kite value for the given configuration. |
| 168 | func NewWithConfig(name, version string, cfg *config.Config) *Kite { |
| 169 | if name == "" { |
| 170 | panic("kite: name cannot be empty") |
| 171 | } |
| 172 | |
| 173 | if digits := strings.Split(version, "."); len(digits) != 3 { |
| 174 | panic("kite: version must be 3-digits semantic version") |
| 175 | } |
| 176 | |
| 177 | kiteID := uuid.Must(uuid.NewV4()) |
| 178 | |
| 179 | l, setlevel := newLogger(name) |
| 180 | |
| 181 | kClient := &kontrolClient{ |
| 182 | readyConnected: make(chan struct{}), |
| 183 | readyRegistered: make(chan struct{}), |
| 184 | registerChan: make(chan *url.URL, 1), |
| 185 | } |
| 186 | |
| 187 | k := &Kite{ |
| 188 | Config: cfg, |
| 189 | Log: l, |
| 190 | SetLogLevel: setlevel, |
| 191 | Authenticators: make(map[string]func(*Request) error), |
| 192 | handlers: make(map[string]*Method), |
| 193 | kontrol: kClient, |
| 194 | name: name, |
| 195 | version: version, |
| 196 | Id: kiteID.String(), |
| 197 | readyC: make(chan bool), |
| 198 | closeC: make(chan bool), |
| 199 | heartbeatC: make(chan *heartbeatReq, 1), |
| 200 | muxer: mux.NewRouter(), |
| 201 | } |
| 202 | |
| 203 | if cfg != nil && cfg.UseWebRTC { |
| 204 | k.WebRTCHandler = NewWebRCTHandler() |
| 205 | } |
| 206 | |
| 207 | // All sockjs communication is done through this endpoint.. |
| 208 | k.muxer.PathPrefix("/kite").Handler(sockjs.NewHandler("/kite", *cfg.SockJS, k.sockjsHandler)) |
| 209 | |
| 210 | // Add useful debug logs |
| 211 | k.OnConnect(func(c *Client) { k.Log.Debug("New session: %s", c.session.ID()) }) |
| 212 | k.OnFirstRequest(func(c *Client) { k.Log.Debug("Session %q is identified as %q", c.session.ID(), c.Kite) }) |
| 213 | k.OnDisconnect(func(c *Client) { k.Log.Debug("Kite has disconnected: %q", c.Kite) }) |
| 214 | k.OnRegister(k.updateAuth) |
| 215 | |
| 216 | // Every kite should be able to authenticate the user from token. |
| 217 | // Tokens are granted by Kontrol Kite. |
| 218 | k.Authenticators["token"] = k.AuthenticateFromToken |
| 219 | |
| 220 | // A kite accepts requests with the same username. |
| 221 | k.Authenticators["kiteKey"] = k.AuthenticateFromKiteKey |
| 222 | |
| 223 | // Register default methods and handlers. |
| 224 | k.addDefaultHandlers() |
| 225 |
no test coverage detected