xiaoh2018 commited on
Commit
826af92
·
verified ·
1 Parent(s): dc01849

Upload admin.py

Browse files
Files changed (1) hide show
  1. src/api/admin.py +105 -62
src/api/admin.py CHANGED
@@ -519,8 +519,13 @@ async def update_proxy_config_alias(
519
  token: str = Depends(verify_admin_token)
520
  ):
521
  """Update proxy configuration (alias for frontend compatibility)"""
522
- await proxy_manager.update_proxy_config(request.proxy_enabled, request.proxy_url)
523
- return {"success": True, "message": "代理配置更新成功"}
 
 
 
 
 
524
 
525
 
526
  @router.post("/api/config/proxy")
@@ -529,8 +534,13 @@ async def update_proxy_config(
529
  token: str = Depends(verify_admin_token)
530
  ):
531
  """Update proxy configuration"""
532
- await proxy_manager.update_proxy_config(request.proxy_enabled, request.proxy_url)
533
- return {"success": True, "message": "代理配置更新成功"}
 
 
 
 
 
534
 
535
 
536
  @router.get("/api/config/generation")
@@ -682,10 +692,18 @@ async def update_admin_config(
682
  token: str = Depends(verify_admin_token)
683
  ):
684
  """Update admin configuration (error_ban_threshold)"""
685
- # Update error_ban_threshold in database
686
- await db.update_admin_config(error_ban_threshold=request.error_ban_threshold)
 
687
 
688
- return {"success": True, "message": "配置更新成功"}
 
 
 
 
 
 
 
689
 
690
 
691
  @router.post("/api/admin/password")
@@ -741,12 +759,17 @@ async def update_generation_timeout(
741
  token: str = Depends(verify_admin_token)
742
  ):
743
  """Update generation timeout configuration"""
744
- await db.update_generation_config(request.image_timeout, request.video_timeout)
 
745
 
746
- # 🔥 Hot reload: sync database config to memory
747
- await db.reload_config_to_memory()
748
 
749
- return {"success": True, "message": "生成配置更新成功"}
 
 
 
 
750
 
751
 
752
  # ========== AT Auto Refresh Config ==========
@@ -815,16 +838,21 @@ async def update_cache_config_full(
815
  token: str = Depends(verify_admin_token)
816
  ):
817
  """Update complete cache configuration"""
818
- enabled = request.get("enabled")
819
- timeout = request.get("timeout")
820
- base_url = request.get("base_url")
 
821
 
822
- await db.update_cache_config(enabled=enabled, timeout=timeout, base_url=base_url)
823
 
824
- # 🔥 Hot reload: sync database config to memory
825
- await db.reload_config_to_memory()
826
 
827
- return {"success": True, "message": "缓存配置更新成功"}
 
 
 
 
828
 
829
 
830
  @router.post("/api/cache/base-url")
@@ -833,13 +861,18 @@ async def update_cache_base_url(
833
  token: str = Depends(verify_admin_token)
834
  ):
835
  """Update cache base URL"""
836
- base_url = request.get("base_url", "")
837
- await db.update_cache_config(base_url=base_url)
 
838
 
839
- # 🔥 Hot reload: sync database config to memory
840
- await db.reload_config_to_memory()
841
 
842
- return {"success": True, "message": "缓存Base URL更新成功"}
 
 
 
 
843
 
844
 
845
  @router.post("/api/captcha/config")
@@ -848,32 +881,37 @@ async def update_captcha_config(
848
  token: str = Depends(verify_admin_token)
849
  ):
850
  """Update captcha configuration"""
851
- from ..services.browser_captcha import validate_browser_proxy_url
852
-
853
- captcha_method = request.get("captcha_method")
854
- yescaptcha_api_key = request.get("yescaptcha_api_key")
855
- yescaptcha_base_url = request.get("yescaptcha_base_url")
856
- browser_proxy_enabled = request.get("browser_proxy_enabled", False)
857
- browser_proxy_url = request.get("browser_proxy_url", "")
858
-
859
- # 验证浏览器代理URL格式
860
- if browser_proxy_enabled and browser_proxy_url:
861
- is_valid, error_msg = validate_browser_proxy_url(browser_proxy_url)
862
- if not is_valid:
863
- return {"success": False, "message": error_msg}
864
-
865
- await db.update_captcha_config(
866
- captcha_method=captcha_method,
867
- yescaptcha_api_key=yescaptcha_api_key,
868
- yescaptcha_base_url=yescaptcha_base_url,
869
- browser_proxy_enabled=browser_proxy_enabled,
870
- browser_proxy_url=browser_proxy_url if browser_proxy_enabled else None
871
- )
 
