Init
This commit is contained in:
commit
c02a4cd1d1
6 changed files with 175 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
Cargo.lock
|
||||
config.toml
|
20
Cargo.toml
Normal file
20
Cargo.toml
Normal file
|
@ -0,0 +1,20 @@
|
|||
[package]
|
||||
name = "droneconf"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
axum = { version = "0.2.8", features = ["headers", "multipart"] }
|
||||
hyper = "0.14.16"
|
||||
tower = { version = "0.4", features = ["util", "timeout"] }
|
||||
tower-http = { version = "0.1", features = ["add-extension"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.51"
|
||||
structopt = "0.3.22"
|
||||
toml = "0.5.8"
|
||||
reqwest = { version = "0.11", features = ["stream", "multipart", "json"] }
|
||||
thiserror = "1.0.30"
|
||||
url = {version = "2.2.2", features = ["serde"]}
|
11
src/config.rs
Normal file
11
src/config.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use reqwest::Url;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub addr: SocketAddr,
|
||||
pub gitea_url: Url,
|
||||
pub config_repo_name: String,
|
||||
}
|
31
src/error.rs
Normal file
31
src/error.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use std::convert::Infallible;
|
||||
|
||||
use axum::{
|
||||
body::{Bytes, Empty},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use hyper::StatusCode;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("File read error: {0}")]
|
||||
Read(#[from] std::io::Error),
|
||||
#[error("Deserialize error: {0}")]
|
||||
Deserialize(#[from] toml::de::Error),
|
||||
#[error("Axum error: {0}")]
|
||||
Axum(#[from] hyper::Error),
|
||||
#[error("Reqwest error: {0}")]
|
||||
Reqwest(#[from] reqwest::Error),
|
||||
#[error("No content error")]
|
||||
NoContent,
|
||||
}
|
||||
|
||||
impl IntoResponse for Error {
|
||||
type Body = Empty<Bytes>;
|
||||
|
||||
type BodyError = Infallible;
|
||||
|
||||
fn into_response(self) -> hyper::Response<Self::Body> {
|
||||
StatusCode::NO_CONTENT.into_response()
|
||||
}
|
||||
}
|
79
src/main.rs
Normal file
79
src/main.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
use std::{path::PathBuf, time::Duration};
|
||||
|
||||
use axum::{
|
||||
extract::Extension, handler::post, response::IntoResponse, AddExtensionLayer, Json, Router,
|
||||
};
|
||||
use config::Config;
|
||||
use error::Error;
|
||||
use model::{APIConfig, Request};
|
||||
use serde_json::Value;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::model::Response;
|
||||
|
||||
mod config;
|
||||
mod error;
|
||||
mod model;
|
||||
|
||||
#[derive(StructOpt)]
|
||||
struct Opt {
|
||||
#[structopt(
|
||||
short,
|
||||
long,
|
||||
help = "config file to use",
|
||||
default_value = "./config.toml"
|
||||
)]
|
||||
config: PathBuf,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
let opt = Opt::from_args();
|
||||
let config = std::fs::read(&opt.config)?;
|
||||
let config = toml::from_slice::<Config>(&config)?;
|
||||
let api_conf = APIConfig(config.gitea_url, config.config_repo_name);
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", post(on_request))
|
||||
.layer(AddExtensionLayer::new(api_conf));
|
||||
|
||||
axum::Server::bind(&config.addr)
|
||||
.serve(app.into_make_service())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn on_request(
|
||||
Json(body): Json<Request>,
|
||||
Extension(APIConfig(base_url, repo_name)): Extension<APIConfig>,
|
||||
) -> Result<impl IntoResponse, Error> {
|
||||
let client = reqwest::ClientBuilder::new()
|
||||
.user_agent("curl")
|
||||
.timeout(Duration::from_secs(30))
|
||||
.build()?;
|
||||
let index_url = format!(
|
||||
"{}/api/v1/repos/{}/{}/raw/index.json",
|
||||
base_url.to_string(),
|
||||
body.namespace(),
|
||||
&repo_name
|
||||
);
|
||||
let res: Value = client.get(index_url).send().await?.json().await?;
|
||||
|
||||
if let Value::Object(obj) = res {
|
||||
let v = obj.get(&body.name()).ok_or(Error::NoContent)?;
|
||||
if let Value::String(path) = v {
|
||||
let conf_url = format!(
|
||||
"{}/api/v1/repos/{}/{}/raw/{}",
|
||||
base_url.to_string(),
|
||||
body.namespace(),
|
||||
&repo_name,
|
||||
path
|
||||
);
|
||||
let drone_config = client.get(conf_url).send().await?.text().await?;
|
||||
let response = Response { data: drone_config };
|
||||
return Ok(Json(response));
|
||||
}
|
||||
}
|
||||
Err(Error::NoContent)
|
||||
}
|
31
src/model.rs
Normal file
31
src/model.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use reqwest::Url;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Request {
|
||||
pub repo: Repository,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Repository {
|
||||
pub name: String,
|
||||
pub namespace: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Response {
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct APIConfig(pub Url, pub String);
|
||||
|
||||
impl Request {
|
||||
pub fn namespace(&self) -> String {
|
||||
self.repo.namespace.clone()
|
||||
}
|
||||
|
||||
pub fn name(&self) -> String {
|
||||
self.repo.name.clone()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue