//create a new instance of the server based on the env vars and the image creator. Return them to be tested func doSetup(port int) (*server.Server, string, error) { imageCreator, err := kiln.NewImageCreatorFromEnv() if err != nil { return nil, "", err } podSpecProvider, err := kiln.NewPodSpecIoFromEnv() if err != nil { return nil, "", err } baseHost := fmt.Sprintf("http://localhost:%d", port) testServer := server.NewServer(imageCreator, podSpecProvider) //start server in the background go func() { //start the server and produce it to the start channel testServer.Start(port, 10*time.Second) }() //wait for it to start hostBase := fmt.Sprintf("%s/imagespaces", baseHost) started := false //wait for host to start for 10 seconds for i := 0; i < 20; i++ { host := fmt.Sprintf("localhost:%d", port) conn, err := net.Dial("tcp", host) //done waiting, continue if err == nil { conn.Close() started = true break } time.Sleep(500 * time.Millisecond) } if !started { return nil, "", errors.New("Server did not start") } return testServer, hostBase, nil }
func main() { portString := os.Getenv("PORT") if portString == "" { kiln.LogError.Fatal("You must specifiy the PORT environment variable") } port, err := strconv.Atoi(portString) if err != nil { kiln.LogError.Fatal("You must specify a valid integer for the PORT value") } timeoutString := os.Getenv("SHUTDOWN_TIMEOUT") if timeoutString == "" { kiln.LogError.Fatal("You must specifiy the SHUTDOWN_TIMEOUT environment variable") } timeout, err := strconv.Atoi(timeoutString) if err != nil { kiln.LogError.Fatal("You must specify a valid integer for the SHUTDOWN_TIMEOUT value") } imageCreator, err := kiln.NewImageCreatorFromEnv() //we should die here if we're unable to start if err != nil { kiln.LogError.Fatalf("Unable to create image creator %s", err) } podSpec, err := kiln.NewPodSpecIoFromEnv() if err != nil { kiln.LogError.Fatalf("Unable to create pod spec provider. Error is %s", err) } //start the reaper process in the background if os.Getenv("NO_REAP") == "" { reaperIntervalString := os.Getenv("REAP_INTERVAL") if reaperIntervalString == "" { kiln.LogError.Fatal("You must specifiy the REAP_INTERVAL environment variable") } reaperInterval, err := strconv.Atoi(reaperIntervalString) if err != nil { kiln.LogError.Fatal("You must specify a valid integer for the REAP_INTERVAL value") } reaperMinAgeString := os.Getenv("REAP_MIN_AGE") if reaperMinAgeString == "" { kiln.LogError.Fatal("You must specifiy the REAP_MIN_AGE environment variable") } reaperMinAge, err := strconv.Atoi(reaperMinAgeString) if err != nil { kiln.LogError.Fatal("You must specify a valid integer for the REAP_MIN_AGE value") } minTime := time.Duration(reaperMinAge) * time.Second reapInterval := time.Duration(reaperInterval) * time.Second kiln.LogInfo.Printf("Starting background reaper process. Reper will remove images older than %f second, and will run every %f seconds ", minTime.Seconds(), reapInterval.Seconds()) //start the reap interval go kiln.ReapForever(minTime, imageCreator, reapInterval) } kiln.LogInfo.Printf("Successfully configured server and validated configuration. Starting server.") server := server.NewServer(imageCreator, podSpec) server.Start(port, time.Duration(timeout)*time.Second) }