872
 
873
- # 🔥 Hot reload: sync database config to memory
874
- await db.reload_config_to_memory()
875
 
876
- return {"success": True, "message": "验证码配置更新成功"}
 
 
 
 
877
 
878
 
879
  @router.get("/api/captcha/config")
@@ -931,24 +969,29 @@ async def update_plugin_config(
931
  token: str = Depends(verify_admin_token)
932
  ):
933
  """Update plugin configuration"""
934
- connection_token = request.get("connection_token", "")
935
- auto_enable_on_update = request.get("auto_enable_on_update", True) # 默认开启
 
936
 
937
- # Generate random token if empty
938
- if not connection_token:
939
- connection_token = secrets.token_urlsafe(32)
940
 
941
- await db.update_plugin_config(
942
- connection_token=connection_token,
943
- auto_enable_on_update=auto_enable_on_update
944
- )
945
 
946
- return {
947
- "success": True,
948
- "message": "插件配置更新成功",
949
- "connection_token": connection_token,
950
- "auto_enable_on_update": auto_enable_on_update
951
- }
 
 
 
 
952
 
953
 
954
  @router.post("/api/plugin/update-token")
 
519
  token: str = Depends(verify_admin_token)
520
  ):
521
  """Update proxy configuration (alias for frontend compatibility)"""
522
+ try:
523
+ await proxy_manager.update_proxy_config(request.proxy_enabled, request.proxy_url)
524
+ return {"success": True, "message": "代理配置更新成功"}
525
+ except Exception as e:
526
+ # 捕获所有异常,确保返回有效的 JSON 响应
527
+ print(f"Error updating proxy config: {e}")
528
+ return {"success": False, "message": f"保存失败: {str(e)}"}
529
 
530
 
531
  @router.post("/api/config/proxy")
 
534
  token: str = Depends(verify_admin_token)
535
  ):
536
  """Update proxy configuration"""
537
+ try:
538
+ await proxy_manager.update_proxy_config(request.proxy_enabled, request.proxy_url)
539
+ return {"success": True, "message": "代理配置更新成功"}
540
+ except Exception as e:
541
+ # 捕获所有异常,确保返回有效的 JSON 响应
542
+ print(f"Error updating proxy config: {e}")
543
+ return {"success": False, "message": f"保存失败: {str(e)}"}
544
 
545
 
546
  @router.get("/api/config/generation")
 
692
  token: str = Depends(verify_admin_token)
693
  ):
694
  """Update admin configuration (error_ban_threshold)"""
695
+ try:
696
+ # Update error_ban_threshold in database
697
+ await db.update_admin_config(error_ban_threshold=request.error_ban_threshold)
698
 
699
+ # 🔥 Hot reload: sync database config to memory
700
+ await db.reload_config_to_memory()
701
+
702
+ return {"success": True, "message": "配置更新成功"}
703
+ except Exception as e:
704
+ # 捕获所有异常,确保返回有效的 JSON 响应
705
+ print(f"Error updating admin config: {e}")
706
+ return {"success": False, "message": f"保存失败: {str(e)}"}
707
 
708
 
709
  @router.post("/api/admin/password")
 
759
  token: str = Depends(verify_admin_token)
760
  ):
761
  """Update generation timeout configuration"""
762
+ try:
763
+ await db.update_generation_config(request.image_timeout, request.video_timeout)
764
 
765
+ # 🔥 Hot reload: sync database config to memory
766
+ await db.reload_config_to_memory()
767
 
768
+ return {"success": True, "message": "生成配置更新成功"}
769
+ except Exception as e:
770
+ # 捕获所有异常,确保返回有效的 JSON 响应
771
+ print(f"Error updating generation timeout: {e}")
772
+ return {"success": False, "message": f"保存失败: {str(e)}"}
773
 
774
 
775
  # ========== AT Auto Refresh Config ==========
 
838
  token: str = Depends(verify_admin_token)
839
  ):
840
  """Update complete cache configuration"""
