From 094ff04c022024ab33ae99b570a7416af4dcd682 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Mon, 2 Oct 2023 20:00:40 +0200 Subject: [PATCH] feat: add loading of license database --- Cargo.toml | 2 ++ src/data.rs | 24 ++++++++++++++++++++++++ src/main.rs | 25 +++++++++++++++++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/data.rs diff --git a/Cargo.toml b/Cargo.toml index 36fcb1b..cb79530 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,7 @@ edition = "2021" async-std = { version = "1.12.0", features = ["attributes"] } envy = "0.4.2" jealousy = { version = "0.1.5", features = ["derive"] } +lazy_static = "1.4.0" serde = { version = "1.0.188", features = ["derive"] } +serde_yaml = "0.9.25" tide = "0.16.0" diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..4e1fdcc --- /dev/null +++ b/src/data.rs @@ -0,0 +1,24 @@ +use std::path::Path; + +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct LicenseDatabase { + licenses: Vec, +} + +impl LicenseDatabase { + pub fn load_file(file: &Path) -> LicenseDatabase { + let file_contents = std::fs::read_to_string(file).unwrap(); + let database: LicenseDatabase = serde_yaml::from_str(&file_contents).unwrap(); + + database + } +} + +#[derive(Debug, Deserialize, Clone)] +struct License { + id: String, + filename: String, + description: String, +} diff --git a/src/main.rs b/src/main.rs index 18be213..02f9eae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,14 @@ -use std::net::Ipv4Addr; -use serde::Deserialize; +use data::LicenseDatabase; use jealousy::FromEnv; +use serde::Deserialize; +use std::{net::Ipv4Addr, path::PathBuf, str::FromStr}; use tide::{http::mime, log, Response, Result, Server, StatusCode}; +mod data; + +#[macro_use] +extern crate lazy_static; + #[derive(Debug, Deserialize, FromEnv)] #[from_env(prefix = "LICENSE_API")] struct Config { @@ -29,6 +35,19 @@ impl Config { } } +lazy_static! { + #[derive (Debug)] + static ref DATABASE: LicenseDatabase = { + // TODO: error handling and use log::info when done + // not sure if i should validate the unwrap since main won't start if there is an error... + let config = Config::from_env().unwrap(); + let db_path: PathBuf = PathBuf::from_str(&config.database).unwrap(); + let database: LicenseDatabase = LicenseDatabase::load_file(db_path.as_path()); + + database + }; +} + #[async_std::main] async fn main() -> Result<()> { log::start(); @@ -36,8 +55,6 @@ async fn main() -> Result<()> { let config = Config::from_env(); match config { Ok(config) => { - // TODO: load database and use log::info when done - let mut app = tide::new(); create_routes(&mut app, &config); -- libgit2 1.7.2