schematicapi.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package api
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. "github.com/go-chi/chi"
  10. "github.com/go-chi/render"
  11. "github.com/willie68/schematic-service-go/dao"
  12. "github.com/willie68/schematic-service-go/model"
  13. )
  14. //SchematicsRoutes getting all routes for the config endpoint
  15. func SchematicsRoutes() *chi.Mux {
  16. router := chi.NewRouter()
  17. router.Post("/", PostSchematicEndpoint)
  18. router.Get("/", GetSchematicsEndpoint)
  19. router.Get("/count", GetSchematicsCountEndpoint)
  20. router.Delete("/{schematicId}", DeleteSchematicEndpoint)
  21. router.Put("/{schematicId}", UpdateSchematicEndpoint)
  22. router.Get("/{schematicId}", GetSchematicHandler)
  23. router.Get("/files/{fileId}", GetSchematicFileHandler)
  24. router.Post("/files", PostFileEndpoint)
  25. return router
  26. }
  27. // GetSchematicHandler gets a tenant
  28. func GetSchematicHandler(response http.ResponseWriter, req *http.Request) {
  29. schematicID := chi.URLParam(req, "schematicId")
  30. schematic, err := dao.GetStorage().GetSchematic(schematicID)
  31. if err != nil {
  32. Msg(response, http.StatusBadRequest, err.Error())
  33. return
  34. }
  35. owner, _, _ := req.BasicAuth()
  36. if schematic.PrivateFile && schematic.Owner != owner {
  37. Msg(response, http.StatusForbidden, "you don't have the permission to see this file")
  38. return
  39. }
  40. render.JSON(response, req, schematic)
  41. }
  42. // GetSchematicFileHandler gets a tenant
  43. func GetSchematicFileHandler(response http.ResponseWriter, req *http.Request) {
  44. fileID := chi.URLParam(req, "fileId")
  45. filename, err := dao.GetStorage().GetFilename(fileID)
  46. response.Header().Add("Content-disposition", "attachment; filename=\""+filename+"\"")
  47. err = dao.GetStorage().GetFile(fileID, response)
  48. if err != nil {
  49. Msg(response, http.StatusBadRequest, err.Error())
  50. return
  51. }
  52. }
  53. // GetSchematicsEndpoint gets all tenants
  54. func GetSchematicsEndpoint(response http.ResponseWriter, req *http.Request) {
  55. offset := 0
  56. limit := 10
  57. query := req.URL.Query().Get("query")
  58. offsetStr := req.URL.Query().Get("offset")
  59. limitStr := req.URL.Query().Get("limit")
  60. var err error
  61. if offsetStr != "" {
  62. offset, err = strconv.Atoi(offsetStr)
  63. if err != nil {
  64. Msg(response, http.StatusBadRequest, err.Error())
  65. return
  66. }
  67. }
  68. if limitStr != "" {
  69. limit, err = strconv.Atoi(limitStr)
  70. if err != nil {
  71. Msg(response, http.StatusBadRequest, err.Error())
  72. return
  73. }
  74. }
  75. log.Printf("query: %s, offset: %d, limit: %d\n", query, offset, limit)
  76. owner, _, _ := req.BasicAuth()
  77. n, schematics, err := dao.GetStorage().GetSchematics(query, offset, limit, owner)
  78. if err != nil {
  79. Msg(response, http.StatusBadRequest, err.Error())
  80. return
  81. }
  82. m := make(map[string]interface{})
  83. m["data"] = schematics
  84. m["found"] = n
  85. m["count"] = len(schematics)
  86. m["query"] = query
  87. m["offset"] = offset
  88. m["limit"] = limit
  89. render.JSON(response, req, m)
  90. }
  91. // GetSchematicsCountEndpoint gets all tenants
  92. func GetSchematicsCountEndpoint(response http.ResponseWriter, req *http.Request) {
  93. offset := 0
  94. limit := 10
  95. query := req.URL.Query().Get("query")
  96. offsetStr := req.URL.Query().Get("offset")
  97. limitStr := req.URL.Query().Get("limit")
  98. var err error
  99. if offsetStr != "" {
  100. offset, err = strconv.Atoi(offsetStr)
  101. if err != nil {
  102. Msg(response, http.StatusBadRequest, err.Error())
  103. return
  104. }
  105. }
  106. if limitStr != "" {
  107. limit, err = strconv.Atoi(limitStr)
  108. if err != nil {
  109. Msg(response, http.StatusBadRequest, err.Error())
  110. return
  111. }
  112. }
  113. log.Printf("query: %s, offset: %d, limit: %d\n", query, offset, limit)
  114. owner, _, _ := req.BasicAuth()
  115. n, err := dao.GetStorage().GetSchematicsCount(query, owner)
  116. if err != nil {
  117. Msg(response, http.StatusBadRequest, err.Error())
  118. return
  119. }
  120. m := make(map[string]interface{})
  121. m["found"] = n
  122. m["query"] = query
  123. m["offset"] = offset
  124. m["limit"] = limit
  125. render.JSON(response, req, m)
  126. }
  127. func PostSchematicEndpoint(response http.ResponseWriter, req *http.Request) {
  128. var schematic model.Schematic
  129. err := render.DefaultDecoder(req, &schematic)
  130. if err != nil {
  131. Msg(response, http.StatusBadRequest, err.Error())
  132. return
  133. }
  134. username, _, _ := req.BasicAuth()
  135. if username != "" {
  136. schematic.Owner = username
  137. }
  138. schematic.CreatedAt = time.Now()
  139. schematic.LastModifiedAt = time.Now()
  140. id, err := dao.GetStorage().CreateSchematic(schematic)
  141. if err != nil {
  142. Msg(response, http.StatusBadRequest, err.Error())
  143. return
  144. }
  145. schematic, err = dao.GetStorage().GetSchematic(id)
  146. if err != nil {
  147. Msg(response, http.StatusBadRequest, err.Error())
  148. return
  149. }
  150. render.Status(req, http.StatusCreated)
  151. render.JSON(response, req, schematic)
  152. }
  153. func PostFileEndpoint(response http.ResponseWriter, req *http.Request) {
  154. req.ParseForm()
  155. f, fileHeader, err := req.FormFile("file")
  156. if err != nil {
  157. Msg(response, http.StatusBadRequest, err.Error())
  158. return
  159. }
  160. //mimeType := fileHeader.Header.Get("Content-type")
  161. filename := fileHeader.Filename
  162. reader := bufio.NewReader(f)
  163. fileid, err := dao.GetStorage().AddFile(filename, reader)
  164. if err != nil {
  165. fmt.Printf("%v\n", err)
  166. } else {
  167. fmt.Printf("fileid: %s\n", fileid)
  168. }
  169. location := fmt.Sprintf("/api/v1/schematics/files/%s", fileid)
  170. response.Header().Add("Location", location)
  171. render.Status(req, http.StatusCreated)
  172. m := make(map[string]interface{})
  173. m["fileid"] = fileid
  174. m["filename"] = filename
  175. render.JSON(response, req, m)
  176. }
  177. func DeleteSchematicEndpoint(response http.ResponseWriter, req *http.Request) {
  178. schematicID := chi.URLParam(req, "schematicId")
  179. schematic, err := dao.GetStorage().GetSchematic(schematicID)
  180. if err != nil {
  181. if err == dao.ErrNoDocument {
  182. Msg(response, http.StatusNotFound, err.Error())
  183. return
  184. }
  185. Msg(response, http.StatusBadRequest, err.Error())
  186. return
  187. }
  188. owner, _, _ := req.BasicAuth()
  189. if schematic.PrivateFile && schematic.Owner != owner {
  190. Msg(response, http.StatusForbidden, "you don't have the permission to see this file")
  191. return
  192. }
  193. dao.GetStorage().DeleteSchematic(schematic.ID.Hex())
  194. render.JSON(response, req, schematic)
  195. }
  196. func UpdateSchematicEndpoint(response http.ResponseWriter, req *http.Request) {
  197. var schematic model.Schematic
  198. err := render.DefaultDecoder(req, &schematic)
  199. if err != nil {
  200. Msg(response, http.StatusBadRequest, err.Error())
  201. return
  202. }
  203. username, _, _ := req.BasicAuth()
  204. if username != "" {
  205. schematic.Owner = username
  206. }
  207. schematic.CreatedAt = time.Now()
  208. schematic.LastModifiedAt = time.Now()
  209. id, err := dao.GetStorage().UpdateSchematic(schematic)
  210. if err != nil {
  211. Msg(response, http.StatusBadRequest, err.Error())
  212. return
  213. }
  214. schematic, err = dao.GetStorage().GetSchematic(id)
  215. if err != nil {
  216. Msg(response, http.StatusBadRequest, err.Error())
  217. return
  218. }
  219. render.Status(req, http.StatusCreated)
  220. render.JSON(response, req, schematic)
  221. }