You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. import sys
  3. from pathlib import Path
  4. sys.path.append(str(Path(__file__).parent.parent))
  5. import httpx
  6. from common import config_helper, Logger
  7. def setup_admin_account() -> None:
  8. """Setup Dify API with an admin account."""
  9. log = Logger("SetupAdmin")
  10. log.header("Setting up Admin Account")
  11. # Admin account credentials
  12. admin_config = {
  13. "email": "test@dify.ai",
  14. "username": "dify",
  15. "password": "password123",
  16. }
  17. # Save credentials to config file
  18. if config_helper.write_config("admin_config", admin_config):
  19. log.info(
  20. f"Admin credentials saved to: {config_helper.get_config_path('benchmark_state')}"
  21. )
  22. # API setup endpoint
  23. base_url = "http://localhost:5001"
  24. setup_endpoint = f"{base_url}/console/api/setup"
  25. # Prepare setup payload
  26. setup_payload = {
  27. "email": admin_config["email"],
  28. "name": admin_config["username"],
  29. "password": admin_config["password"],
  30. }
  31. log.step("Configuring Dify with admin account...")
  32. try:
  33. # Make the setup request
  34. with httpx.Client() as client:
  35. response = client.post(
  36. setup_endpoint,
  37. json=setup_payload,
  38. headers={"Content-Type": "application/json"},
  39. )
  40. if response.status_code == 201:
  41. log.success("Admin account created successfully!")
  42. log.key_value("Email", admin_config["email"])
  43. log.key_value("Username", admin_config["username"])
  44. elif response.status_code == 400:
  45. log.warning(
  46. "Setup may have already been completed or invalid data provided"
  47. )
  48. log.debug(f"Response: {response.text}")
  49. else:
  50. log.error(f"Setup failed with status code: {response.status_code}")
  51. log.debug(f"Response: {response.text}")
  52. except httpx.ConnectError:
  53. log.error("Could not connect to Dify API at http://localhost:5001")
  54. log.info("Make sure the API server is running with: ./dev/start-api")
  55. except Exception as e:
  56. log.error(f"An error occurred: {e}")
  57. if __name__ == "__main__":
  58. setup_admin_account()