api.go 3.7 KB

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