From cd6ecdd3b7eee6f1e792f78739695524b24e93fb Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 17 Jul 2022 20:33:49 -0700 Subject: feat(api): sydney version api --- src/modules/api/sydney.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/modules/api/sydney.rs (limited to 'src/modules/api') diff --git a/src/modules/api/sydney.rs b/src/modules/api/sydney.rs new file mode 100644 index 0000000..1a5e2ae --- /dev/null +++ b/src/modules/api/sydney.rs @@ -0,0 +1,70 @@ +// This file is part of Locus . +// Copyright (C) 2022-2022 Fuwn +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +struct Commit { + sha: String, + url: String, +} + +#[derive(Serialize, Deserialize)] +struct Tags { + pub name: String, + zipball_url: String, + tarball_url: String, + commit: Commit, + node_id: String, +} + +pub fn module(router: &mut windmark::Router) { + crate::route::track_mount( + router, + "/api/sydney/version", + "Sydney's version", + Box::new(move |context| { + let mut content = "0.0.0".to_string(); + + if let Ok(response) = reqwest::blocking::Client::new() + .get("https://api.github.com/repos/gemrest/sydney/tags") + .header( + "User-Agent", + format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")), + ) + .send() + { + if let Ok(response_content) = response.json::>() { + let response_content: Vec = response_content; + + if let Some(first_tag) = response_content.get(0) { + content = first_tag.name.clone(); + } + + if let Some(just_tag) = content.get(1..) { + content = just_tag.to_string(); + } + } + } + + crate::route::cache(&context, &content); + + windmark::Response::Success(content) + }), + ); +} -- cgit v1.2.3