handlers.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. )
  6. type Status struct {
  7. Playing bool //If the device is playing
  8. PlayingFileName string //The playing filename
  9. PlayingFileURL string //The playing URL
  10. PlayingPosition int //The number of seconds that is currently playing
  11. Volume int //The volume currently is playing at, in percentage
  12. Status string //Status of the device, support {downloading, converting, playing, paused, ready}
  13. }
  14. type Endpoint struct {
  15. Name string
  16. RelPath string
  17. Desc string
  18. Type string
  19. AllowRead bool
  20. AllowWrite bool
  21. Min int
  22. Max int
  23. Steps float64
  24. Regex string
  25. }
  26. func handleIndex(w http.ResponseWriter, r *http.Request) {
  27. //Serve the index
  28. sendOK(w)
  29. }
  30. func handleStatus(w http.ResponseWriter, r *http.Request) {
  31. //Send out the current device status
  32. js, _ := json.Marshal(deviceStatus)
  33. sendJSONResponse(w, string(js))
  34. }
  35. func handleEndpoints(w http.ResponseWriter, r *http.Request) {
  36. endpoints := []Endpoint{}
  37. endpoints = append(endpoints, Endpoint{
  38. Name: "Play",
  39. RelPath: "play",
  40. Desc: "Toggle player to play or pause",
  41. Type: "none",
  42. })
  43. endpoints = append(endpoints, Endpoint{
  44. Name: "Stop",
  45. RelPath: "stop",
  46. Desc: "Stop and flush the buffer of the playing song",
  47. Type: "none",
  48. })
  49. endpoints = append(endpoints, Endpoint{
  50. Name: "Load",
  51. RelPath: "load",
  52. Desc: "Load a network resources to play",
  53. Type: "string",
  54. })
  55. endpoints = append(endpoints, Endpoint{
  56. Name: "Volume",
  57. RelPath: "vol",
  58. Desc: "Set the volume of the device playback in percentage",
  59. Type: "integer",
  60. Max: 100,
  61. Min: 0,
  62. Steps: 1,
  63. })
  64. endpoints = append(endpoints, Endpoint{
  65. Name: "Jump",
  66. RelPath: "jump",
  67. Desc: "Jump to a given location on the track",
  68. Type: "integer",
  69. Steps: 1,
  70. })
  71. js, _ := json.Marshal(endpoints)
  72. sendJSONResponse(w, string(js))
  73. }