Beispiel #1
0
// 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
}
Beispiel #2
0
func main() {
	flag.Parse()

	gce.DevHandler = *debug
	gceh, err := gce.NewDeployHandler(*host, "/launch/")
	if err != nil {
		log.Fatal(err)
	}
	if *logfile != "" {
		f, err := os.Create(*logfile)
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close() // lazy. no matter, just an example.
		gceh.(*gce.DeployHandler).SetLogger(log.New(f, "GCEDEPLOYER", log.LstdFlags))
	}

	http.HandleFunc("/static/piggy.gif", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, *piggy)
	})
	http.Handle("/launch/", &httputil.PrefixHandler{"/launch/", gceh})
	if *debug {
		if err := http.ListenAndServe(*host, nil); err != nil {
			log.Fatal(err)
		}
		return
	}
	if err := http.ListenAndServeTLS(*host, *cert, *key, nil); err != nil {
		log.Fatal(err)
	}
}