Skip to content

Commit 9b6c4ce

Browse files
ch604awalilko
andauthored
api documentation (#876)
* api documentation * utoipa openapi docs generator --------- Co-authored-by: Andrej Walilko <awalilko@liquidweb.com>
1 parent 9d50db4 commit 9b6c4ce

File tree

22 files changed

+542
-15
lines changed

22 files changed

+542
-15
lines changed

.github/workflows/main.yml

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
daemon_changed: ${{ steps.files_changed.outputs.daemon_count != '0' }}
2525
daemon_needed: ${{ steps.files_changed.outputs.daemon_count != '0' || steps.files_changed.outputs.installer_build != '0' }}
2626
web_changed: ${{ steps.files_changed.outputs.web_count != '0' }}
27-
docs_changed: ${{ steps.files_changed.outputs.docs_count != '0' }}
27+
docs_changed: ${{ steps.files_changed.outputs.docs_count != '0' || steps.files_changed.outputs.daemon_count != '0' }}
2828
installer_changed: ${{ steps.files_changed.outputs.installer_count != '0' }}
2929
installer_gui_changed: ${{ steps.files_changed.outputs.installer_gui_count != '0' }}
3030
rootshell_needed: ${{ steps.files_changed.outputs.rootshell_count != '0' || steps.files_changed.outputs.installer_build != '0' }}
@@ -84,40 +84,37 @@ jobs:
8484
- uses: actions/checkout@v4
8585
with:
8686
persist-credentials: false
87+
- uses: Swatinem/rust-cache@v2
8788
- name: Install mdBook
8889
run: |
8990
cargo install mdbook --no-default-features --features search --vers "^0.4" --locked
9091
- name: Test mdBook
9192
run: mdbook test
9293

93-
mdbook_publish:
94-
name: Publish mdBook to Github Pages
94+
mdbook_build:
95+
name: Build mdBook for Github Pages
9596
needs: mdbook_test
9697
if: ${{ github.ref == 'refs/heads/main' }}
9798
permissions:
98-
pages: write
9999
contents: write
100-
id-token: write
101100
runs-on: ubuntu-latest
102101
steps:
103102
- uses: actions/checkout@v4
104103
with:
105104
persist-credentials: false
105+
- uses: Swatinem/rust-cache@v2
106106
- name: Install mdBook
107107
run: |
108108
cargo install mdbook --no-default-features --features search --vers "^0.4" --locked
109109
110110
- name: Build mdBook
111111
run: mdbook build
112112

113-
- name: Setup Pages
114-
uses: actions/configure-pages@v4
115113
- name: Upload artifact
116-
uses: actions/upload-pages-artifact@v3
114+
uses: actions/upload-artifact@v4
117115
with:
116+
name: book
118117
path: book
119-
- name: Deploy to Github Pages
120-
uses: actions/deploy-pages@v4
121118

122119
check_and_test:
123120
needs: files_changed
@@ -583,3 +580,57 @@ jobs:
583580
rayhunter-v${{ env.VERSION }}-${{ matrix.platform }}.zip
584581
rayhunter-v${{ env.VERSION }}-${{ matrix.platform }}.zip.sha256
585582
if-no-files-found: error
583+
584+
openapi_build:
585+
if: needs.files_changed.outputs.docs_changed == 'true'
586+
needs:
587+
- files_changed
588+
permissions:
589+
contents: write
590+
runs-on: ubuntu-latest
591+
steps:
592+
- uses: actions/checkout@v4
593+
with:
594+
persist-credentials: false
595+
- uses: dtolnay/rust-toolchain@stable
596+
with:
597+
targets: armv7-unknown-linux-musleabihf
598+
- uses: Swatinem/rust-cache@v2
599+
- name: Build rayhunter-daemon openapi docs
600+
run: |
601+
mkdir -p daemon/web/build
602+
touch daemon/web/build/{favicon.png,index.html.gz,rayhunter_orca_only.png,rayhunter_text.png}
603+
cargo run --bin gen_api --features apidocs -- ./rayhunter-openapi.json
604+
- name: Make swagger folder
605+
run: |
606+
mkdir api-docs
607+
mv doc/swagger-ui.html api-docs/index.html
608+
mv rayhunter-openapi.json api-docs/
609+
- uses: actions/upload-artifact@v4
610+
with:
611+
name: api-docs
612+
path: api-docs
613+
614+
github_pages_publish:
615+
name: Upload new documentation to Github Pages
616+
if: ${{ github.ref == 'refs/heads/main' }}
617+
permissions:
618+
pages: write
619+
contents: write
620+
id-token: write
621+
needs:
622+
- mdbook_build
623+
- openapi_build
624+
runs-on: ubuntu-latest
625+
steps:
626+
- name: Setup Pages
627+
uses: actions/configure-pages@v4
628+
- uses: actions/download-artifact@v4
629+
- name: Organize pages into directory
630+
run: cp -a api-docs book/
631+
- name: Upload pages
632+
uses: actions/upload-pages-artifact@v3
633+
with:
634+
path: book
635+
- name: Deploy Github Pages
636+
uses: actions/deploy-pages@v4

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

daemon/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ version = "0.10.1"
44
edition = "2024"
55
rust-version = "1.88.0"
66

7+
[lib]
8+
name = "rayhunter_daemon"
9+
path = "src/lib.rs"
10+
11+
[[bin]]
12+
name = "gen_api"
13+
path = "src/bin/gen_api.rs"
14+
required-features = ["apidocs"]
15+
716
[features]
817
default = ["rustcrypto-tls"]
918
rustcrypto-tls = ["reqwest/rustls-tls-webpki-roots-no-provider", "dep:rustls-rustcrypto"]
1019
ring-tls = ["reqwest/rustls-tls-webpki-roots"]
20+
apidocs = ["dep:utoipa"]
1121

1222
[dependencies]
1323
rayhunter = { path = "../lib" }
@@ -32,3 +42,4 @@ anyhow = "1.0.98"
3242
reqwest = { version = "0.12.20", default-features = false }
3343
rustls-rustcrypto = { version = "0.0.2-alpha", optional = true }
3444
async-trait = "0.1.88"
45+
utoipa = { version = "5.4.0", optional = true }

daemon/src/analysis.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ impl AnalysisWriter {
7777
}
7878
}
7979

