dthh5038
2018-07-22 18:25
浏览 465
已采纳

停止服务器而不退出程序golang

I am using golang to run a simple server on http://localhost:8080. I need a way to stop the server and restart a different server when a user accesses http://localhost:8080/winrestart. So far I have this:

package main


import (
  "net/http" //serving files and stuff
  "log"      //logging that the server is running and other stuff
  "fmt"      //"congrats on winning!"
)


func main() {

  //servemux
  srvmx := http.NewServeMux()

  //handlers that serve the home html file when called
  fs := http.FileServer(http.Dir("./home/"))
  os := http.FileServer(http.Dir("./lvlone/"))
  ws := http.FileServer(http.Dir("./win/"))

  //creates custom server
  server := http.Server {
    Addr: ":8080",
    Handler: srvmx,
  }

  //handles paths by serving correct files
  srvmx.Handle("/", fs)
  srvmx.Handle("/lvlione/", http.StripPrefix("/lvlione/", os))
  srvmx.Handle("/win/", http.StripPrefix("/win/", ws))
  srvmx.HandleFunc("/winrestart/", func(w http.ResponseWriter, r *http.Request){
    fmt.Println("server is being closed")

    //creates new servemux
    wsm := http.NewServeMux()

    //this handler just redirects people to the beggining
    rh := http.RedirectHandler("http://127.0.0.1:8080/", 308)

    //create new redirect server
    redirector := http.Server {
      Addr: ":8080",
      Handler: wsm,
    }

    //Handle all paths by redirecting
    wsm.Handle("/lvlione/", rh)
    wsm.Handle("/win/", rh)
    wsm.Handle("/winrestart/", rh)

    //logs redirect server is Listening
    log.Println("redirecting...")
    server.Close()
    redirector.ListenAndServe()
  })

  //logs that server is Listening
  log.Println("Listening...")
  //starts normal level server
  server.ListenAndServe()
}

As of now, the server closes and the program exits, but no new server is started. Is there a way this can be done?

图片转代码服务由CSDN问答提供 功能建议

我正在使用golang在 http:// localhost:8080 。 当用户访问 http:// localhost:8080 / winrestart 。 到目前为止,我已经有了:

 包main 
 
 
import(
“ net / http” //正在提供文件和内容
“ log” //正在记录 服务器正在运行并且有其他东西
“ fmt” //“恭喜获胜!” 
)
 
 
func main(){
 
 // servemux 
 srvmx:= http.NewServeMux(  )
 
 //在调用时提供主html文件的处理程序
 fs:= http.FileServer(http.Dir(“ ./ home /”))
 os:= http.FileServer(http.Dir(  “ ./lvlone/"))
 ws:= http.FileServer(http.Dir(” ./ win /“))
 
 //创建自定义服务器
服务器:= http.Server {
地址 :“:8080”,
处理程序:srvmx,
} 
 
 //通过提供正确的文件来处理路径
 srvmx.Handle(“ /”,fs)
 srvmx.Handle(“ / lvlione /”  ,http.StripPrefix(“ / lvlione /”,os))
 srvmx.Handle(“ / win /”,http.StripPrefix(“ / win /”,ws))
 srvmx.HandleFunc(“ / winrestart /”  ,func(w http.ResponseWriter,r * http.Request){
 fmt.Println(“服务器正在关闭”)
 
 //创建新的servemux 
 wsm:= http.NewServeMux()
 \  n //此处理程序仅重定向人 到开始
 rh:= http.RedirectHandler(“ http://127.0.0.1:8080/”,308)
 
 //创建新的重定向服务器
重定向器\ = http.Server {
地址:  “:8080”,
处理程序:wsm,
} 
 
 //通过重定向
 wsm.Handle(“ / lvlione /”,rh)
 wsm.Handle(“ / win /”处理所有路径 ,rh)
 wsm.Handle(“ / winrestart /”,rh)
 
 //日志重定向服务器正在监听
 log.Println(“ redirecting ...”)
 server.Close()
  redirector.ListenAndServe()
})
 
 //记录服务器正在监听
 log.Println(“ Listening ...”)
 //启动普通级别的服务器
 server.ListenAndServe()
  } 
   
 
 

到目前为止,服务器关闭并且程序退出,但是没有启动新服务器。 有办法吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongren4758 2018-07-23 10:33
    已采纳

    The problem here is that the main thread becomes unblocked when you call server.Close()

    The main thread starts the server on it's very last line: server.ListenAndServe() but when the /winrestart/ handler method gets called; this handler method calls server.Close(), which stops the server, and the original blocking call to server.ListenAndServe() becomes unblocked. The main goroutine exits, and the program exits.

    Runnable, simplified example showing this:

    https://play.golang.org/p/RM1uNASBaC1

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题