main.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. use actix_web::{HttpServer, web, App};
  2. use actix_cors::Cors;
  3. #[actix_web::main]
  4. async fn main() -> std::io::Result<()> {
  5. let app_env = std::env::var("APP_ENV").unwrap_or_else(|_| "development".to_string());
  6. HttpServer::new(move || {
  7. let cors = if app_env == "development" {
  8. Cors::permissive()
  9. } else {
  10. Cors::default()
  11. .allowed_origin_fn(|origin, _req| {
  12. origin.as_bytes().ends_with(b".example.com")
  13. })
  14. .allowed_methods(vec!["GET", "POST", "PUT", "PATCH", "DELETE"])
  15. .allow_any_header()
  16. .supports_credentials()
  17. };
  18. App::new()
  19. .wrap(cors)
  20. .app_data(
  21. web::JsonConfig::default().error_handler(|err, _req| {
  22. AppError::JsonDeserializationError(err.to_string()).into()
  23. })
  24. )
  25. .configure(routes::other::config)
  26. })
  27. .bind(("0.0.0.0", 8000))?
  28. .run()
  29. .await
  30. }