package main import ( "context" "fmt" "log" "net/http" "os" "os/signal" "syscall" "time" ) func init() { fmt.Println("ZPROX started") } func handler1(w http.ResponseWriter, r *http.Request) { if r.Host == "test.diukman.com" { w.Write([]byte("H1")) } } func main() { router := http.NewServeMux() router.Handle("/", http.HandlerFunc(handler1)) srv := &http.Server{ Addr: ":8080", Handler: router, } go func() { if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { log.Fatalf("Failed to initialize server: %v\n", err) } }() log.Printf("Listening on port %v\n", srv.Addr) // Wait for kill signal of channel quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) // This blocks until a signal is passed into the quit channel <-quit // The context is used to inform the server it has 5 seconds to finish // the request it is currently handling ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() // Shutdown server log.Println("Shutting down server...") if err := srv.Shutdown(ctx); err != nil { log.Fatalf("Server forced to shutdown: %v\n", err) } }