MCPcopy
hub / github.com/perkeep/perkeep / NewHandler

Function NewHandler

pkg/server/app/app.go:358–435  ·  view source on GitHub ↗

NewHandler creates a new handler from the given HandlerConfig. Two exceptions apply to the HandlerConfig documentation: NewHandler does not create default values for Prefix and ServerBaseURL. Prefix should be provided, and ServerBaseURL might be needed, depending on the other fields.

(cfg HandlerConfig)

Source from the content-addressed store, hash-verified

356// values for Prefix and ServerBaseURL. Prefix should be provided, and
357// ServerBaseURL might be needed, depending on the other fields.
358func NewHandler(cfg HandlerConfig) (*Handler, error) {
359 if cfg.Program == "" {
360 return nil, fmt.Errorf("app: could not initialize Handler: empty Program")
361 }
362 name := cfg.Program
363
364 if cfg.Prefix == "" {
365 return nil, fmt.Errorf("app: could not initialize Handler for %q: empty Prefix", name)
366 }
367
368 listen, backendURL, apiHost := cfg.Listen, cfg.BackendURL, cfg.APIHost
369 var err error
370 if listen == "" {
371 serverListen := cfg.ServerListen
372 if serverListen == "" {
373 if cfg.ServerBaseURL == "" {
374 return nil, fmt.Errorf(`app: could not initialize Handler for %q: "Listen", "ServerListen" and "ServerBaseURL" all undefined`, name)
375 }
376 parsedUrl, err := url.Parse(cfg.ServerBaseURL)
377 if err != nil {
378 return nil, fmt.Errorf("app: could not initialize Handler for %q: unparseable ServerBaseURL %q: %v", name, cfg.ServerBaseURL, err)
379 }
380 serverListen = parsedUrl.Host
381 }
382 listen, err = randListen(serverListen)
383 if err != nil {
384 return nil, err
385 }
386 }
387 if backendURL == "" {
388 if cfg.ServerBaseURL == "" {
389 return nil, fmt.Errorf(`app: could not initialize Handler for %q: neither "BackendURL" or "ServerBaseURL" defined`, name)
390 }
391 backendURL, err = baseURL(cfg.ServerBaseURL, listen)
392 if err != nil {
393 return nil, err
394 }
395 }
396 if apiHost == "" {
397 if cfg.ServerBaseURL == "" {
398 return nil, fmt.Errorf(`app: could not initialize Handler for %q: neither "APIHost" or "ServerBaseURL" defined`, name)
399 }
400 apiHost = cfg.ServerBaseURL + "/"
401 }
402
403 proxyURL, err := url.Parse(backendURL)
404 if err != nil {
405 return nil, fmt.Errorf("could not parse backendURL %q: %v", backendURL, err)
406 }
407
408 username, password := auth.RandToken(20), auth.RandToken(20)
409 camliAuth := username + ":" + password
410 basicAuth := auth.NewBasicAuth(username, password)
411 envVars := map[string]string{
412 "CAMLI_API_HOST": apiHost,
413 "CAMLI_AUTH": camliAuth,
414 "CAMLI_APP_LISTEN": listen,
415 }

Callers 1

setupHandlerMethod · 0.92

Calls 5

RandTokenFunction · 0.92
NewBasicAuthFunction · 0.92
randListenFunction · 0.85
ParseMethod · 0.80
baseURLFunction · 0.70

Tested by

no test coverage detected