Skip to content

Commit

Permalink
0.2.x (#17)
Browse files Browse the repository at this point in the history
* 增加docker构建支持
  • Loading branch information
w-sodalite committed Jan 31, 2024
1 parent 3e001c5 commit 3416110
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 4 deletions.
61 changes: 61 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FROM alpine:latest AS build-env

# 修改Alpine镜像地址
RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 设置rustup安装代理环境
ENV RUSTUP_DIST_SERVER="https://rsproxy.cn"
ENV RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"

# 安装依赖库
RUN apk update
RUN apk add -u --no-cache gcc
RUN apk add -u --no-cache musl-dev
RUN apk add -u --no-cache wget

# 安装rustup
RUN mkdir /tmp/run
RUN wget https://rsproxy.cn/rustup-init.sh
RUN sh rustup-init.sh -y --profile minimal --no-modify-path

# 链接rustup和cargo到/usr/local/bin
RUN ln -s $HOME/.cargo/bin/rustup /usr/local/bin/rustup
RUN ln -s $HOME/.cargo/bin/cargo /usr/local/bin/cargo

# 导入cargo配置
COPY proxy_config /
RUN mv /proxy_config $HOME/.cargo/config

WORKDIR /satex

# 复制文件
COPY src ./src
COPY satex-core ./satex-core
COPY satex-serve ./satex-serve
COPY satex-matcher ./satex-matcher
COPY satex-layer ./satex-layer
COPY satex-service ./satex-service
COPY satex-discovery ./satex-discovery
COPY Cargo.toml .
COPY Cargo.lock .

# 编译
RUN cargo build --release

FROM alpine:latest

WORKDIR /satex

# 复制构建文件
COPY --from=build-env /satex/target/release/satex ./bin/

# 复制配置文件
COPY examples/docker/satex.yaml ./conf/
COPY examples/docker/static.yaml ./conf/
COPY examples/resources ./static/

# 暴露端口
EXPOSE 80

# 启动
ENTRYPOINT ["/satex/bin/satex","-c","/satex/conf/satex.yaml"]
19 changes: 19 additions & 0 deletions examples/docker/satex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
satex:
# 日志配置
tracing:
# 日志级别
max_level: info
# 是否使用ANSI
ansi: true
# 是否显示级别
level: true
# 是否显示文件
file: false
# 是否显示线程名称
thread_names: true
# 是否显示行号
line_number: true

# 服务配置
serves:
- /satex/conf/static.yaml
12 changes: 12 additions & 0 deletions examples/docker/static.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 服务配置
server:
# 服务端口
port: 80

# 路由配置
router:
# 路由表
routes:
# 静态文件
- id: static
service: Static=/satex/static
10 changes: 10 additions & 0 deletions proxy_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true
2 changes: 0 additions & 2 deletions satex-discovery/src/lb/make/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ make! {
///
/// 内部API
///
#[doc(hidden)]
#[macro_export]
macro_rules! make_load_balance {
($name:ident $(,)?) => {
satex_core::make_impl!(MakeLoadBalance,LoadBalance,$name);
Expand Down
3 changes: 2 additions & 1 deletion satex-layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ tower-http = { workspace = true, features = [
"trace",
"add-extension",
"cors",
"compression-full"
"compression-full",
"timeout"
] }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion satex-layer/src/make/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl MakeRouteServiceLayer for MakeCompressionLayer {
));
if let Some(exclude_content_types) = config.exclude_content_types {
for exclude_content_type in exclude_content_types {
variants.push(Variant::ExcludeContentType(exclude_content_type));
variants.push(Variant::exclude_content_type(exclude_content_type));
}
}
Ok(layer.compress_when(variants))
Expand Down
1 change: 1 addition & 0 deletions satex-layer/src/make/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod request_body_limit;
pub mod rewrite_path;
pub mod set_header;
pub mod set_status;
pub mod timeout;
pub mod x_forward;

make! {
Expand Down
52 changes: 52 additions & 0 deletions satex-layer/src/make/timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::time::Duration;

use tower_http::timeout::TimeoutLayer;

use satex_core::config::args::Args;
use satex_core::Error;

use crate::make::make_layer;
use crate::MakeRouteServiceLayer;

make_layer! {
Timeout,

#[serde(deserialize_with="satex_core::serde::tot::as_u64")]
secs: u64
}

fn make(args: Args) -> Result<TimeoutLayer, Error> {
let config = Config::try_from(args)?;
Ok(TimeoutLayer::new(Duration::from_secs(config.secs)))
}

#[cfg(test)]
mod test {
use std::convert::Infallible;
use std::time::Duration;

use hyper::{Request, Response};
use tokio::time::sleep;
use tower::{service_fn, Layer, Service};

use satex_core::config::args::{Args, Shortcut};
use satex_core::http::Body;

use crate::make::timeout::MakeTimeoutLayer;
use crate::MakeRouteServiceLayer;

#[tokio::test]
async fn test_layer() {
let args = Args::from(Shortcut::from("3"));
let make = MakeTimeoutLayer::default();
let layer = make.make(args).unwrap();
let service = service_fn(|_: Request<Body>| async move {
sleep(Duration::from_secs(5)).await;
Ok::<_, Infallible>(Response::new(Body::empty()))
});
let mut service = layer.layer(service);
let request = Request::new(Body::empty());
let response = service.call(request).await.unwrap();
assert_eq!(response.status().as_u16(), 408);
}
}
2 changes: 2 additions & 0 deletions satex-layer/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::make::request_body_limit::MakeRequestBodyLimitLayer;
use crate::make::rewrite_path::MakeRewritePathLayer;
use crate::make::set_header::{MakeSetRequestHeaderLayer, MakeSetResponseHeaderLayer};
use crate::make::set_status::MakeSetStatusLayer;
use crate::make::timeout::MakeTimeoutLayer;
use crate::make::x_forward::MakeXForwardLayer;
use crate::{ArcMakeRouteServiceLayer, MakeRouteServiceLayer, NamedRouteServiceLayer};

Expand All @@ -19,6 +20,7 @@ registry!(
NamedRouteServiceLayer,
[
MakeCorsLayer,
MakeTimeoutLayer,
MakeXForwardLayer,
MakeSetStatusLayer,
MakePathStripLayer,
Expand Down

0 comments on commit 3416110

Please sign in to comment.