github.com/gogf/gf@v1.16.9/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/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"github.com/gogf/gf/os/gfile"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/os/gproc"
    15  	"github.com/gogf/gf/os/gtimer"
    16  	"github.com/gogf/gf/os/gview"
    17  )
    18  
    19  // utilAdmin is the controller for administration.
    20  type utilAdmin struct{}
    21  
    22  // Index shows the administration page.
    23  func (p *utilAdmin) Index(r *Request) {
    24  	data := map[string]interface{}{
    25  		"pid":  gproc.Pid(),
    26  		"path": gfile.SelfPath(),
    27  		"uri":  strings.TrimRight(r.URL.Path, "/"),
    28  	}
    29  	buffer, _ := gview.ParseContent(r.Context(), `
    30              <html>
    31              <head>
    32                  <title>GoFrame Web Server Admin</title>
    33              </head>
    34              <body>
    35                  <p>Pid: {{.pid}}</p>
    36                  <p>File Path: {{.path}}</p>
    37                  <p><a href="{{$.uri}}/restart">Restart</a></p>
    38                  <p><a href="{{$.uri}}/shutdown">Shutdown</a></p>
    39              </body>
    40              </html>
    41      `, data)
    42  	r.Response.Write(buffer)
    43  }
    44  
    45  // Restart restarts all the servers in the process.
    46  func (p *utilAdmin) Restart(r *Request) {
    47  	var err error = nil
    48  	// Custom start binary path when this process exits.
    49  	path := r.GetQueryString("newExeFilePath")
    50  	if path == "" {
    51  		path = gfile.SelfPath()
    52  	}
    53  	if len(path) > 0 {
    54  		err = RestartAllServer(path)
    55  	} else {
    56  		err = RestartAllServer()
    57  	}
    58  	if 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(time.Second, func() {
    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  	// Only shut down current servers.
    88  	// It may have multiple underlying http servers.
    89  	for _, v := range s.servers {
    90  		v.close()
    91  	}
    92  	return nil
    93  }