zprox_bkup_1/main.go
Zeev Diukman 9a868bb5f8 start
2024-08-22 06:15:17 +03:00

55 lines
1.1 KiB
Go

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) {
w.Write([]byte("HELLO ZPROX"))
}
func main() {
router := http.NewServeMux()
router.Handle("/", http.HandlerFunc(handler1))
srv := &http.Server{
Addr: ":80",
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)
}
}