Skip to content

Commit

Permalink
feat: add http server support
Browse files Browse the repository at this point in the history
  • Loading branch information
will-we committed Jan 12, 2025
1 parent 81209a2 commit cc0fd84
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
rand = "0.8.5"
base64 = "0.22.1"
tokio = { version = "1.41.0", features = ["rt", "rt-multi-thread", "net", "fs", "tokio-macros"] }
tokio = { version = "1.41.0", features = ["rt", "rt-multi-thread", "net", "fs", "tokio-macros", "macros"] }
axum = { version = "0.7.7", features = ["http2", "query", "tracing"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"
2 changes: 2 additions & 0 deletions src/cli/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum HttpCommand {
pub struct ServeOpts {
#[arg(short, long, default_value = "8080", help = "监听端口")]
pub port: u16,
#[arg(long, default_value = ".", help = "文件根目录")]
pub path: String,
}

#[derive(Parser, Debug)]
Expand Down
26 changes: 23 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use axum::routing::get;
use axum::Router;
use base64::engine::general_purpose::URL_SAFE;
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
Expand All @@ -10,11 +12,13 @@ use rust_cli::opts::{Opts, SubCommand};
use serde_json::Value;
use std::fs;
use std::io::stdin;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use tracing::info;
/// rust-li csv -i input.csv -o output.json -d ","
fn main() -> anyhow::Result<()> {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
info!("Starting rust-li...2");
info!("Starting rust-li...");

let opts = Opts::parse();
match opts.cmd {
Expand Down Expand Up @@ -87,7 +91,9 @@ fn main() -> anyhow::Result<()> {

SubCommand::Http(http_command) => match http_command {
HttpCommand::Serve(opts) => {
println!("Serving HTTP requests on port {}", opts.port);
process_http_requests(opts.port, opts.path)
.await
.expect("server start failed");
Ok(())
}
HttpCommand::Client(opts) => {
Expand All @@ -98,10 +104,24 @@ fn main() -> anyhow::Result<()> {
}
}

async fn process_http_requests(port: u16, path: String) -> anyhow::Result<()> {
info!("Serving HTTP requests on port {}, path {}", port, path);
let app = Router::new().route("/", get(index_handler()));
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port);
let listener = tokio::net::TcpListener::bind(addr).await?;
info!("Listening on http://{}", listener.local_addr()?);
axum::serve(listener, app).await?;
Ok(())
}

fn generate_password(length: u8) -> String {
rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(length as usize)
.map(char::from)
.collect()
}

fn index_handler() -> &'static str {
"hello world, this is from rust server!"
}

0 comments on commit cc0fd84

Please sign in to comment.