Since the update to actix-rt@2.3 (f413996a), the web server hasn't been
started, when we executed `cargo run -p rust-code-analysis-web`.

It froze once `actix_rt::System::new().run()?;` was called,
effectively preventing the execution of the code below.

By looking at the actix-rt examples and changing the code to be more
like the examples, I was able to fix this.
The webserver now behaves as expected.
This commit is contained in:
Thomas Praxl 2022-03-21 10:39:57 +01:00 коммит произвёл GitHub
Родитель 97e576b90a
Коммит 08c61f48ad
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 61 добавлений и 65 удалений

Просмотреть файл

@ -9,7 +9,8 @@ use clap::{crate_version, App, Arg};
use web::server;
fn main() {
#[actix_web::main]
async fn main() {
let matches = App::new("rust-code-analysis-web")
.version(crate_version!())
.author(&*env!("CARGO_PKG_AUTHORS").replace(':', "\n"))
@ -51,7 +52,7 @@ fn main() {
eprintln!("Invalid port number");
return;
};
if let Err(e) = server::run(host.to_string(), port, num_jobs) {
if let Err(e) = server::run(host.to_string(), port, num_jobs).await {
eprintln!("Cannot run the server at {}:{}: {}", host, port, e);
}
}

Просмотреть файл

@ -1,4 +1,3 @@
use actix_rt::Runtime;
use actix_web::{
dev::Body,
guard, http,
@ -220,71 +219,67 @@ fn ping() -> HttpResponse {
HttpResponse::Ok().body(Body::Empty)
}
pub fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> {
actix_rt::System::new().run()?;
let rt = Runtime::new()?;
pub async fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> {
let max_size = 1024 * 1024 * 4;
rt.block_on(async move {
HttpServer::new(move || {
App::new()
.service(
web::resource("/ast")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<AstPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(ast_parser)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebCommentPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(comment_removal_json)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(comment_removal_plain)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebMetricsPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(metrics_json)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(metrics_plain)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebFunctionPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(function_json)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(function_plain)),
)
.service(web::resource("/ping").route(web::get().to(ping)))
})
.workers(n_threads)
.bind((host.as_str(), port))?
.run()
.await
HttpServer::new(move || {
App::new()
.service(
web::resource("/ast")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<AstPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(ast_parser)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebCommentPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(comment_removal_json)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(comment_removal_plain)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebMetricsPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(metrics_json)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(metrics_plain)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebFunctionPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(function_json)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(function_plain)),
)
.service(web::resource("/ping").route(web::get().to(ping)))
})
.workers(n_threads)
.bind((host.as_str(), port))?
.run()
.await
}
// curl --header "Content-Type: application/json" --request POST --data '{"id": "1234", "file_name": "prova.cpp", "code": "int x = 1;", "comment": true, "span": true}' http://127.0.0.1:8081/ast