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(-)
@@ -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"
@@ -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,
}
@@ -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 = {
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) => {
let mut app = tide::new();
create_routes(&mut app, &config);