index : license-api-rs.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2023-10-02 18:00:40.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2023-10-02 18:00:40.0 +00:00:00
commit
094ff04c022024ab33ae99b570a7416af4dcd682 [patch]
tree
2bdd6bedbc0e92c4b0b78ef351041a38a40d5a26
parent
e6ef55e7782e1630dd0abe272900810bd5589bd4
download
094ff04c022024ab33ae99b570a7416af4dcd682.tar.gz

feat: add loading of license database



Diff

 Cargo.toml  |  2 ++
 src/data.rs | 24 ++++++++++++++++++++++++
 src/main.rs | 25 +++++++++++++++++++++----
 3 files changed, 47 insertions(+), 4 deletions(-)

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<License>,
}

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);