routes.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package server
  2. import (
  3. "github.com/gorilla/websocket"
  4. "github.com/sirupsen/logrus"
  5. "godash/bookmark"
  6. "godash/files"
  7. "godash/hub"
  8. "godash/message"
  9. "godash/system"
  10. "godash/weather"
  11. "net/http"
  12. )
  13. type launchpadInformation struct {
  14. Title string
  15. Host string
  16. Bookmarks []bookmark.Bookmark
  17. Weather weather.OpenWeatherApiResponse
  18. System system.LiveInformation
  19. }
  20. func launchpad(w http.ResponseWriter, r *http.Request) {
  21. w.Header().Set("Content-Type", "text/html")
  22. files.ParseAndServeHtml(w, "index.gohtml", launchpadInformation{
  23. Title: "Godash",
  24. Bookmarks: bookmark.Bookmarks,
  25. Weather: weather.CurrentOpenWeather,
  26. System: system.Sys.Live,
  27. })
  28. }
  29. // @Schemes
  30. // @Summary get the current weather
  31. // @Description gets the current weather
  32. // @Tags weather
  33. // @Produce json
  34. // @Success 200 {object} weather.OpenWeatherApiResponse
  35. // @Success 204 {object} message.Response
  36. // @Router /weather [get]
  37. func getWeather(w http.ResponseWriter, r *http.Request) {
  38. if weather.Conf.OpenWeather.Key != "" {
  39. jsonResponse(w, weather.CurrentOpenWeather, http.StatusOK)
  40. } else {
  41. jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
  42. }
  43. }
  44. // @Schemes
  45. // @Summary live system information
  46. // @Description gets live information of the system
  47. // @Tags system
  48. // @Produce json
  49. // @Success 200 {object} system.LiveInformation
  50. // @Success 204 {object} message.Response
  51. // @Router /system/live [get]
  52. func routeLiveSystem(w http.ResponseWriter, r *http.Request) {
  53. if system.Config.LiveSystem {
  54. jsonResponse(w, system.Sys.Live, http.StatusOK)
  55. } else {
  56. jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
  57. }
  58. }
  59. // @Schemes
  60. // @Summary static system information
  61. // @Description gets static information of the system
  62. // @Tags system
  63. // @Produce json
  64. // @Success 200 {object} system.StaticInformation
  65. // @Success 204 {object} message.Response
  66. // @Router /system/static [get]
  67. func routeStaticSystem(w http.ResponseWriter, r *http.Request) {
  68. if system.Config.LiveSystem {
  69. jsonResponse(w, system.Sys.Static, http.StatusOK)
  70. } else {
  71. jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
  72. }
  73. }
  74. func webSocket(w http.ResponseWriter, r *http.Request) {
  75. conn, err := upgrader.Upgrade(w, r, nil)
  76. if err != nil {
  77. logrus.WithField("error", err).Warning("Cannot upgrade websocket")
  78. return
  79. }
  80. messageChan := make(hub.NotifierChan)
  81. server.Hub.NewClients <- messageChan
  82. defer func() {
  83. server.Hub.ClosingClients <- messageChan
  84. conn.Close()
  85. }()
  86. go readPump(conn)
  87. for {
  88. select {
  89. case msg, ok := <-messageChan:
  90. if !ok {
  91. err := conn.WriteMessage(websocket.CloseMessage, []byte{})
  92. if err != nil {
  93. return
  94. }
  95. return
  96. }
  97. err := conn.WriteJSON(msg)
  98. if err != nil {
  99. return
  100. }
  101. }
  102. }
  103. }