| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- use actix_web::{HttpServer, web, middleware, App};
- use actix_cors::Cors;
- use sqlx::PgPool;
- use crate::app_error::AppError;
- mod app_error;
- mod routes;
- mod controllers;
- mod models;
- mod logic;
- mod auth;
- #[actix_web::main]
- async fn main() -> Result<(), AppError> {
- let app_env = std::env::var("APP_ENV").unwrap_or_else(|_| "development".to_string());
- let pool = PgPool::connect("postgres://leemorgan:password123@localhost:5432/homo")
- .await
- .expect("Failed to start Postgres");
- HttpServer::new(move || {
- let cors = if app_env == "development" {
- Cors::permissive()
- } else {
- Cors::default()
- .allowed_origin_fn(|origin, _req| {
- origin.as_bytes().ends_with(b".example.com")
- })
- .allowed_methods(vec!["GET", "POST", "PUT", "PATCH", "DELETE"])
- .allow_any_header()
- .supports_credentials()
- };
- App::new()
- .wrap(middleware::Compress::default())
- .wrap(cors)
- .app_data(web::Data::new(pool.clone()))
- .app_data(
- web::JsonConfig::default().error_handler(|err, _req| {
- AppError::JsonDeserializationError(err.to_string()).into()
- })
- )
- .configure(routes::user::config)
- .configure(routes::home::config)
- .configure(routes::device::config)
- })
- .bind(("0.0.0.0", 8000))
- .expect("Failed to bind to port")
- .run()
- .await
- .expect("Failed to start server");
- Ok(())
- }
|