bootstrap.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package bootstrap
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. )
  11. type customHandler func(h http.Handler) http.Handler
  12. type callbackBeforeAfter func(w http.ResponseWriter, r *http.Request)
  13. type callbackServer func(s *http.Server)
  14. type bootstrap struct {
  15. path string
  16. before callbackBeforeAfter
  17. after callbackBeforeAfter
  18. }
  19. func new(path string, before callbackBeforeAfter, after callbackBeforeAfter) *bootstrap {
  20. return &bootstrap{path, before, after}
  21. }
  22. func (this *bootstrap) handler(w http.ResponseWriter, r *http.Request) {
  23. if this.before != nil {
  24. this.before(w, r)
  25. }
  26. if r.URL.Path == "/"+this.path+"/bootstrap.css" {
  27. w.Header().Set("Cache-Control", "public, max-age=31536000")
  28. w.Header().Set("Content-Type", "text/css")
  29. w.Write(resource_bootstrap_css)
  30. return
  31. } else if r.URL.Path == "/"+this.path+"/bootstrap.js" {
  32. w.Header().Set("Cache-Control", "public, max-age=31536000")
  33. w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
  34. w.Write(resource_bootstrap_js)
  35. return
  36. } else if r.URL.Path == "/"+this.path+"/jquery.js" {
  37. w.Header().Set("Cache-Control", "public, max-age=31536000")
  38. w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
  39. w.Write(resource_jquery_js)
  40. return
  41. } else if r.URL.Path == "/"+this.path+"/popper.js" {
  42. w.Header().Set("Cache-Control", "public, max-age=31536000")
  43. w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
  44. w.Write(resource_popper_js)
  45. return
  46. }
  47. if this.after != nil {
  48. this.after(w, r)
  49. }
  50. }
  51. func Start(h customHandler, host string, timeout time.Duration, path string, before callbackBeforeAfter, after callbackBeforeAfter, cbserv callbackServer) {
  52. mux := http.NewServeMux()
  53. mux.HandleFunc("/", new(path, before, after).handler)
  54. var srv *http.Server
  55. if h == nil {
  56. srv = &http.Server{
  57. Addr: host,
  58. Handler: mux,
  59. }
  60. } else {
  61. srv = &http.Server{
  62. Addr: host,
  63. Handler: h(mux),
  64. }
  65. }
  66. if cbserv != nil {
  67. cbserv(srv)
  68. }
  69. stop := make(chan os.Signal)
  70. signal.Notify(stop, syscall.SIGTERM)
  71. signal.Notify(stop, syscall.SIGINT)
  72. go func() {
  73. fmt.Printf("Starting server at http://%s/\n", host)
  74. if err := srv.ListenAndServe(); err != nil {
  75. if err != http.ErrServerClosed {
  76. fmt.Println(err)
  77. stop <- os.Interrupt
  78. os.Exit(1)
  79. }
  80. }
  81. }()
  82. switch val := <-stop; val {
  83. case syscall.SIGTERM:
  84. fmt.Println("Shutting down server (terminate)...")
  85. case syscall.SIGINT:
  86. fmt.Println("Shutting down server (interrupt)...")
  87. default:
  88. fmt.Println("Shutting down server...")
  89. }
  90. ctx, cancel := context.WithTimeout(context.Background(), timeout*time.Second)
  91. defer cancel()
  92. if err := srv.Shutdown(ctx); err != nil {
  93. fmt.Println(err)
  94. os.Exit(1)
  95. }
  96. }