set.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package configapi
  2. import (
  3. "net/http"
  4. "encoding/json"
  5. "github.com/azukaar/cosmos-server/src/utils"
  6. "github.com/azukaar/cosmos-server/src/authorizationserver"
  7. "github.com/azukaar/cosmos-server/src/constellation"
  8. )
  9. func ConfigApiSet(w http.ResponseWriter, req *http.Request) {
  10. if utils.AdminOnly(w, req) != nil {
  11. return
  12. }
  13. if(req.Method == "PUT") {
  14. var request utils.Config
  15. err1 := json.NewDecoder(req.Body).Decode(&request)
  16. if err1 != nil {
  17. utils.Error("SettingsUpdate: Invalid User Request", err1)
  18. utils.HTTPError(w, "User Creation Error",
  19. http.StatusInternalServerError, "UC001")
  20. return
  21. }
  22. errV := utils.Validate.Struct(request)
  23. if errV != nil {
  24. utils.Error("SettingsUpdate: Invalid User Request", errV)
  25. utils.HTTPError(w, "User Creation Error: " + errV.Error(),
  26. http.StatusInternalServerError, "UC003")
  27. return
  28. }
  29. // restore AuthPrivateKey and TLSKey
  30. config := utils.ReadConfigFromFile()
  31. request.HTTPConfig.AuthPrivateKey = config.HTTPConfig.AuthPrivateKey
  32. request.HTTPConfig.AuthPublicKey = config.HTTPConfig.AuthPublicKey
  33. request.HTTPConfig.TLSCert = config.HTTPConfig.TLSCert
  34. request.HTTPConfig.TLSKey = config.HTTPConfig.TLSKey
  35. request.NewInstall = config.NewInstall
  36. utils.SetBaseMainConfig(request)
  37. utils.TriggerEvent(
  38. "cosmos.settings",
  39. "Settings updated",
  40. "success",
  41. "",
  42. map[string]interface{}{
  43. })
  44. utils.DisconnectDB()
  45. authorizationserver.Init()
  46. utils.RestartHTTPServer()
  47. constellation.RestartNebula()
  48. json.NewEncoder(w).Encode(map[string]interface{}{
  49. "status": "OK",
  50. })
  51. } else {
  52. utils.Error("SettingsUpdate: Method not allowed" + req.Method, nil)
  53. utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
  54. return
  55. }
  56. }