endpoints.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package api
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/go-chi/chi"
  6. "github.com/go-chi/render"
  7. "github.com/google/martian/log"
  8. "github.com/willie68/schematic-service-go/config"
  9. "github.com/willie68/schematic-service-go/dao"
  10. )
  11. // TenantHeader in this header thr right tenant should be inserted
  12. const TenantHeader = "X-es-tenant"
  13. const timeout = 1 * time.Minute
  14. //APIKey the apikey of this service
  15. var APIKey string
  16. //SystemID the systemid of this service
  17. var SystemID string
  18. /*
  19. ConfigDescription describres all metadata of a config
  20. */
  21. type ConfigDescription struct {
  22. StoreID string `json:"storeid"`
  23. TenantID string `json:"tenantID"`
  24. Size int `json:"size"`
  25. }
  26. /*
  27. ConfigRoutes getting all routes for the config endpoint
  28. */
  29. func ConfigRoutes() *chi.Mux {
  30. router := chi.NewRouter()
  31. router.Get("/", GetConfigEndpoint)
  32. router.Delete("/dropall", DropAllEndpoint)
  33. router.Post("/backup", PostBackupEndpoint)
  34. return router
  35. }
  36. /*
  37. GetConfigEndpoint getting if a store for a tenant is initialised
  38. because of the automatic store creation, the value is more likely that data is stored for this tenant
  39. */
  40. func GetConfigEndpoint(response http.ResponseWriter, req *http.Request) {
  41. Msg(response, http.StatusNotImplemented, "not im plemented yet")
  42. }
  43. /*
  44. PostConfigEndpoint create a new store for a tenant
  45. because of the automatic store creation, this method will always return 201
  46. */
  47. func PostBackupEndpoint(response http.ResponseWriter, req *http.Request) {
  48. go func() {
  49. err := dao.GetStorage().Backup(config.Get().Backup.Path)
  50. if err != nil {
  51. log.Infof("error in backup: %v", err)
  52. }
  53. }()
  54. render.JSON(response, req, "backup started.")
  55. }
  56. //DropAllEndpoint dropping all data
  57. func DropAllEndpoint(response http.ResponseWriter, req *http.Request) {
  58. dao.GetStorage().DropAll()
  59. render.JSON(response, req, "")
  60. }