-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_env.py
87 lines (67 loc) · 2.69 KB
/
generate_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import secrets
import string
def generate_secret_key(length=32):
# Define the character pool including numbers, uppercase, and lowercase letters
characters = string.ascii_letters + string.digits # This includes [a-zA-Z0-9]
# Generate a random secret key from the pool
key = ''.join(secrets.choice(characters) for _ in range(length))
return key
def load_chatgpt_credentials(file_path='chatgpt_credentials.env'):
# Initialize a dictionary to hold the credentials
credentials = {}
try:
# Open the credentials file and read the contents
with open(file_path, 'r') as file:
for line in file:
# Split each line by the equals sign into key and value
key, value = line.strip().split('=', 1)
credentials[key] = value
except FileNotFoundError:
print(f"Warning: {file_path} not found. Skipping ChatGPT credentials.")
return credentials
def generate_nextjs_env_file(static_bearer_secret_key): # noqa
nextjs_env_content = f"""
NEXT_PUBLIC_API_BASE_URL=http://localhost:8000/api/v1
NEXT_PUBLIC_API_KEY={static_bearer_secret_key}
"""
nextjs_folder_path = "./nextjs"
if not os.path.exists(nextjs_folder_path):
os.makedirs(nextjs_folder_path)
with open(os.path.join(nextjs_folder_path, '.env'), 'w') as file:
file.write(nextjs_env_content)
def main():
mongodb_fastapi_password = generate_secret_key(length=16) # New password for MongoDB and FastAPI UI
chatgpt_credentials = load_chatgpt_credentials()
static_bearer_secret_key = generate_secret_key(length=32)
# Prepare the settings with the generated static_bearer_secret_key
env_content = f"""
# You can set your backend settings here.
# mongodb connection
mongodb_server=mongodb
mongodb_port=27017
mongodb_username=bringthemhome
mongodb_password={mongodb_fastapi_password}
# fastapi
fastapi_ui_username=bringthemhome
fastapi_ui_password=bringthemhome
# static_bearer_secret_key to protect your register from unauthorized access
static_bearer_secret_key={static_bearer_secret_key}
jwt_secret_key=bringthemhome
algorithm=HS256
# chatgpt
open_ai_organization={chatgpt_credentials.get('open_ai_organization', 'this')}
open_ai_secret_key={chatgpt_credentials.get('open_ai_secret_key', 'this')}
# default admin user
owner_username=root
owner_password=bringthemhome
""".strip()
# Write the combined content to an .env file
with open('.env', 'w') as file:
file.write(env_content)
generate_nextjs_env_file(static_bearer_secret_key)
print("Generated .env with keys and additional settings")
print("Generated .env for Next.js")
if __name__ == "__main__":
main()