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

View File

@ -8,10 +8,10 @@ edition = "2021"
[dependencies] [dependencies]
argh = "0.1.7" argh = "0.1.7"
base64 = "0.13.0" 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"] } redis = { version = "~0.21.5", features = ["tokio-comp"] }
serde = { version = "~1.0.136", features = ["derive"] } serde = { version = "~1.0.136", features = ["derive"] }
serde_json = "1.0.79"
tokio = { version = "~1.17.0", features = ["full"] } tokio = { version = "~1.17.0", features = ["full"] }
toml = "0.5.8"
validators = { version = "~0.24.1", features = ["url-dep"] } validators = { version = "~0.24.1", features = ["url-dep"] }
warp = "0.3.2" 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: . build: .
environment: environment:
- PROFILE: release - PROFILE: release
- LONK_CONFIG: /data/config.toml - LONK_CONFIG: /data/config.json
volumes: volumes:
- ./data:/data - ./data:/data
redis: redis:

View File

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