wip: fixes to last commit to successfully run

This commit is contained in:
meeg_leeto 2022-04-08 22:47:09 +01:00
parent 4d83500fed
commit 703a52ea8f
5 changed files with 29 additions and 32 deletions

13
Cargo.lock generated
View File

@ -259,7 +259,7 @@ dependencies = [
"atomic",
"pear",
"serde",
"toml",
"serde_json",
"uncased",
"version_check",
]
@ -584,8 +584,8 @@ dependencies = [
"figment",
"redis",
"serde",
"serde_json",
"tokio",
"toml",
"validators",
"warp",
]
@ -1296,15 +1296,6 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.1"

View File

@ -8,10 +8,10 @@ edition = "2021"
[dependencies]
argh = "0.1.7"
base64 = "0.13.0"
figment = { version = "0.10.6", features = ["toml", "env"] }
figment = { version = "0.10.6", features = ["json", "env"] }
redis = { version = "~0.21.5", features = ["tokio-comp"] }
serde = { version = "~1.0.136", features = ["derive"] }
serde_json = "1.0.79"
tokio = { version = "~1.17.0", features = ["full"] }
toml = "0.5.8"
validators = { version = "~0.24.1", features = ["url-dep"] }
warp = "0.3.2"

12
data/config.json Normal file
View File

@ -0,0 +1,12 @@
{
"db": {
"address": "redis://127.0.0.1"
},
"slug_rules": {
"length": 5,
"chars": "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
},
"serve_rules": {
"Dir": "/etc/lonk/served"
}
}

View File

@ -4,7 +4,7 @@ services:
build: .
environment:
- PROFILE: release
- LONK_CONFIG: /data/config.toml
- LONK_CONFIG: /data/config.json
volumes:
- ./data:/data
redis:

View File

@ -1,11 +1,8 @@
use argh::FromArgs;
use figment::{
providers::{Format, Toml},
Figment,
};
use figment::{providers::Format, Figment};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeSet, path::PathBuf, str::FromStr, sync::Arc};
use tokio::sync::mpsc::{self, UnboundedSender};
use tokio::sync::mpsc::UnboundedSender;
use validators::prelude::*;
use warp::{filters::BoxedFilter, hyper::StatusCode, Filter};
@ -19,7 +16,7 @@ macro_rules! unwrap_or_unwrap_err {
}
#[derive(Serialize, Deserialize, Debug, Validator, Clone)]
#[validator(domain(ipv4(Allow), local(NotAllow), at_least_two_labels(Allow), port(Allow)))]
#[validator(domain(ipv4(Allow), local(Allow), at_least_two_labels(Allow), port(Allow)))]
struct Url {
domain: String,
port: Option<u16>,
@ -27,12 +24,14 @@ struct Url {
#[derive(Deserialize, Serialize, Debug, Clone)]
struct DbConfig {
pub address: Url,
pub address: String,
}
impl Default for DbConfig {
fn default() -> Self {
Self { address: Url::parse_str("redis://127.0.0.1/").unwrap() }
Self {
address: "redis://127.0.0.1".to_string(),
}
}
}
@ -187,9 +186,9 @@ fn shorten(
#[tokio::main]
async fn serve() {
// Read configuration
let config_file = std::env::var("LONK_CONFIG").unwrap_or("lonk.toml".to_string());
let config_file = std::env::var("LONK_CONFIG").unwrap_or("lonk.json".to_string());
let config: Config = Figment::new()
.merge(Toml::file(&config_file))
.merge(figment::providers::Json::file(&config_file))
.extract()
.expect("Could not parse configuration file.");
@ -198,12 +197,7 @@ async fn serve() {
// Initialize database
let db = {
let client = if let Some(port) = config.db.address.port {
redis::Client::open((config.db.address.domain, port))
} else {
redis::Client::open(config.db.address.domain)
};
let client = client.expect("Error opening Redis database.");
let client = redis::Client::open(config.db.address).expect("Error opening Redis database.");
//let conn = Connection::open(config.db_location).expect("Could not open database.");
Arc::new(SlugDatabase::from_client(client))
};
@ -245,8 +239,8 @@ fn main() {
if run.print_default_config {
println!(
"{}",
toml::to_string(&Config::default())
.expect("Default configuration should always be TOML serializable")
serde_json::to_string_pretty(&Config::default())
.expect("Default configuration should always be JSON serializable")
);
} else {
serve();