Skip to content

Instantly share code, notes, and snippets.

@kilroyjones
Created December 2, 2022 22:23
Show Gist options
  • Save kilroyjones/4acba2cf5daa294feb8e8d616e992c64 to your computer and use it in GitHub Desktop.
Save kilroyjones/4acba2cf5daa294feb8e8d616e992c64 to your computer and use it in GitHub Desktop.
CORS
use actix_web::{App, post, get, HttpServer, HttpResponse, Error, web, Responder};
use actix_cors::Cors;
use backend::models::Albums;
use dotenv::dotenv;
use std::env;
use sqlx::sqlite::SqlitePool;
use serde_derive::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Test {
t: String
}
#[post("/")]
asyn fn index(val: web::Json<Test>) -> Result<HttpResponse, Error> {
println!("{:?}", val);
Ok(HttpResponse::Ok().body("test".into()))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let cors = Cors::default()
.allow_any_origin()
.allowed_methods(vec!["GET"])
.allow_any_header()
.max_age(3600);
App::new()
.wrap(cors)
.service(index)
})
.bind("127.0.0.1:3000")?
.run()
.await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment