实验内容:
简单Web App的实现,通过docker部署的Swagger Editor进行测试,并解决跨域请求资源的问题,同时使用OpenAPI3 文档来定义echo请求
实验结果:
本地测试
通过Swagger Editor的测试
总结反思:
- 解决跨域资源请求时,因为写习惯本地环回的测试,bind的时候,用了127.0.0.1
,查半天,感谢dlc,已老实
- 因为之前的账号不用了,重新注册了个论坛账号,新人只能发一张图片,但是网站提示是OVO,拼尽全力,无法战胜,再次感谢dlc,发现盲点:
代码附录:
use actix_web::{get,web,Result};
use actix_cors::Cors;
use actix_web::http::header;
#[get("/echo/{user_name}")]
async fn echo(path: web::Path<String>)-> Result<String>{
let user_name=path.into_inner();
return Ok(format!("Hello, {}",user_name));
}
#[actix_web::main]
async fn main() -> std::io::Result<()>{
use actix_web::{App,HttpServer};
HttpServer::new(|| {
let cors = Cors::default()
.allow_any_origin()
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE);
App::new()
.wrap(cors)
.service(echo)
})
.bind(("0.0.0.0", 8081))?
.run()
.await
}