// gceDeployHandler conditionally returns an http.Handler for a GCE launcher, // configured to run at /prefix/ (the trailing slash can be omitted). // If CAMLI_GCE_CLIENTID is not set, the launcher-config.json file, if present, // is used instead of environment variables to initialize the launcher. If a // launcher isn't enabled, gceDeployHandler returns nil. If another error occurs, // log.Fatal is called. func gceDeployHandler(prefix string) http.Handler { hostPort, err := netutil.HostPort("https://" + *httpsAddr) if err != nil { hostPort = "camlistore.org:443" } var gceh http.Handler if e := os.Getenv("CAMLI_GCE_CLIENTID"); e != "" { gceh, err = gce.NewDeployHandler(hostPort, prefix) } else { config := filepath.Join(*root, "launcher-config.json") if _, err := os.Stat(config); err != nil { if os.IsNotExist(err) { return nil } log.Fatalf("Could not stat launcher-config.json: %v", err) } gceh, err = gce.NewDeployHandlerFromConfig(hostPort, prefix, config) } if err != nil { log.Fatalf("Error initializing gce deploy handler: %v", err) } pageBytes, err := ioutil.ReadFile(filepath.Join(*root, "tmpl", "page.html")) if err != nil { log.Fatalf("Error initializing gce deploy handler: %v", err) } if err := gceh.(*gce.DeployHandler).AddTemplateTheme(string(pageBytes)); err != nil { log.Fatalf("Error initializing gce deploy handler: %v", err) } log.Printf("Starting Camlistore launcher on https://%s%s", hostPort, prefix) return gceh }
// gceDeployHandler returns an http.Handler for a GCE launcher, // configured to run at /prefix/ (the trailing slash can be omitted). // The launcher is not initialized if: // - in production, the launcher-config.json file is not found in the relevant bucket // - neither CAMLI_GCE_CLIENTID is set, nor launcher-config.json is found in the // camlistore server config dir. func gceDeployHandler(ctx context.Context, prefix string) (*gce.DeployHandler, error) { var hostPort string var err error scheme := "https" if inProd { hostPort = "camlistore.org:443" } else { addr := *httpsAddr if *devMode && *httpsAddr == "" { addr = *httpAddr scheme = "http" } hostPort, err = netutil.ListenHostPort(addr) if err != nil { // the deploy handler needs to know its own // hostname or IP for the oauth2 callback. return nil, fmt.Errorf("invalid -https flag: %v", err) } } config, err := gceDeployHandlerConfig(ctx) if config == nil { return nil, err } gceh, err := gce.NewDeployHandlerFromConfig(hostPort, prefix, config) if err != nil { return nil, fmt.Errorf("NewDeployHandlerFromConfig: %v", err) } pageBytes, err := ioutil.ReadFile(filepath.Join(*root, "tmpl", "page.html")) if err != nil { return nil, err } if err := gceh.AddTemplateTheme(string(pageBytes)); err != nil { return nil, fmt.Errorf("AddTemplateTheme: %v", err) } gceh.SetScheme(scheme) log.Printf("Starting Camlistore launcher on %s://%s%s", scheme, hostPort, prefix) return gceh, nil }