Browse Source

Update bootstrap module

Vova Tkach 6 years ago
parent
commit
2727683716

+ 1 - 1
go.mod

@@ -2,7 +2,7 @@ module golang-fave
 
 
 require (
 require (
 	github.com/go-sql-driver/mysql v1.4.1
 	github.com/go-sql-driver/mysql v1.4.1
-	github.com/vladimirok5959/golang-server-bootstrap v1.0.3
+	github.com/vladimirok5959/golang-server-bootstrap v1.0.4
 	github.com/vladimirok5959/golang-server-resources v1.0.2
 	github.com/vladimirok5959/golang-server-resources v1.0.2
 	github.com/vladimirok5959/golang-server-sessions v1.0.1
 	github.com/vladimirok5959/golang-server-sessions v1.0.1
 	github.com/vladimirok5959/golang-server-static v1.0.0
 	github.com/vladimirok5959/golang-server-static v1.0.0

+ 2 - 0
go.sum

@@ -5,6 +5,8 @@ github.com/vladimirok5959/golang-server-bootstrap v1.0.2 h1:UiOKkiNGcEddnx9O0cuL
 github.com/vladimirok5959/golang-server-bootstrap v1.0.2/go.mod h1:R5PGBuqlupYd0evIXoi81plWH/HpNQO2V/jHxZzg2y0=
 github.com/vladimirok5959/golang-server-bootstrap v1.0.2/go.mod h1:R5PGBuqlupYd0evIXoi81plWH/HpNQO2V/jHxZzg2y0=
 github.com/vladimirok5959/golang-server-bootstrap v1.0.3 h1:fNLzc3qZrB1SiX7PtjPU+mel/TJFuy6nI2y/9Pup6Uw=
 github.com/vladimirok5959/golang-server-bootstrap v1.0.3 h1:fNLzc3qZrB1SiX7PtjPU+mel/TJFuy6nI2y/9Pup6Uw=
 github.com/vladimirok5959/golang-server-bootstrap v1.0.3/go.mod h1:R5PGBuqlupYd0evIXoi81plWH/HpNQO2V/jHxZzg2y0=
 github.com/vladimirok5959/golang-server-bootstrap v1.0.3/go.mod h1:R5PGBuqlupYd0evIXoi81plWH/HpNQO2V/jHxZzg2y0=
+github.com/vladimirok5959/golang-server-bootstrap v1.0.4 h1:+Su6EAc5ZXntjEOkYKVZUJ8pRVrh7GIvqrkmeMS48Vo=
+github.com/vladimirok5959/golang-server-bootstrap v1.0.4/go.mod h1:R5PGBuqlupYd0evIXoi81plWH/HpNQO2V/jHxZzg2y0=
 github.com/vladimirok5959/golang-server-resources v1.0.2 h1:XwxFXyaOtfDGRmYp8P9q4P4gx4YK8NiYacpHe9V8Lck=
 github.com/vladimirok5959/golang-server-resources v1.0.2 h1:XwxFXyaOtfDGRmYp8P9q4P4gx4YK8NiYacpHe9V8Lck=
 github.com/vladimirok5959/golang-server-resources v1.0.2/go.mod h1:tsf2oAEf3E3ukiQSCO7dstl0IXbEXec68UUIiMWysBc=
 github.com/vladimirok5959/golang-server-resources v1.0.2/go.mod h1:tsf2oAEf3E3ukiQSCO7dstl0IXbEXec68UUIiMWysBc=
 github.com/vladimirok5959/golang-server-sessions v1.0.1 h1:cQsLk8hz7pkIV7/XectfejDF2j+61FE+/s4xErxBNJw=
 github.com/vladimirok5959/golang-server-sessions v1.0.1 h1:cQsLk8hz7pkIV7/XectfejDF2j+61FE+/s4xErxBNJw=

+ 1 - 1
main.go

@@ -146,5 +146,5 @@ func main() {
 
 
 		// Error 404
 		// Error 404
 		utils.SystemErrorPage404(w)
 		utils.SystemErrorPage404(w)
-	})
+	}, nil)
 }
 }

+ 26 - 9
vendor/github.com/vladimirok5959/golang-server-bootstrap/bootstrap/bootstrap.go

