github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_server_admin.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"context"
    11  	"os"
    12  	"strings"
    13  	"time"
    14  
    15  	"github.com/wangyougui/gf/v2/os/gfile"
    16  	"github.com/wangyougui/gf/v2/os/gproc"
    17  	"github.com/wangyougui/gf/v2/os/gtimer"
    18  	"github.com/wangyougui/gf/v2/os/gview"
    19  )
    20  
    21  // utilAdmin is the controller for administration.
    22  type utilAdmin struct{}
    23  
    24  // Index shows the administration page.
    25  func (p *utilAdmin) Index(r *Request) {
    26  	data := map[string]interface{}{
    27  		"pid":  gproc.Pid(),
    28  		"path": gfile.SelfPath(),
    29  		"uri":  strings.TrimRight(r.URL.Path, "/"),
    30  	}
    31  	buffer, _ := gview.ParseContent(r.Context(), `
    32              <html>
    33              <head>
    34                  <title>GoFrame Web Server Admin</title>
    35              </head>
    36              <body>
    37                  <p>Pid: {{.pid}}</p>
    38                  <p>File Path: {{.path}}</p>
    39                  <p><a href="{{$.uri}}/restart">Restart</a></p>
    40                  <p><a href="{{$.uri}}/shutdown">Shutdown</a></p>
    41              </body>
    42              </html>
    43      `, data)
    44  	r.Response.Write(buffer)
    45  }
    46  
    47  // Restart restarts all the servers in the process.
    48  func (p *utilAdmin) Restart(r *Request) {
    49  	var (
    50  		ctx = r.Context()
    51  		err error
    52  	)
    53  	// Custom start binary path when this process exits.
    54  	path := r.GetQuery("newExeFilePath").String()
    55  	if path == "" {
    56  		path = os.Args[0]
    57  	}
    58  	if err = RestartAllServer(ctx, path); err == nil {
    59  		r.Response.WriteExit("server restarted")
    60  	} else {
    61  		r.Response.WriteExit(err.Error())
    62  	}
    63  }
    64  
    65  // Shutdown shuts down all the servers.
    66  func (p *utilAdmin) Shutdown(r *Request) {
    67  	gtimer.SetTimeout(r.Context(), time.Second, func(ctx context.Context) {
    68  		// It shuts down the server after 1 second, which is not triggered by system signal,
    69  		// to ensure the response successfully to the client.
    70  		_ = r.Server.Shutdown()
    71  	})
    72  	r.Response.WriteExit("server shutdown")
    73  }
    74  
    75  // EnableAdmin enables the administration feature for the process.
    76  // The optional parameter `pattern` specifies the URI for the administration page.
    77  func (s *Server) EnableAdmin(pattern ...string) {
    78  	p := "/debug/admin"
    79  	if len(pattern) > 0 {
    80  		p = pattern[0]
    81  	}
    82  	s.BindObject(p, &utilAdmin{})
    83  }
    84  
    85  // Shutdown shuts down current server.
    86  func (s *Server) Shutdown() error {
    87  	var ctx = context.TODO()
    88  	s.doServiceDeregister()
    89  	// Only shut down current servers.
    90  	// It may have multiple underlying http servers.
    91  	for _, v := range s.servers {
    92  		v.close(ctx)
    93  	}
    94  	return nil
    95  }