api.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "strings"
  8. "imuslab.com/bokofs/bokofsd/mod/diskinfo"
  9. )
  10. /*
  11. API Router
  12. This module handle routing of the API calls
  13. */
  14. // Primary handler for the API router
  15. func HandlerAPIcalls() http.Handler {
  16. return http.StripPrefix("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. // Get the disk ID from the URL path
  18. pathParts := strings.Split(r.URL.Path, "/")
  19. if len(pathParts) < 2 {
  20. http.Error(w, "Bad Request", http.StatusBadRequest)
  21. return
  22. }
  23. diskID := pathParts[1]
  24. if diskID == "" {
  25. http.Error(w, "Bad Request", http.StatusBadRequest)
  26. return
  27. }
  28. if diskID == "system" {
  29. HandleSystemAPIcalls().ServeHTTP(w, r)
  30. return
  31. }
  32. fmt.Fprintf(w, "API call for disk ID: %s\n", diskID)
  33. }))
  34. }
  35. // Handler for system API calls
  36. func HandleSystemAPIcalls() http.Handler {
  37. return http.StripPrefix("/system/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  38. //Check the next part of the URL
  39. pathParts := strings.Split(r.URL.Path, "/")
  40. subPath := pathParts[0]
  41. switch subPath {
  42. case "diskinfo":
  43. if len(pathParts) < 2 {
  44. http.Error(w, "Bad Request - Invalid disk name", http.StatusBadRequest)
  45. return
  46. }
  47. diskID := pathParts[1]
  48. if diskID == "" {
  49. http.Error(w, "Bad Request - Invalid disk name", http.StatusBadRequest)
  50. return
  51. }
  52. if !diskinfo.DevicePathIsValidDisk(diskID) {
  53. log.Println("Invalid disk ID:", diskID)
  54. http.Error(w, "Bad Request", http.StatusBadRequest)
  55. return
  56. }
  57. // Handle diskinfo API calls
  58. targetDiskInfo, err := diskinfo.GetDiskInfo(diskID)
  59. if err != nil {
  60. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  61. return
  62. }
  63. // Convert the disk info to JSON and write it to the response
  64. js, _ := json.Marshal(targetDiskInfo)
  65. w.Header().Set("Content-Type", "application/json")
  66. w.WriteHeader(http.StatusOK)
  67. w.Write(js)
  68. return
  69. case "partinfo":
  70. if len(pathParts) < 2 {
  71. http.Error(w, "Bad Request - Missing parition name", http.StatusBadRequest)
  72. return
  73. }
  74. partID := pathParts[1]
  75. if partID == "" {
  76. http.Error(w, "Bad Request - Missing parition name", http.StatusBadRequest)
  77. return
  78. }
  79. if !diskinfo.DevicePathIsValidPartition(partID) {
  80. log.Println("Invalid partition name:", partID)
  81. http.Error(w, "Bad Request - Invalid parition name", http.StatusBadRequest)
  82. return
  83. }
  84. // Handle partinfo API calls
  85. targetPartInfo, err := diskinfo.GetPartitionInfo(partID)
  86. if err != nil {
  87. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  88. return
  89. }
  90. // Convert the partition info to JSON and write it to the response
  91. js, _ := json.Marshal(targetPartInfo)
  92. w.Header().Set("Content-Type", "application/json")
  93. w.WriteHeader(http.StatusOK)
  94. w.Write(js)
  95. return
  96. case "partlist":
  97. // Handle partlist API calls
  98. return
  99. case "diskusage":
  100. // Handle diskusage API calls
  101. fmt.Fprintln(w, "Disk usage API call")
  102. default:
  103. fmt.Println("Unknown API call:", subPath)
  104. http.Error(w, "Not Found", http.StatusNotFound)
  105. }
  106. }))
  107. }