コード例 #1
0
ファイル: demo.go プロジェクト: jnormore/go.grace
func main() {
	flag.Parse()
	gracehttp.Serve(
		&http.Server{Addr: *address0, Handler: newHandler("Zero  ")},
		&http.Server{Addr: *address1, Handler: newHandler("First ")},
		&http.Server{Addr: *address2, Handler: newHandler("Second")},
	)
}
コード例 #2
0
ファイル: main.go プロジェクト: ryanttb/encore
func main() {
	var listenAddress, serverUrl, taskTemplatesPath, statsTemplatesPath, staticPath, cubeCollectionType, logfile, geoipDatabase string
	flag.BoolVar(&debugMode, "debug", false, "Enable parsing of cmh- debug parameters in requests")
	flag.StringVar(&listenAddress, "listen_address", "127.0.0.1:8080", "")
	flag.StringVar(&serverUrl, "server_url", "http://localhost:8080", "URL that clients should use to contact this server.")
	flag.StringVar(&taskTemplatesPath, "task_templates_path", "task-templates", "Path to task templates")
	flag.StringVar(&statsTemplatesPath, "stats_templates_path", "stats-templates", "Path to stats templates")
	flag.StringVar(&staticPath, "static_path", "static", "Path to static content to serve")
	flag.StringVar(&cubeCollectionType, "cube_collection_type", "encore", "Use this label for statistics we send to Cube")
	flag.StringVar(&logfile, "logfile", "", "Write logs to this file instead of stdout")
	flag.StringVar(&geoipDatabase, "geoip_database", "/usr/share/GeoIP/GeoIP.dat", "Path of GeoIP database")
	flag.Parse()

	printVersionIfAsked()

	if logfile != "" {
		f, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
		if err != nil {
			log.Fatalf("error opening logfile: %v", err)
		}
		defer f.Close()
		log.SetOutput(f)
	}

	log.Printf("starting")

	initMetrics()

	go cube.Run(cubeCollectionType)

	s := store.Open()
	defer s.Close()

	tasksServer := NewTaskServer(s, serverUrl, taskTemplatesPath, geoipDatabase)
	submissionServer := NewSubmissionServer(s)
	statsServer := NewStatsServer(s, statsTemplatesPath)

	mux := http.NewServeMux()
	mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(staticPath))))
	mux.Handle("/task.js", tasksServer)
	mux.Handle("/task.html", tasksServer)
	mux.Handle("/submit", submissionServer)
	mux.HandleFunc("/version", versionServer)
	mux.Handle("/stats/", statsServer)
	mux.HandleFunc("/stats/refer", refererRedirect)
	server := http.Server{
		Addr:    listenAddress,
		Handler: mux,
	}

	log.Printf("serving at %s", listenAddress)
	if err := gracehttp.Serve(&server); err != nil {
		panic(err)
	}

	log.Printf("exiting")
}
コード例 #3
0
ファイル: archrepo.go プロジェクト: daaku/archrepo
func main() {
	flag.Parse()
	err := gracehttp.Serve(
		&http.Server{Addr: *repoAddr, Handler: http.FileServer(http.Dir(*repo))},
		&http.Server{Addr: *adminAddr, Handler: adminHandler()},
	)
	if err != nil {
		log.Fatal(err)
	}
}
コード例 #4
0
ファイル: testserver.go プロジェクト: jnormore/go.grace
func main() {
	var httpAddr, httpsAddr string
	flag.StringVar(&httpAddr, "http", ":48560", "http address to bind to")
	flag.StringVar(&httpsAddr, "https", ":48561", "https address to bind to")
	flag.Parse()

	// we have self signed certs
	http.DefaultTransport = &http.Transport{
		DisableKeepAlives: true,
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}

	err := flag.Set("gracehttp.log", "false")
	if err != nil {
		log.Fatalf("Error setting gracehttp.log: %s", err)
	}

	// print json to stderr once we can successfully connect to all three
	// addresses. the ensures we only print the line once we're ready to serve.
	go func() {
		var wg sync.WaitGroup
		wg.Add(2)
		go wait(&wg, fmt.Sprintf("http://%s/sleep/?duration=0", httpAddr))
		go wait(&wg, fmt.Sprintf("https://%s/sleep/?duration=0", httpsAddr))
		wg.Wait()

		err = json.NewEncoder(os.Stderr).Encode(&response{Pid: os.Getpid()})
		if err != nil {
			log.Fatalf("Error writing startup json: %s", err)
		}
	}()

	err = gracehttp.Serve(
		&http.Server{Addr: httpAddr, Handler: newHandler()},
		httpsServer(httpsAddr),
	)
	if err != nil {
		log.Fatalf("Error in gracehttp.Serve: %s", err)
	}
}