80+
/// The system status relating to QMDL file analysis
8081
#[derive(Debug, Serialize, Clone)]
82+
#[cfg_attr(feature = "apidocs", derive(utoipa::ToSchema))]
8183
pub struct AnalysisStatus {
84+
/// The vector array of queued files
8285
queued: Vec<String>,
86+
/// The file currently being analyzed
8387
running: Option<String>,
88+
/// The vector array of finished files
8489
finished: Vec<String>,
8590
}
8691

@@ -215,6 +220,16 @@ pub fn run_analysis_thread(
215220
});
216221
}
217222

223+
#[cfg_attr(feature = "apidocs", utoipa::path(
224+
get,
225+
path = "/api/analysis",
226+
tag = "Recordings",
227+
responses(
228+
(status = StatusCode::OK, description = "Success", body = AnalysisStatus)
229+
),
230+
summary = "Analysis status",
231+
description = "Show analysis status for all QMDL files."
232+
))]
218233
pub async fn get_analysis_status(
219234
State(state): State<Arc<ServerState>>,
220235
) -> Result<Json<AnalysisStatus>, (StatusCode, String)> {
@@ -231,6 +246,20 @@ fn queue_qmdl(name: &str, analysis_status: &mut RwLockWriteGuard<AnalysisStatus>
231246
true
232247
}
233248

249+
#[cfg_attr(feature = "apidocs", utoipa::path(
250+
post,
251+
path = "/api/analysis/{name}",
252+
tag = "Recordings",
253+
responses(
254+
(status = StatusCode::ACCEPTED, description = "Success"),
255+
(status = StatusCode::INTERNAL_SERVER_ERROR, description = "Unable to queue analysis file")
256+
),
257+
params(
258+
("name" = String, Path, description = "QMDL file to analyze")
259+
),
260+
summary = "Start analysis",
261+
description = "Begin analysis of QMDL file {name}."
262+
))]
234263
pub async fn start_analysis(
235264
State(state): State<Arc<ServerState>>,
236265
Path(qmdl_name): Path<String>,

daemon/src/battery/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ pub mod wingtech;
1818

1919
const LOW_BATTERY_LEVEL: u8 = 10;
2020

21+
/// Device battery information
2122
#[derive(Clone, Copy, PartialEq, Debug, Serialize)]
23+
#[cfg_attr(feature = "apidocs", derive(utoipa::ToSchema))]
2224
pub struct BatteryState {
25+
/// The current level in percentage of the device battery
2326
level: u8,
27+
/// A boolean indicating whether the battery is currently being charged
2428
is_plugged_in: bool,
2529
}
2630

daemon/src/bin/gen_api.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::{env, fs};
2+
3+
fn main() {
4+
let content = rayhunter_daemon::ApiDocs::generate();
5+
let mut filename = "openapi.json".to_string();
6+
let args: Vec<String> = env::args().collect();
7+
if args.len() > 1 {
8+
filename = args[1].to_string();
9+
}
10+
11+
fs::write(filename, content).unwrap();
12+
}

daemon/src/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,30 @@ use rayhunter::analysis::analyzer::AnalyzerConfig;
77
use crate::error::RayhunterError;
88
use crate::notifications::NotificationType;
99

10+
/// The structure of a valid rayhunter configuration
1011
#[derive(Debug, Clone, Deserialize, Serialize)]
1112
#[serde(default)]
13+
#[cfg_attr(feature = "apidocs", derive(utoipa::ToSchema))]
1214
pub struct Config {
15+
/// Path to store QMDL files
1316
pub qmdl_store_path: String,
17+
/// Listening port
1418
pub port: u16,
19+
/// Debug mode
1520
pub debug_mode: bool,
21+
/// Internal device name
1622
pub device: Device,
23+
/// UI level
1724
pub ui_level: u8,
25+
/// Colorblind mode
1826
pub colorblind_mode: bool,
27+
/// Key input mode
1928
pub key_input_mode: u8,
29+
/// ntfy.sh URL
2030
pub ntfy_url: Option<String>,
31+
/// Vector containing the types of enabled notifications
2132
pub enabled_notifications: Vec<NotificationType>,
33+
/// Vector containing the list of enabled analyzers
2234
pub analyzers: AnalyzerConfig,
2335
pub min_space_to_start_recording_mb: u64,
2436
pub min_space_to_continue_recording_mb: u64,

0 commit comments

Comments
 (0)