| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | # | 
					
						
							|  |  |  | #  Copyright 2024 The InfiniFlow Authors. All Rights Reserved. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | #  you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | #  You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #      http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | #  distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | #  See the License for the specific language governing permissions and | 
					
						
							|  |  |  | #  limitations under the License. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2024-11-14 17:13:48 +08:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | from importlib.util import module_from_spec, spec_from_file_location | 
					
						
							|  |  |  | from pathlib import Path | 
					
						
							|  |  |  | from flask import Blueprint, Flask | 
					
						
							|  |  |  | from werkzeug.wrappers.request import Request | 
					
						
							|  |  |  | from flask_cors import CORS | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  | from flasgger import Swagger | 
					
						
							| 
									
										
										
										
											2024-11-18 12:05:38 +08:00
										 |  |  | from itsdangerous.url_safe import URLSafeTimedSerializer as Serializer | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | from api.db import StatusEnum | 
					
						
							|  |  |  | from api.db.db_models import close_connection | 
					
						
							|  |  |  | from api.db.services import UserService | 
					
						
							|  |  |  | from api.utils import CustomJSONEncoder, commands | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from flask_session import Session | 
					
						
							|  |  |  | from flask_login import LoginManager | 
					
						
							| 
									
										
										
										
											2024-11-15 17:30:56 +08:00
										 |  |  | from api import settings | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | from api.utils.api_utils import server_error_response | 
					
						
							| 
									
										
										
										
											2024-11-18 12:05:38 +08:00
										 |  |  | from api.constants import API_VERSION | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  | __all__ = ["app"] | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Request.json = property(lambda self: self.get_json(force=True, silent=True)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app = Flask(__name__) | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Add this at the beginning of your file to configure Swagger UI | 
					
						
							|  |  |  | swagger_config = { | 
					
						
							|  |  |  |     "headers": [], | 
					
						
							|  |  |  |     "specs": [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             "endpoint": "apispec", | 
					
						
							|  |  |  |             "route": "/apispec.json", | 
					
						
							|  |  |  |             "rule_filter": lambda rule: True,  # Include all endpoints | 
					
						
							|  |  |  |             "model_filter": lambda tag: True,  # Include all models | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     "static_url_path": "/flasgger_static", | 
					
						
							|  |  |  |     "swagger_ui": True, | 
					
						
							|  |  |  |     "specs_route": "/apidocs/", | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | swagger = Swagger( | 
					
						
							|  |  |  |     app, | 
					
						
							|  |  |  |     config=swagger_config, | 
					
						
							|  |  |  |     template={ | 
					
						
							|  |  |  |         "swagger": "2.0", | 
					
						
							|  |  |  |         "info": { | 
					
						
							|  |  |  |             "title": "RAGFlow API", | 
					
						
							|  |  |  |             "description": "", | 
					
						
							|  |  |  |             "version": "1.0.0", | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         "securityDefinitions": { | 
					
						
							|  |  |  |             "ApiKeyAuth": {"type": "apiKey", "name": "Authorization", "in": "header"} | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CORS(app, supports_credentials=True, max_age=2592000) | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | app.url_map.strict_slashes = False | 
					
						
							|  |  |  | app.json_encoder = CustomJSONEncoder | 
					
						
							|  |  |  | app.errorhandler(Exception)(server_error_response) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## convince for dev and debug | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  | # app.config["LOGIN_DISABLED"] = True | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | app.config["SESSION_PERMANENT"] = False | 
					
						
							|  |  |  | app.config["SESSION_TYPE"] = "filesystem" | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  | app.config["MAX_CONTENT_LENGTH"] = int( | 
					
						
							|  |  |  |     os.environ.get("MAX_CONTENT_LENGTH", 128 * 1024 * 1024) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Session(app) | 
					
						
							|  |  |  | login_manager = LoginManager() | 
					
						
							|  |  |  | login_manager.init_app(app) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | commands.register_commands(app) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def search_pages_path(pages_dir): | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     app_path_list = [ | 
					
						
							|  |  |  |         path for path in pages_dir.glob("*_app.py") if not path.name.startswith(".") | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     api_path_list = [ | 
					
						
							|  |  |  |         path for path in pages_dir.glob("*sdk/*.py") if not path.name.startswith(".") | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  |     app_path_list.extend(api_path_list) | 
					
						
							|  |  |  |     return app_path_list | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def register_page(page_path): | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     path = f"{page_path}" | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     page_name = page_path.stem.rstrip("_app") | 
					
						
							|  |  |  |     module_name = ".".join( | 
					
						
							| 
									
										
										
										
											2024-11-15 17:30:56 +08:00
										 |  |  |         page_path.parts[page_path.parts.index("api"): -1] + (page_name,) | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     spec = spec_from_file_location(module_name, page_path) | 
					
						
							|  |  |  |     page = module_from_spec(spec) | 
					
						
							|  |  |  |     page.app = app | 
					
						
							|  |  |  |     page.manager = Blueprint(page_name, module_name) | 
					
						
							|  |  |  |     sys.modules[module_name] = page | 
					
						
							|  |  |  |     spec.loader.exec_module(page) | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     page_name = getattr(page, "page_name", page_name) | 
					
						
							|  |  |  |     url_prefix = ( | 
					
						
							| 
									
										
										
										
											2024-11-18 12:05:38 +08:00
										 |  |  |         f"/api/{API_VERSION}" if "/sdk/" in path else f"/{API_VERSION}/{page_name}" | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     app.register_blueprint(page.manager, url_prefix=url_prefix) | 
					
						
							|  |  |  |     return url_prefix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pages_dir = [ | 
					
						
							|  |  |  |     Path(__file__).parent, | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     Path(__file__).parent.parent / "api" / "apps", | 
					
						
							|  |  |  |     Path(__file__).parent.parent / "api" / "apps" / "sdk", | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | client_urls_prefix = [ | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     register_page(path) for dir in pages_dir for path in search_pages_path(dir) | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @login_manager.request_loader | 
					
						
							|  |  |  | def load_user(web_request): | 
					
						
							| 
									
										
										
										
											2024-11-15 17:30:56 +08:00
										 |  |  |     jwt = Serializer(secret_key=settings.SECRET_KEY) | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  |     authorization = web_request.headers.get("Authorization") | 
					
						
							|  |  |  |     if authorization: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             access_token = str(jwt.loads(authorization)) | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |             user = UserService.query( | 
					
						
							|  |  |  |                 access_token=access_token, status=StatusEnum.VALID.value | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  |             if user: | 
					
						
							|  |  |  |                 return user[0] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2024-11-12 17:35:13 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2024-11-14 17:13:48 +08:00
										 |  |  |             logging.exception("load_user got exception") | 
					
						
							| 
									
										
										
										
											2024-08-15 09:17:36 +08:00
										 |  |  |             return None | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.teardown_request | 
					
						
							|  |  |  | def _db_close(exc): | 
					
						
							| 
									
										
										
										
											2024-11-04 08:35:36 +01:00
										 |  |  |     close_connection() |