github.com/mattevans/edward@v1.9.2/edward/testdata/features/edward-test-expectenv/main.go (about)

     1  // A simple executable that stays runnning until an interrupt is received
     2  // Based on: https://gobyexample.com/signals
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  	"net/http"
     9  	"os"
    10  	"os/signal"
    11  	"time"
    12  )
    13  
    14  func handler(w http.ResponseWriter, r *http.Request) {
    15  	fmt.Fprintf(w, "Hello: %s!", r.URL.Path[1:])
    16  }
    17  
    18  func main() {
    19  	http.HandleFunc("/", handler)
    20  
    21  	applied := os.Getenv("APPLIED")
    22  	if applied != "YES" {
    23  		log.Fatal("Expected env variable APPLIED=YES, got:", applied)
    24  	}
    25  
    26  	port := os.Args[1]
    27  	fmt.Printf("Starting to listen on %v\n", port)
    28  	go func() {
    29  		log.Fatal(http.ListenAndServe(":"+port, nil))
    30  	}()
    31  
    32  	timer := time.NewTimer(5 * time.Second)
    33  	defer timer.Stop()
    34  
    35  	stop := make(chan os.Signal, 1)
    36  	signal.Notify(stop, os.Interrupt)
    37  
    38  	select {
    39  	case <-stop:
    40  		fmt.Println("Terminated")
    41  	case <-timer.C:
    42  		fmt.Println("Timed out")
    43  	}
    44  
    45  	fmt.Println("Exiting")
    46  }