CoolFace
Apppublic

devin15/cursor2api-rust

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
main.rs106 linesDownload Raw Back to src
1mod app;2mod chat;3mod common;4 5use app::{6    config::handle_config_update,7    constant::{8        EMPTY_STRING, PKG_VERSION, ROUTE_ABOUT_PATH, ROUTE_API_PATH, ROUTE_BASIC_CALIBRATION_PATH, ROUTE_CONFIG_PATH, ROUTE_ENV_EXAMPLE_PATH, ROUTE_GET_CHECKSUM, ROUTE_GET_HASH, ROUTE_GET_TIMESTAMP_HEADER, ROUTE_GET_TOKENINFO_PATH, ROUTE_HEALTH_PATH, ROUTE_LOGS_PATH, ROUTE_README_PATH, ROUTE_ROOT_PATH, ROUTE_STATIC_PATH, ROUTE_TOKENINFO_PATH, ROUTE_UPDATE_TOKENINFO_PATH, ROUTE_USER_INFO_PATH9    },10    lazy::{AUTH_TOKEN, ROUTE_CHAT_PATH, ROUTE_MODELS_PATH},11    model::*,12};13use axum::{14    routing::{get, post},15    Router,16};17use chat::{18    route::{19        handle_about, handle_api_page, handle_basic_calibration, handle_config_page, handle_env_example, handle_get_checksum, handle_get_hash, handle_get_timestamp_header, handle_get_tokeninfo, handle_health, handle_logs, handle_logs_post, handle_readme, handle_root, handle_static, handle_tokeninfo_page, handle_update_tokeninfo, handle_update_tokeninfo_post, handle_user_info20    },21    service::{handle_chat, handle_models},22};23use common::utils::{24    load_tokens, parse_bool_from_env, parse_string_from_env, parse_usize_from_env,25};26use std::sync::Arc;27use tokio::sync::Mutex;28use tower_http::{cors::CorsLayer, limit::RequestBodyLimitLayer};29 30#[tokio::main]31async fn main() {32    // 设置自定义 panic hook33    std::panic::set_hook(Box::new(|info| {34        // std::env::set_var("RUST_BACKTRACE", "1");35        if let Some(msg) = info.payload().downcast_ref::<String>() {36            eprintln!("{}", msg);37        } else if let Some(msg) = info.payload().downcast_ref::<&str>() {38            eprintln!("{}", msg);39        }40    }));41 42    // 加载环境变量43    dotenvy::dotenv().ok();44 45    if AUTH_TOKEN.is_empty() {46        panic!("AUTH_TOKEN must be set")47    };48 49    // 初始化全局配置50    AppConfig::init(51        parse_bool_from_env("ENABLE_STREAM_CHECK", true),52        parse_bool_from_env("INCLUDE_STOP_REASON_STREAM", true),53        VisionAbility::from_str(&parse_string_from_env("VISION_ABILITY", EMPTY_STRING)),54        parse_bool_from_env("ENABLE_SLOW_POOL", false),55        parse_bool_from_env("PASS_ANY_CLAUDE", false),56    );57 58    // 加载 tokens59    let token_infos = load_tokens();60 61    // 初始化应用状态62    let state = Arc::new(Mutex::new(AppState::new(token_infos)));63 64    // 设置路由65    let app = Router::new()66        .route(ROUTE_ROOT_PATH, get(handle_root))67        .route(ROUTE_HEALTH_PATH, get(handle_health))68        .route(ROUTE_TOKENINFO_PATH, get(handle_tokeninfo_page))69        .route(ROUTE_MODELS_PATH.as_str(), get(handle_models))70        .route(ROUTE_UPDATE_TOKENINFO_PATH, get(handle_update_tokeninfo))71        .route(ROUTE_GET_TOKENINFO_PATH, post(handle_get_tokeninfo))72        .route(73            ROUTE_UPDATE_TOKENINFO_PATH,74            post(handle_update_tokeninfo_post),75        )76        .route(ROUTE_CHAT_PATH.as_str(), post(handle_chat))77        .route(ROUTE_LOGS_PATH, get(handle_logs))78        .route(ROUTE_LOGS_PATH, post(handle_logs_post))79        .route(ROUTE_ENV_EXAMPLE_PATH, get(handle_env_example))80        .route(ROUTE_CONFIG_PATH, get(handle_config_page))81        .route(ROUTE_CONFIG_PATH, post(handle_config_update))82        .route(ROUTE_STATIC_PATH, get(handle_static))83        .route(ROUTE_ABOUT_PATH, get(handle_about))84        .route(ROUTE_README_PATH, get(handle_readme))85        .route(ROUTE_API_PATH, get(handle_api_page))86        .route(ROUTE_GET_HASH, get(handle_get_hash))87        .route(ROUTE_GET_CHECKSUM, get(handle_get_checksum))88        .route(ROUTE_GET_TIMESTAMP_HEADER, get(handle_get_timestamp_header))89        .route(ROUTE_BASIC_CALIBRATION_PATH, post(handle_basic_calibration))90        .route(ROUTE_USER_INFO_PATH, post(handle_user_info))91        .layer(RequestBodyLimitLayer::new(92            1024 * 1024 * parse_usize_from_env("REQUEST_BODY_LIMIT_MB", 2),93        ))94        .layer(CorsLayer::permissive())95        .with_state(state);96 97    // 启动服务器98    let port = parse_string_from_env("PORT", "3000");99    let addr = format!("0.0.0.0:{}", port);100    println!("服务器运行在端口 {}", port);101    println!("当前版本: v{}", PKG_VERSION);102 103    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();104    axum::serve(listener, app).await.unwrap();105}106