gbserver/src/main.rs

94 lines
2.9 KiB
Rust
Raw Normal View History

use std::sync::Arc;
2025-07-04 10:39:31 -03:00
use actix_cors::Cors;
use actix_web::http::header;
use actix_web::{web, App, HttpServer};
use dotenv::dotenv;
2025-07-19 12:10:16 -03:00
use services::state::*;
2025-08-16 18:13:03 -03:00
use services::{config::*, file::*};
use sqlx::PgPool;
2025-08-16 18:13:03 -03:00
use crate::services::automation::AutomationService;
use crate::services::email::{get_emails, list_emails, save_click, send_email};
use crate::services::llm::{chat, chat_stream};
2025-08-14 09:42:05 -03:00
use crate::services::llm_provider::chat_completions;
2025-07-29 21:39:24 -03:00
use crate::services::web_automation::{initialize_browser_pool, BrowserPool};
2025-08-14 09:42:05 -03:00
2025-08-16 18:13:03 -03:00
mod models;
mod services;
#[tokio::main(flavor = "multi_thread")]
async fn main() -> std::io::Result<()> {
dotenv().ok();
2025-07-20 15:01:18 -03:00
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let config = AppConfig::from_env();
let db_url = config.database_url();
let db_custom_url = config.database_custom_url();
let db = PgPool::connect(&db_url).await.unwrap();
let db_custom = PgPool::connect(&db_custom_url).await.unwrap();
let minio_client = init_minio(&config)
.await
.expect("Failed to initialize Minio");
let browser_pool = Arc::new(BrowserPool::new(
"http://localhost:9515".to_string(),
5,
"/usr/bin/brave-browser-beta".to_string(),
));
2025-08-14 09:42:05 -03:00
2025-08-16 18:13:03 -03:00
#[cfg(feature = "local_llm")]
{
use crate::services::llm_local::ensure_llama_server_running;
ensure_llama_server_running()
.await
.expect("Failed to initialize LLM local server.");
}
2025-08-14 09:42:05 -03:00
2025-07-29 21:39:24 -03:00
initialize_browser_pool()
.await
.expect("Failed to initialize browser pool");
2025-08-14 09:42:05 -03:00
let app_state = web::Data::new(AppState {
db: db.into(),
db_custom: db_custom.into(),
config: Some(config.clone()),
minio_client: minio_client.into(),
browser_pool: browser_pool.clone(),
});
2025-08-16 18:13:03 -03:00
// Start automation service in background
let automation_state = app_state.get_ref().clone(); // This gets the Arc<AppState>
2025-07-19 12:10:16 -03:00
2025-08-16 20:40:20 -03:00
let automation = AutomationService::new(automation_state, "src/prompts");
2025-08-16 18:13:03 -03:00
let _automation_handle = automation.spawn();
// Start HTTP server
HttpServer::new(move || {
2025-07-04 10:39:31 -03:00
let cors = Cors::default()
2025-07-19 12:10:16 -03:00
.send_wildcard()
2025-07-04 10:39:31 -03:00
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE)
.max_age(3600);
2025-07-20 00:03:37 -03:00
App::new()
2025-08-16 18:13:03 -03:00
.wrap(cors)
.app_data(app_state.clone())
.service(upload_file)
.service(list_file)
.service(save_click)
.service(get_emails)
2025-07-04 10:39:31 -03:00
.service(list_emails)
.service(send_email)
2025-07-04 23:20:48 -03:00
.service(chat_stream)
2025-08-14 09:42:05 -03:00
.service(chat_completions)
2025-07-04 23:20:48 -03:00
.service(chat)
})
.bind((config.server.host.clone(), config.server.port))?
.run()
.await
}