2024-11-04 10:56:52 -08:00
|
|
|
use crate::router::create_router;
|
|
|
|
|
use crate::router::Router;
|
|
|
|
|
use actix_web::http::header::{HeaderValue, CONTENT_TYPE};
|
|
|
|
|
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
2024-10-28 09:49:48 -07:00
|
|
|
use bytes::Bytes;
|
|
|
|
|
use futures_util::StreamExt;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct AppState {
|
|
|
|
|
router: Box<dyn Router>,
|
|
|
|
|
client: reqwest::Client,
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 10:56:52 -08:00
|
|
|
impl AppState {
|
2024-10-28 09:49:48 -07:00
|
|
|
pub fn new(worker_urls: Vec<String>, policy: String, client: reqwest::Client) -> Self {
|
|
|
|
|
// Create router based on policy
|
|
|
|
|
let router = create_router(worker_urls, policy);
|
2024-11-04 10:56:52 -08:00
|
|
|
|
|
|
|
|
Self { router, client }
|
2024-10-28 09:49:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/v1/models")]
|
2024-11-04 10:56:52 -08:00
|
|
|
async fn v1_model(data: web::Data<AppState>) -> impl Responder {
|
|
|
|
|
let worker_url = match data.router.get_first() {
|
2024-10-28 09:49:48 -07:00
|
|
|
Some(url) => url,
|
|
|
|
|
None => return HttpResponse::InternalServerError().finish(),
|
|
|
|
|
};
|
|
|
|
|
// Use the shared client
|
2024-11-04 10:56:52 -08:00
|
|
|
match data
|
|
|
|
|
.client
|
|
|
|
|
.get(format!("{}/v1/models", worker_url))
|
2024-10-28 09:49:48 -07:00
|
|
|
.send()
|
2024-11-04 10:56:52 -08:00
|
|
|
.await
|
2024-10-28 09:49:48 -07:00
|
|
|
{
|
|
|
|
|
Ok(res) => {
|
|
|
|
|
let status = actix_web::http::StatusCode::from_u16(res.status().as_u16())
|
2024-11-04 10:56:52 -08:00
|
|
|
.unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
2024-10-28 09:49:48 -07:00
|
|
|
// print the status
|
|
|
|
|
println!("Worker URL: {}, Status: {}", worker_url, status);
|
|
|
|
|
match res.bytes().await {
|
|
|
|
|
Ok(body) => HttpResponse::build(status).body(body.to_vec()),
|
|
|
|
|
Err(_) => HttpResponse::InternalServerError().finish(),
|
|
|
|
|
}
|
2024-11-04 10:56:52 -08:00
|
|
|
}
|
2024-10-28 09:49:48 -07:00
|
|
|
Err(_) => HttpResponse::InternalServerError().finish(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/get_model_info")]
|
2024-11-04 10:56:52 -08:00
|
|
|
async fn get_model_info(data: web::Data<AppState>) -> impl Responder {
|
|
|
|
|
let worker_url = match data.router.get_first() {
|
2024-10-28 09:49:48 -07:00
|
|
|
Some(url) => url,
|
|
|
|
|
None => return HttpResponse::InternalServerError().finish(),
|
|
|
|
|
};
|
|
|
|
|
// Use the shared client
|
2024-11-04 10:56:52 -08:00
|
|
|
match data
|
|
|
|
|
.client
|
|
|
|
|
.get(format!("{}/get_model_info", worker_url))
|
2024-10-28 09:49:48 -07:00
|
|
|
.send()
|
2024-11-04 10:56:52 -08:00
|
|
|
.await
|
2024-10-28 09:49:48 -07:00
|
|
|
{
|
|
|
|
|
Ok(res) => {
|
|
|
|
|
let status = actix_web::http::StatusCode::from_u16(res.status().as_u16())
|
2024-11-04 10:56:52 -08:00
|
|
|
.unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
2024-10-28 09:49:48 -07:00
|
|
|
// print the status
|
|
|
|
|
println!("Worker URL: {}, Status: {}", worker_url, status);
|
|
|
|
|
match res.bytes().await {
|
|
|
|
|
Ok(body) => HttpResponse::build(status).body(body.to_vec()),
|
|
|
|
|
Err(_) => HttpResponse::InternalServerError().finish(),
|
|
|
|
|
}
|
2024-11-04 10:56:52 -08:00
|
|
|
}
|
2024-10-28 09:49:48 -07:00
|
|
|
Err(_) => HttpResponse::InternalServerError().finish(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no deser and ser, just forward and return
|
|
|
|
|
#[post("/generate")]
|
2024-11-04 10:56:52 -08:00
|
|
|
async fn generate(req: HttpRequest, body: Bytes, data: web::Data<AppState>) -> impl Responder {
|
2024-10-28 09:49:48 -07:00
|
|
|
// create a router struct
|
|
|
|
|
// TODO: use router abstraction for different policy
|
2024-11-04 10:56:52 -08:00
|
|
|
let worker_url = match data.router.select() {
|
2024-10-28 09:49:48 -07:00
|
|
|
Some(url) => url,
|
|
|
|
|
None => return HttpResponse::InternalServerError().finish(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Check if client requested streaming
|
|
|
|
|
let is_stream = serde_json::from_slice::<serde_json::Value>(&body)
|
|
|
|
|
.map(|v| v.get("stream").and_then(|s| s.as_bool()).unwrap_or(false))
|
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
2024-11-04 10:56:52 -08:00
|
|
|
let res = match data
|
|
|
|
|
.client
|
|
|
|
|
.post(format!("{}/generate", worker_url))
|
2024-10-28 09:49:48 -07:00
|
|
|
.header(
|
2024-11-04 10:56:52 -08:00
|
|
|
"Content-Type",
|
2024-10-28 09:49:48 -07:00
|
|
|
req.headers()
|
|
|
|
|
.get("Content-Type")
|
|
|
|
|
.and_then(|h| h.to_str().ok())
|
2024-11-04 10:56:52 -08:00
|
|
|
.unwrap_or("application/json"),
|
2024-10-28 09:49:48 -07:00
|
|
|
)
|
|
|
|
|
.body(body.to_vec())
|
|
|
|
|
.send()
|
2024-11-04 10:56:52 -08:00
|
|
|
.await
|
2024-10-28 09:49:48 -07:00
|
|
|
{
|
|
|
|
|
Ok(res) => res,
|
|
|
|
|
Err(_) => return HttpResponse::InternalServerError().finish(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let status = actix_web::http::StatusCode::from_u16(res.status().as_u16())
|
|
|
|
|
.unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
|
|
if !is_stream {
|
|
|
|
|
match res.bytes().await {
|
|
|
|
|
Ok(body) => HttpResponse::build(status).body(body.to_vec()),
|
|
|
|
|
Err(_) => HttpResponse::InternalServerError().finish(),
|
2024-11-04 10:56:52 -08:00
|
|
|
}
|
2024-10-28 09:49:48 -07:00
|
|
|
} else {
|
|
|
|
|
HttpResponse::build(status)
|
|
|
|
|
.insert_header((CONTENT_TYPE, HeaderValue::from_static("text/event-stream")))
|
|
|
|
|
.streaming(res.bytes_stream().map(|b| match b {
|
|
|
|
|
Ok(b) => Ok::<_, actix_web::Error>(b),
|
2024-11-04 10:56:52 -08:00
|
|
|
Err(_) => Err(actix_web::error::ErrorInternalServerError(
|
|
|
|
|
"Failed to read stream",
|
|
|
|
|
)),
|
2024-10-28 09:49:48 -07:00
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 10:56:52 -08:00
|
|
|
pub async fn startup(
|
|
|
|
|
host: String,
|
|
|
|
|
port: u16,
|
|
|
|
|
worker_urls: Vec<String>,
|
|
|
|
|
routing_policy: String,
|
|
|
|
|
) -> std::io::Result<()> {
|
2024-10-28 09:49:48 -07:00
|
|
|
println!("Starting server on {}:{}", host, port);
|
|
|
|
|
println!("Worker URLs: {:?}", worker_urls);
|
|
|
|
|
|
|
|
|
|
// Create client once with configuration
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.build()
|
|
|
|
|
.expect("Failed to create HTTP client");
|
|
|
|
|
|
|
|
|
|
// Store both worker_urls and client in AppState
|
2024-11-04 10:56:52 -08:00
|
|
|
let app_state = web::Data::new(AppState::new(worker_urls, routing_policy, client));
|
2024-10-28 09:49:48 -07:00
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
|
App::new()
|
|
|
|
|
.app_data(app_state.clone())
|
|
|
|
|
.service(generate)
|
|
|
|
|
.service(v1_model)
|
|
|
|
|
.service(get_model_info)
|
|
|
|
|
})
|
|
|
|
|
.bind((host, port))?
|
|
|
|
|
.run()
|
|
|
|
|
.await
|
2024-11-04 10:56:52 -08:00
|
|
|
}
|