Skip to content

Commit

Permalink
web: Add first version
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Sep 16, 2024
1 parent 7c15a13 commit 2172381
Show file tree
Hide file tree
Showing 4 changed files with 13,820 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/web/endpoints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use axum::{
extract::Path,
response::{Html, IntoResponse},
Json,
};
use include_dir::{include_dir, Dir};
use serde::Serialize;

static HTML_DIST: Dir = include_dir!("src/web/html");

#[derive(Serialize, Debug, Default)]
pub struct InfoContent {
/// Name of the program
name: String,
/// Version/tag
version: String,
/// Git SHA
sha: String,
build_date: String,
/// Authors name
authors: String,
}

#[derive(Serialize, Debug, Default)]
pub struct Info {
/// Version of the REST API
version: u32,
/// Service information
service: InfoContent,
}

pub async fn root(filename: Option<Path<String>>) -> impl IntoResponse {
let filename = if filename.is_none() {
"index.html".to_string()
} else {
filename.unwrap().0
};

HTML_DIST.get_file(filename).map_or(
Html("File not found".to_string()).into_response(),
|file| {
let content = file.contents_utf8().unwrap_or("");
Html(content.to_string()).into_response()
},
)
}

pub async fn info() -> Json<Info> {
let info = Info {
version: 0,
service: InfoContent {
name: env!("CARGO_PKG_NAME").into(),
version: "0.0.0".into(), //env!("VERGEN_GIT_SEMVER").into(),
sha: env!("VERGEN_GIT_SHA").into(),
build_date: env!("VERGEN_BUILD_TIMESTAMP").into(),
authors: env!("CARGO_PKG_AUTHORS").into(),
},
};

Json(info)
}
52 changes: 52 additions & 0 deletions src/web/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<html>
<head>
<title>mavlink-server</title>
<meta charset="utf-8" />
</head>
<body>
<div id="app" style="display: flex; column-gap: 1em">
<div style="display: flex; column-gap: 1em">
<div>
<div v-if="info">
<div v-once>
{{info.service.name}} - {{info.service.version}} -
{{info.service.sha.substring(0, 7)}} - {{info.service.build_date.substring(0, 10)}}<br/>
By: {{formatAuthors(info.service.authors)}}<br />
</div>
</div>
</div>
</div>
</div>
<script src="vue.global.js"></script>
<script>
const app = Vue.createApp({
mounted () {
fetch('info').then(
function (response) {
response.json().then((info) => this.info = info)
}.bind(this),
)
},
methods: {
formatAuthors (authors) {
const formatted = authors.split(':').map((author) => {
console.log(`author: ${author}`)
const {
groups: { name },
} = /(?<name>.*) <.*>/gm.exec(author)
return name
})
return formatted.join(', ')
},
},
data () {
return {
info: undefined,
}
},
})

app.mount('#app')
</script>
</body>
</html>
Loading

0 comments on commit 2172381

Please sign in to comment.