📌  相关文章
📜  优雅关机 - Go 编程语言 - Go 编程语言代码示例

📅  最后修改于: 2022-03-11 14:44:59.697000             🧑  作者: Mango

代码示例1
package main

import (
    "context"
    "log"
    "net/http"
    "os"
    "os/signal"
    "syscall"
    "time"

    "github.com/gorilla/mux" // need to use dep for package management
)

func TestEndpoint(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(200)
    w.Write([]byte("Test is what we usually do"))
}

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/test", TestEndpoint).Methods("GET")

    srv := &http.Server{
        Addr:    ":8080",
        Handler: router,
    }

    done := make(chan os.Signal, 1)
    signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

    go func() {
        if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
            log.Fatalf("listen: %s\n", err)
        }
    }()
    log.Print("Server Started")

    <-done
    log.Print("Server Stopped")
    
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer func() {
        // extra handling here
        cancel()
    }()

    if err := srv.Shutdown(ctx); err != nil {
        log.Fatalf("Server Shutdown Failed:%+v", err)
    }
    log.Print("Server Exited Properly")
}