5 Commits

Author SHA1 Message Date
xfy
b75173a5b0 add build targets script 2024-05-16 17:15:26 +08:00
xfy
d985c09b8d add cli support
add specifiy config file with `-c`
2024-05-16 16:00:03 +08:00
xfy
6f6b92e18a docker ps test 2024-05-16 14:40:45 +08:00
xfy
63594d0eab fix docker install 2024-05-16 13:49:42 +08:00
a0ac5dbf9b add docker in ci 2024-05-16 05:37:17 +00:00
9 changed files with 168 additions and 6 deletions

View File

@ -4,7 +4,7 @@ stages:
variables:
APP_NAME: "candy"
GIT_CLONE_PATH: "$CI_BUILDS_DIR/$CI_PROJECT_NAME/$CI_PIPELINE_ID"
# GIT_CLONE_PATH: "$CI_BUILDS_DIR/$CI_PROJECT_NAME/$CI_PIPELINE_ID"
image: "rust:latest"
@ -28,8 +28,9 @@ build:freebsd:
stage: build
script:
- apt-get update -y
- apt-get install -y musl-tools libssl-dev
- apt-get install -y musl-tools libssl-dev docker.io
- rustup target add x86_64-unknown-freebsd
- docker ps
- cross build --release --target x86_64-unknown-freebsd
- "mv target/release/$APP_NAME target/release/$APP_NAME-x86_64-unknown-freebsd-$CI_COMMIT_TAG"
- "mv target/release/$APP_NAME-x86_64-unknown-freebsd-$CI_COMMIT_TAG public/"

120
Cargo.lock generated
View File

@ -41,6 +41,55 @@ dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "anstream"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "anyhow"
version = "1.0.83"
@ -134,6 +183,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"async-compression",
"clap",
"config",
"futures-util",
"http",
@ -166,6 +216,52 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]]
name = "config"
version = "0.14.0"
@ -294,6 +390,12 @@ version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -397,6 +499,12 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]]
name = "itoa"
version = "1.0.11"
@ -754,6 +862,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.61"
@ -974,6 +1088,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "valuable"
version = "0.1.0"

View File

@ -26,6 +26,7 @@ futures-util = "0.3.30"
tokio-util = "0.7.11"
async-compression = { version = "0.4.10", features = ["tokio", "zstd", "gzip", "deflate", "brotli"] }
http = "1.1.0"
clap = { version = "4.5.4", features = ["derive"] }
[profile.release]
lto = true

View File

@ -26,5 +26,6 @@
- [ ] FastCGI
- [ ] SSL
- [ ] Cli
- [x] Specific custom config location
- [ ] HTTP 2
- [ ] HTTP 3

27
build.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash
set -xue
rm -rf ./target/release/
rm -rf ./target/debug/
cargo build --release --target x86_64-unknown-linux-gnu
rm -rf ./target/release/
rm -rf ./target/debug/
cargo build --release --target x86_64-unknown-linux-musl
rm -rf ./target/release/
rm -rf ./target/debug/
cross build --release --target aarch64-unknown-linux-gnu
rm -rf ./target/release/
rm -rf ./target/debug/
cross build --release --target aarch64-unknown-linux-musl
rm -rf ./target/release/
rm -rf ./target/debug/
cross build --release --target x86_64-pc-windows-gnu
rm -rf ./target/release/
rm -rf ./target/debug/
cross build --release --target x86_64-unknown-freebsd

9
src/cli.rs Normal file
View File

@ -0,0 +1,9 @@
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
/// Sets a custom config file
#[arg(short, long, value_name = "FILE", default_value = "./config.toml")]
pub config: String,
}

View File

@ -45,8 +45,8 @@ pub struct Settings {
pub host: Vec<SettingHost>,
}
pub fn init_config() -> Result<Settings> {
let file = fs::read_to_string("./config.toml").with_context(|| "read ./config.toml failed")?;
pub fn init_config(path: &str) -> Result<Settings> {
let file = fs::read_to_string(path).with_context(|| format!("read {path} failed"))?;
let mut settings: Settings = toml::from_str(&file)?;
// convert route map

View File

@ -8,7 +8,7 @@ use crate::config::{init_config, MIMEType, Settings};
static SETTINGS: OnceLock<Settings> = OnceLock::new();
pub fn get_settings() -> &'static Settings {
SETTINGS.get_or_init(|| {
init_config()
init_config("./config.toml")
.map_err(|err| {
error!("get_or_init config failed: {err}");
exit(1);

View File

@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use clap::Parser;
use tokio::task::JoinSet;
use tracing::{debug, info};
@ -9,6 +10,7 @@ use crate::{
utils::init_logger,
};
mod cli;
mod config;
mod consts;
mod error;
@ -18,8 +20,9 @@ mod utils;
#[tokio::main]
async fn main() -> Result<()> {
let args = cli::Cli::parse();
init_logger();
init_config().with_context(|| "init config failed")?;
init_config(&args.config).with_context(|| "init config failed")?;
// global config
let settings = get_settings();