@@ -6,19 +6,21 @@ import (
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 	"os/signal"
 	"os/signal"
+	"syscall"
 	"time"
 	"time"
 )
 )
 
 
-type hndl func(h http.Handler) http.Handler
-type callback func(w http.ResponseWriter, r *http.Request)
+type customHandler func(h http.Handler) http.Handler
+type callbackBeforeAfter func(w http.ResponseWriter, r *http.Request)
+type callbackServer func(s *http.Server)
 
 
 type bootstrap struct {
 type bootstrap struct {
 	path   string
 	path   string
-	before callback
-	after  callback
+	before callbackBeforeAfter
+	after  callbackBeforeAfter
 }
 }
 
 
-func new(path string, before callback, after callback) *bootstrap {
+func new(path string, before callbackBeforeAfter, after callbackBeforeAfter) *bootstrap {
 	return &bootstrap{path, before, after}
 	return &bootstrap{path, before, after}
 }
 }
 
 
@@ -52,7 +54,7 @@ func (this *bootstrap) handler(w http.ResponseWriter, r *http.Request) {
 	}
 	}
 }
 }
 
 
-func Start(h hndl, host string, timeout time.Duration, path string, before callback, after callback) {
+func Start(h customHandler, host string, timeout time.Duration, path string, before callbackBeforeAfter, after callbackBeforeAfter, cbserv callbackServer) {
 	mux := http.NewServeMux()
 	mux := http.NewServeMux()
 	mux.HandleFunc("/", new(path, before, after).handler)
 	mux.HandleFunc("/", new(path, before, after).handler)
 
 
@@ -69,22 +71,37 @@ func Start(h hndl, host string, timeout time.Duration, path string, before callb
 		}
 		}
 	}
 	}
 
 
+	if cbserv != nil {
+		cbserv(srv)
+	}
+
 	stop := make(chan os.Signal)
 	stop := make(chan os.Signal)
-	signal.Notify(stop, os.Interrupt)
+	signal.Notify(stop, syscall.SIGTERM)
+	signal.Notify(stop, syscall.SIGINT)
 	go func() {
 	go func() {
 		fmt.Printf("Starting server at http://%s/\n", host)
 		fmt.Printf("Starting server at http://%s/\n", host)
 		if err := srv.ListenAndServe(); err != nil {
 		if err := srv.ListenAndServe(); err != nil {
 			if err != http.ErrServerClosed {
 			if err != http.ErrServerClosed {
 				fmt.Println(err)
 				fmt.Println(err)
 				stop <- os.Interrupt
 				stop <- os.Interrupt
+				os.Exit(1)
 			}
 			}
 		}
 		}
 	}()
 	}()
-	<-stop
-	fmt.Println("Shutting down server...")
+
+	switch val := <-stop; val {
+	case syscall.SIGTERM:
+		fmt.Println("Shutting down server (terminate)...")
+	case syscall.SIGINT:
+		fmt.Println("Shutting down server (interrupt)...")
+	default:
+		fmt.Println("Shutting down server...")
+	}
+
 	ctx, cancel := context.WithTimeout(context.Background(), timeout*time.Second)
 	ctx, cancel := context.WithTimeout(context.Background(), timeout*time.Second)
 	defer cancel()
 	defer cancel()
 	if err := srv.Shutdown(ctx); err != nil {
 	if err := srv.Shutdown(ctx); err != nil {
 		fmt.Println(err)
 		fmt.Println(err)
+		os.Exit(1)
 	}
 	}
 }
 }

+ 1 - 1
vendor/modules.txt

@@ -1,6 +1,6 @@
 # github.com/go-sql-driver/mysql v1.4.1
 # github.com/go-sql-driver/mysql v1.4.1
 github.com/go-sql-driver/mysql
 github.com/go-sql-driver/mysql
-# github.com/vladimirok5959/golang-server-bootstrap v1.0.3
+# github.com/vladimirok5959/golang-server-bootstrap v1.0.4
 github.com/vladimirok5959/golang-server-bootstrap/bootstrap
 github.com/vladimirok5959/golang-server-bootstrap/bootstrap
 # github.com/vladimirok5959/golang-server-resources v1.0.2
 # github.com/vladimirok5959/golang-server-resources v1.0.2
 github.com/vladimirok5959/golang-server-resources/resource
 github.com/vladimirok5959/golang-server-resources/resource