Skip to content

Commit

Permalink
fix api
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-kali-team committed Jul 31, 2024
1 parent 3109ca1 commit a5f5269
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/post-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ jobs:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/${{ matrix.target }}/debian/observer-ward_${{ needs.create-release.outputs.release_version }}_${{ matrix.target }}.deb
asset_name: observer-ward_${{ needs.create-release.outputs.release_version }}_${{ matrix.target }}.deb
tag: ${{ env.RELEASE_VERSION }}
tag: ${{ needs.create-release.outputs.release_version }}
homebrew:
runs-on: ubuntu-latest
needs:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ Press CTRL+C to quit
➜ ~ ./observer_ward --api-server 127.0.0.1:8000 --token 22e038328151a7a06fd4ebfa63a10228
[INFO ] 📇probes loaded: 6183
[INFO ] 🚀optimized probes: 8
[INFO ] 🌐API service has been started:http://127.0.0.1:8000/v1/observer_ward
[INFO ] 🌐API service has been started: http://127.0.0.1:8000/v1/observer_ward
[INFO ] 📔:curl --request POST \
--url http://127.0.0.1:8000/v1/observer_ward \
--header 'Authorization: Bearer 22e038328151a7a06fd4ebfa63a10228' \
Expand Down Expand Up @@ -494,10 +494,10 @@ Press CTRL+C to quit
➜ ~ ./observer_ward --api-server /tmp/observer_ward.socket
[INFO ] 📇probes loaded: 6183
[INFO ] 🚀optimized probes: 8
[INFO ] 🌐API service has been started:http://127.0.0.1:8000/v1/observer_ward
[INFO ] 🌐API service has been started: /tmp/observer_ward.socket
[INFO ] 📔:curl --request POST \
--url http://127.0.0.1:8000/v1/observer_ward \
--unix-socket /tmp/observer_ward.socket --url http://localhost/v1/observer_ward \
--unix-socket /tmp/observer_ward.socket \
--url http://localhost/v1/observer_ward \
--header 'Authorization: Bearer 22e038328151a7a06fd4ebfa63a10228' \
--json '{"target":["https://httpbin.org/"]}'
[INFO ] 🗳:[result...]
Expand Down
54 changes: 36 additions & 18 deletions observer_ward/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,43 +144,61 @@ pub fn api_server(
.service(get_config_api)
.service(set_config_api)
});
let (http_server, s) = match listening_address {
let (http_server, url) = match &listening_address {
#[cfg(unix)]
UnixSocketAddr::Unix(u) => (
http_server.bind_uds(u)?,
format!(
"--unix-socket {} --url http://localhost/v1/observer_ward",
listening_address
),
"http://localhost/v1/observer_ward".to_string(),
),
UnixSocketAddr::SocketAddr(sa) => {
if let Ok(ssl_config) = ssl {
(
http_server.bind_openssl(sa, ssl_config)?,
format!("--url https://{}/v1/observer_ward", listening_address),
format!("https://{}/v1/observer_ward", listening_address),
)
} else {
(
http_server.bind(sa)?,
format!("--url http://{}/v1/observer_ward", listening_address),
format!("http://{}/v1/observer_ward", listening_address),
)
}
}
};
print_help(&s, token);
print_help(&url, token, listening_address);
rt::System::new().block_on(http_server.workers(config.thread).run())
}

fn print_help(s: &str, t: Option<String>) {
info!("{}API service has been started:{}", Emoji("🌐", ""), s);
let api_doc = format!(
r#"curl --request POST \
{} \
--header 'Authorization: Bearer {}'\
--json '{{"target":["https://httpbin.org/"]}}'"#,
s,
t.unwrap_or_default()
);
fn print_help(url: &str, t: Option<String>, listening_address: &UnixSocketAddr) {
let api_doc = match listening_address {
UnixSocketAddr::Unix(p) => {
info!(
"{}API service has been started: {}",
Emoji("🌐", ""),
p.to_string_lossy()
);
format!(
r#"curl --request POST \
--unix-socket {} \
--url {} \
--header 'Authorization: Bearer {}' \
--json '{{"target":["https://httpbin.org/"]}}'"#,
listening_address,
url,
t.unwrap_or_default()
)
}
UnixSocketAddr::SocketAddr(_) => {
info!("{}API service has been started: {}", Emoji("🌐", ""), url);
format!(
r#"curl --request POST \
--url {} \
--header 'Authorization: Bearer {}' \
--json '{{"target":["https://httpbin.org/"]}}'"#,
url,
t.unwrap_or_default()
)
}
};
let result = r#"[result...]"#;
info!("{}:{}", Emoji("📔", ""), style(api_doc).green());
info!("{}:{}", Emoji("🗳", ""), style(result).green());
Expand Down

0 comments on commit a5f5269

Please sign in to comment.