Move the web server in a new crate

This commit is contained in:
Luni-4 2020-11-26 13:01:22 +01:00
Родитель 3b6afee81c
Коммит 2d2c40db1a
11 изменённых файлов: 107 добавлений и 56 удалений

19
Cargo.lock сгенерированный
Просмотреть файл

@ -1540,14 +1540,10 @@ dependencies = [
name = "rust-code-analysis-cli"
version = "0.0.18"
dependencies = [
"actix-rt",
"actix-web",
"clap",
"crossbeam",
"futures",
"globset",
"num_cpus",
"pretty_assertions",
"regex",
"rust-code-analysis",
"serde",
@ -1558,6 +1554,21 @@ dependencies = [
"walkdir",
]
[[package]]
name = "rust-code-analysis-web"
version = "0.0.18"
dependencies = [
"actix-rt",
"actix-web",
"clap",
"futures",
"num_cpus",
"pretty_assertions",
"rust-code-analysis",
"serde",
"serde_json",
]
[[package]]
name = "rustc-demangle"
version = "0.1.16"

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

@ -32,7 +32,7 @@ tree-sitter = "^0.17"
pretty_assertions = "^0.6"
[workspace]
members = ["rust-code-analysis-cli"]
members = ["rust-code-analysis-cli", "rust-code-analysis-web"]
exclude = ["enums"]
[profile.release]

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

@ -11,11 +11,8 @@ license = "MPL-2.0"
name = "rust-code-analysis-cli"
[dependencies]
actix-rt = "^1.0"
actix-web = "^3.2"
clap = "^2.33"
crossbeam = "^0.8"
futures = "^0.3"
globset = "^0.4"
num_cpus = "^1.13"
regex = "^1.4"
@ -26,6 +23,3 @@ serde_json = "^1.0"
serde_yaml = "^0.8"
toml = "^0.5"
walkdir = "^2.2"
[dev-dependencies]
pretty_assertions = "^0.6"

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

@ -2,16 +2,13 @@
extern crate clap;
extern crate crossbeam;
extern crate num_cpus;
#[macro_use]
extern crate serde;
extern crate serde_cbor;
#[cfg_attr(test, macro_use)]
extern crate serde_json;
extern crate serde_yaml;
extern crate toml;
mod formats;
mod web;
use clap::{App, Arg};
use crossbeam::channel::{unbounded, Receiver, Sender};
@ -25,8 +22,8 @@ use std::{process, thread};
use walkdir::{DirEntry, WalkDir};
use formats::Format;
use rust_code_analysis::*;
use web::server;
#[derive(Debug)]
struct Config {
@ -382,25 +379,6 @@ fn main() {
.default_value("")
.takes_value(true),
)
.arg(
Arg::with_name("serve")
.help("Run a web server")
.long("serve"),
)
.arg(
Arg::with_name("host")
.help("Host for the web server")
.long("host")
.default_value("127.0.0.1")
.takes_value(true),
)
.arg(
Arg::with_name("port")
.help("Port for the web server")
.long("port")
.default_value("8080")
.takes_value(true),
)
.arg(
Arg::with_name("warning")
.help("Print the warnings")
@ -409,27 +387,6 @@ fn main() {
)
.get_matches();
let num_jobs = if let Ok(num_jobs) = matches.value_of("num_jobs").unwrap().parse::<usize>() {
num_jobs
} else {
num_cpus::get()
};
let serve = matches.is_present("serve");
if serve {
let host = matches.value_of("host").unwrap();
let port = if let Ok(port) = matches.value_of("port").unwrap().parse::<u16>() {
port
} else {
eprintln!("Invalid port number");
return;
};
if let Err(e) = server::run(host.to_string(), port, num_jobs) {
eprintln!("Cannot run the server at {}:{}: {}", host, port, e);
}
return;
}
let paths: Vec<_> = matches.values_of("paths").unwrap().collect();
let paths: Vec<String> = paths.iter().map(|x| (*x).to_string()).collect();
let dump = matches.is_present("dump");
@ -495,7 +452,12 @@ fn main() {
} else {
get_from_ext(typ)
};
let num_jobs = std::cmp::max(2, num_jobs) - 1;
let num_jobs = if let Ok(num_jobs) = matches.value_of("num_jobs").unwrap().parse::<usize>() {
std::cmp::max(2, num_jobs) - 1
} else {
std::cmp::max(2, num_cpus::get()) - 1
};
let line_start = if let Ok(n) = matches.value_of("line_start").unwrap().parse::<usize>() {
Some(n)

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

@ -0,0 +1,24 @@
[package]
name = "rust-code-analysis-web"
version = "0.0.18"
authors = ["Calixte Denizet <cdenizet@mozilla.com>"]
edition = "2018"
keywords = ["metrics"]
description = "Run a web service to compute and export code metrics"
license = "MPL-2.0"
[[bin]]
name = "rust-code-analysis-web"
[dependencies]
actix-rt = "^1.0"
actix-web = "^3.2"
clap = "^2.33"
futures = "^0.3"
num_cpus = "^1.13"
rust-code-analysis = { path = ".." }
serde = "^1.0"
serde_json = "^1.0"
[dev-dependencies]
pretty_assertions = "^0.6"

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

@ -0,0 +1,60 @@
#[macro_use]
extern crate clap;
extern crate num_cpus;
#[macro_use]
extern crate serde;
#[cfg_attr(test, macro_use)]
extern crate serde_json;
mod web;
use clap::{App, Arg};
use web::server;
fn main() {
let matches = App::new("rust-code-analysis-web")
.version(crate_version!())
.author(&*env!("CARGO_PKG_AUTHORS").replace(':', "\n"))
.about("Run a web server")
.arg(
Arg::with_name("num_jobs")
.help("Number of jobs")
.short("j")
.value_name("NUMBER")
.default_value("")
.takes_value(true),
)
.arg(
Arg::with_name("host")
.help("Host for the web server")
.long("host")
.default_value("127.0.0.1")
.takes_value(true),
)
.arg(
Arg::with_name("port")
.help("Port for the web server")
.long("port")
.default_value("8080")
.takes_value(true),
)
.get_matches();
let num_jobs = if let Ok(num_jobs) = matches.value_of("num_jobs").unwrap().parse::<usize>() {
num_jobs
} else {
num_cpus::get()
};
let host = matches.value_of("host").unwrap();
let port = if let Ok(port) = matches.value_of("port").unwrap().parse::<u16>() {
port
} else {
eprintln!("Invalid port number");
return;
};
if let Err(e) = server::run(host.to_string(), port, num_jobs) {
eprintln!("Cannot run the server at {}:{}: {}", host, port, e);
}
}