golang 带托盘的 http server启动关闭服务

https://blog.csdn.net/codyguo/article/details/54582453

  1. // 程序和托盘图标
  2. rsrc -manifest main.manifest -o rsrc.syso -ico main.ico
  3. // 优雅关闭http
  4. github.com/braintree/manners
  5. //展示

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/braintree/manners"
  7. "github.com/lxn/walk"
  8. )
  9. func handler(w http.ResponseWriter, r *http.Request) {
  10. fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
  11. }
  12. type MyWindow struct {
  13. *walk.MainWindow
  14. ni *walk.NotifyIcon
  15. }
  16. func NewMyWindow() *MyWindow {
  17. mw := new(MyWindow)
  18. var err error
  19. mw.MainWindow, err = walk.NewMainWindow()
  20. checkError(err)
  21. return mw
  22. }
  23. func (mw *MyWindow) init() {
  24. http.HandleFunc("/", handler)
  25. }
  26. func (mw *MyWindow) RunHttpServer() error {
  27. return manners.ListenAndServe(":8080", http.DefaultServeMux)
  28. }
  29. func (mw *MyWindow) AddNotifyIcon() {
  30. var err error
  31. mw.ni, err = walk.NewNotifyIcon()
  32. checkError(err)
  33. mw.ni.SetVisible(true)
  34. icon, err := walk.NewIconFromResourceId(3)
  35. checkError(err)
  36. mw.SetIcon(icon)
  37. mw.ni.SetIcon(icon)
  38. startAction := mw.addAction(nil, "start")
  39. stopAction := mw.addAction(nil, "stop")
  40. stopAction.SetEnabled(false)
  41. startAction.Triggered().Attach(func() {
  42. go func() {
  43. err := mw.RunHttpServer()
  44. if err != nil {
  45. mw.msgbox("start", "start http server failed.", walk.MsgBoxIconError)
  46. return
  47. }
  48. }()
  49. startAction.SetChecked(true)
  50. startAction.SetEnabled(false)
  51. stopAction.SetEnabled(true)
  52. mw.msgbox("start", "start http server success.", walk.MsgBoxIconInformation)
  53. })
  54. stopAction.Triggered().Attach(func() {
  55. ok := manners.Close()
  56. if !ok {
  57. mw.msgbox("stop", "stop http server failed.", walk.MsgBoxIconError)
  58. } else {
  59. stopAction.SetEnabled(false)
  60. startAction.SetChecked(false)
  61. startAction.SetEnabled(true)
  62. mw.msgbox("stop", "stop http server success.", walk.MsgBoxIconInformation)
  63. }
  64. })
  65. helpMenu := mw.addMenu("help")
  66. mw.addAction(helpMenu, "help").Triggered().Attach(func() {
  67. walk.MsgBox(mw, "help", "http://127.0.0.1:8080", walk.MsgBoxIconInformation)
  68. })
  69. mw.addAction(helpMenu, "about").Triggered().Attach(func() {
  70. walk.MsgBox(mw, "about", "http server.", walk.MsgBoxIconInformation)
  71. })
  72. mw.addAction(nil, "exit").Triggered().Attach(func() {
  73. mw.ni.Dispose()
  74. mw.Dispose()
  75. walk.App().Exit(0)
  76. })
  77. }
  78. func (mw *MyWindow) addMenu(name string) *walk.Menu {
  79. helpMenu, err := walk.NewMenu()
  80. checkError(err)
  81. help, err := mw.ni.ContextMenu().Actions().AddMenu(helpMenu)
  82. checkError(err)
  83. help.SetText(name)
  84. return helpMenu
  85. }
  86. func (mw *MyWindow) addAction(menu *walk.Menu, name string) *walk.Action {
  87. action := walk.NewAction()
  88. action.SetText(name)
  89. if menu != nil {
  90. menu.Actions().Add(action)
  91. } else {
  92. mw.ni.ContextMenu().Actions().Add(action)
  93. }
  94. return action
  95. }
  96. func (mw *MyWindow) msgbox(title, message string, style walk.MsgBoxStyle) {
  97. mw.ni.ShowInfo(title, message)
  98. walk.MsgBox(mw, title, message, style)
  99. }
  100. func main() {
  101. mw := NewMyWindow()
  102. mw.init()
  103. mw.AddNotifyIcon()
  104. mw.Run()
  105. }
  106. func checkError(err error) {
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. }
ft_authoradmin  ft_create_time2018-03-28 10:26
 ft_update_time2018-03-28 10:27