-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
199 lines (144 loc) · 6.98 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
print()
restart_str = "RESTARTING"
start_str_len = len(restart_str)
restart_symbol_1 = "/\_"
restart_symbol_2 = "\/‾"
start_symbol_len = len(restart_symbol_1)
start_multiplyer = 40
print(restart_symbol_1*start_multiplyer)
print(" "*(start_symbol_len)*( round(start_multiplyer/2) - (round(start_str_len/start_symbol_len))), restart_str)
print(restart_symbol_2*start_multiplyer)
import os
import click
from dotenv import load_dotenv
from pathlib import Path # python3 only
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
import uvicorn
from utils.env import get_boolean, getenv_boolean
from pprint import pprint, pformat, PrettyPrinter
pp = PrettyPrinter(indent=4)
### READ ENV VARS FROM HIDDEN .ENV FILES
env_path_global = Path('.') / 'env-backend.env'
load_dotenv(env_path_global, verbose=True)
env_path_auth = Path('.') / 'env-auth.env'
load_dotenv(env_path_auth, verbose=True)
# for ES
# db_es_enabled = getenv_boolean("DB_ELASTICSEARCH_MODE")
db_es_enabled = os.getenv("DB_ELASTICSEARCH_MODE", 'disabled') != 'disabled'
if db_es_enabled :
env_path_elasticsearch = Path('.') / 'env-elasticsearch.env'
load_dotenv(env_path_elasticsearch, verbose=True)
# for mongoDB
# db_mongo_enabled = getenv_boolean("DB_MONGODB_MODE")
db_mongo_enabled = os.getenv("DB_MONGODB_MODE", 'disabled') != 'disabled'
if db_mongo_enabled :
env_path_mongodb = Path('.') / 'env-mongodb.env'
load_dotenv(env_path_mongodb, verbose=True)
# load config
from core import config
from log_config import log_, pformat
log_.debug( "... starting to log stuff ..." )
### LOAD ROUTER
from api.api_v1.api import api_router
app = FastAPI(
title=config.PROJECT_NAME,
description=f"A <a href='{config.PROJECT_REPO}' target='_blank'>CRUDity</a> instance to expose your open data",
version=config.PROJECT_VERSION,
openapi_url="/api/v1/openapi.json"
)
### CORS
origins = []
# Set all CORS enabled origins
if config.BACKEND_CORS_ORIGINS:
origins_raw = config.BACKEND_CORS_ORIGINS.split(",")
for origin in origins_raw:
use_origin = origin.strip()
origins.append(use_origin)
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
),
app.include_router(
api_router,
prefix=config.API_V1_STR
)
default_mode = config.APP_MODE
default_autoreload = config.APP_AUTORELOAD
default_auth = config.AUTH_MODE
default_host = config.SERVER_HOST
default_port = config.SERVER_PORT
default_esdb = config.DB_ELASTICSEARCH_MODE
default_mongodb = config.DB_MONGODB_MODE
default_docker = 'docker_off'
default_https = 'false'
### APP RUNNER
@click.command()
# @click.option('--mode', default="default", nargs=1, help="The <mode> you need to run the app : default | testing | preprod | production" )
# @click.option('--auth', default="default", nargs=1, help="The <auth> mode you need to run the app : no_auth | dev | default | default_docker | server | server_docker | distant_preprod | distant_prod" )
# @click.option('--host', default="localhost", nargs=1, help="The <host> name you want the app to run on : <IP_NUMBER> " )
# @click.option('--port', default="8000", nargs=1, help="The <port> number you want the app to run on : <PORT_NUMBER>")
# @click.option('--esdb', default="local", nargs=1, help="The <esdb> you need to run the app : disabled | local | distant | server" )
# @click.option('--mongodb', default="local", nargs=1, help="The <mongodb> you need to run the app : disabled | local | distant | server" )
# @click.option('--docker', default="docker_off", nargs=1, help="Are you running the app with <docker> : docker_off | docker_on" )
# @click.option('--https', default="false", nargs=1, help="The <https> mode you want the app to run on : true | false")
# @click.option('--reload_mode', default="true", nargs=1, help="The <https> mode you want the app to run on : true | false")
@click.option('--mode', default=default_mode, nargs=1, help="The <mode> you need to run the app : default | dev | testing | preprod | production" )
@click.option('--autoreload', default=default_autoreload, nargs=1, help="The <autoreload> mode you want the app to run on : true | false")
@click.option('--auth', default=default_auth, nargs=1, help="The <auth> mode you need to run the app : no_auth | dev | default | default_docker | server | server_docker | distant_preprod | distant_prod" )
@click.option('--host', default=default_host, nargs=1, help="The <host> name you want the app to run on : <IP_NUMBER> " )
@click.option('--port', default=default_port, nargs=1, help="The <port> number you want the app to run on : <PORT_NUMBER>")
@click.option('--esdb', default=default_esdb, nargs=1, help="The <esdb> you need to run the app : disabled | local | distant | server" )
@click.option('--mongodb', default=default_mongodb, nargs=1, help="The <mongodb> you need to run the app : disabled | local | distant | server" )
@click.option('--docker', default=default_docker, nargs=1, help="Are you running the app with <docker> : docker_off | docker_on" )
@click.option('--https', default="false", nargs=1, help="The <https> mode you want the app to run on : true | false")
def app_runner(mode, auth, host, port, esdb, mongodb, docker, https, autoreload) :
"""
app_runner
"""
print ("= "*50)
print ("= = = RERUN FLASK APP FROM APP RUNNER = = =")
print ("= "*50)
### WARNING : CLIck will treat every input as string as defaults values are string too
print ("\n=== CUSTOM CONFIG FROM CLI ===\n")
print ("=== mode : ", mode)
print ("=== autoreload : ", autoreload)
print ("=== host : ", host)
print ("=== port : ", port)
print ("=== auth : ", auth)
print ("=== mongodb : ", mongodb)
print ("=== esdb : ", esdb)
print ("=== https : ", https)
print ("=== docker : ", docker)
### OVERRIDE ENV VARS FROM CLI
os.environ["APP_MODE"] = mode
os.environ["AUTH_MODE"] = auth
os.environ["SERVER_HOST"] = host
os.environ["SERVER_PORT"] = port
cli_server_name = f"{host}:{port}"
if config.SERVER_NAME != cli_server_name :
os.environ["SERVER_NAME"] = cli_server_name
os.environ["DB_ELASTICSEARCH"] = esdb # get_boolean(mongodb)
os.environ["DB_MONGODB"] = mongodb # get_boolean(esdb)
os.environ["DOCKER_MODE"] = docker
print ("\n=== config : \n")
pp.pprint(config.__dict__)
reload_string = ""
autoreload_bool = False
if autoreload in [ True, 'y', 'Y','yes', 'Yes', 'YES', 'true', 'True', 'TRUE', '1'] :
autoreload_bool = True
os.environ["APP_AUTORELOAD"] = "True"
reload_string = "--reload"
# uvicorn.run(
# app,
# host=host,
# port=int(port),
# # reload=autoreload_bool ### not working programmatically
# )
os.system( f"uvicorn main:app {reload_string} --host={host} --port={port}" )
if __name__ == "__main__":
log_.debug( "... starting __name__ == '__main__' ..." )
app_runner()