841
+ try:
842
+ enabled = request.get("enabled")
843
+ timeout = request.get("timeout")
844
+ base_url = request.get("base_url")
845
 
846
+ await db.update_cache_config(enabled=enabled, timeout=timeout, base_url=base_url)
847
 
848
+ # 🔥 Hot reload: sync database config to memory
849
+ await db.reload_config_to_memory()
850
 
851
+ return {"success": True, "message": "缓存配置更新成功"}
852
+ except Exception as e:
853
+ # 捕获所有异常,确保返回有效的 JSON 响应
854
+ print(f"Error updating cache config: {e}")
855
+ return {"success": False, "message": f"保存失败: {str(e)}"}
856
 
857
 
858
  @router.post("/api/cache/base-url")
 
861
  token: str = Depends(verify_admin_token)
862
  ):
863
  """Update cache base URL"""
864
+ try:
865
+ base_url = request.get("base_url", "")
866
+ await db.update_cache_config(base_url=base_url)
867
 
868
+ # 🔥 Hot reload: sync database config to memory
869
+ await db.reload_config_to_memory()
870
 
871
+ return {"success": True, "message": "缓存Base URL更新成功"}
872
+ except Exception as e:
873
+ # 捕获所有异常,确保返回有效的 JSON 响应
874
+ print(f"Error updating cache base URL: {e}")
875
+ return {"success": False, "message": f"保存失败: {str(e)}"}
876
 
877
 
878
  @router.post("/api/captcha/config")
 
881
  token: str = Depends(verify_admin_token)
882
  ):
883
  """Update captcha configuration"""
884
+ try:
885
+ from ..services.browser_captcha import validate_browser_proxy_url
886
+
887
+ captcha_method = request.get("captcha_method")
888
+ yescaptcha_api_key = request.get("yescaptcha_api_key")
889
+ yescaptcha_base_url = request.get("yescaptcha_base_url")
890
+ browser_proxy_enabled = request.get("browser_proxy_enabled", False)
891
+ browser_proxy_url = request.get("browser_proxy_url", "")
892
+
893
+ # 验证浏览器代理URL格式
894
+ if browser_proxy_enabled and browser_proxy_url:
895
+ is_valid, error_msg = validate_browser_proxy_url(browser_proxy_url)
896
+ if not is_valid:
897
+ return {"success": False, "message": error_msg}
898
+
899
+ await db.update_captcha_config(
900
+ captcha_method=captcha_method,
901
+ yescaptcha_api_key=yescaptcha_api_key,
902
+ yescaptcha_base_url=yescaptcha_base_url,
903
+ browser_proxy_enabled=browser_proxy_enabled,
904
+ browser_proxy_url=browser_proxy_url if browser_proxy_enabled else None
905
+ )
906
 
907
+ # 🔥 Hot reload: sync database config to memory
908
+ await db.reload_config_to_memory()
909
 
910
+ return {"success": True, "message": "验证码配置更新成功"}
911
+ except Exception as e:
912
+ # 捕获所有异常,确保返回有效的 JSON 响应
913
+ print(f"Error updating captcha config: {e}")
914
+ return {"success": False, "message": f"保存失败: {str(e)}"}
915
 
916
 
917
  @router.get("/api/captcha/config")
 
969
  token: str = Depends(verify_admin_token)
970
  ):
971
  """Update plugin configuration"""
972
+ try:
973
+ connection_token = request.get("connection_token", "")
974
+ auto_enable_on_update = request.get("auto_enable_on_update", True) # 默认开启
975
 
976
+ # Generate random token if empty
977
+ if not connection_token:
978
+ connection_token = secrets.token_urlsafe(32)
979
 
980
+ await db.update_plugin_config(
981
+ connection_token=connection_token,
982
+ auto_enable_on_update=auto_enable_on_update
983
+ )
984
 
985
+ return {
986
+ "success": True,
987
+ "message": "插件配置更新成功",
988
+ "connection_token": connection_token,
989
+ "auto_enable_on_update": auto_enable_on_update
990
+ }
991
+ except Exception as e:
992
+ # 捕获所有异常,确保返回有效的 JSON 响应
993
+ print(f"Error updating plugin config: {e}")
994
+ return {"success": False, "message": f"保存失败: {str(e)}"}
995
 
996
 
997
  @router.post("/api/plugin/update-token")