diff --git a/.env b/.env index c4978f87..ca0310d2 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL=postgres://plume:plume@localhost/plume +DB_URL=postgres://plume:plume@localhost/plume diff --git a/Cargo.lock b/Cargo.lock index 49f848cf..5d642fe9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -808,6 +808,7 @@ dependencies = [ "dotenv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.6 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 44acbf12..f1ae7acc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ bcrypt = "0.2" dotenv = "*" heck = "0.3.0" hex = "0.3" +lazy_static = "*" openssl = "0.10.6" reqwest = "0.8" rocket = "*" diff --git a/migrations/2018-05-02-113930_drop_instance_local_domain/down.sql b/migrations/2018-05-02-113930_drop_instance_local_domain/down.sql new file mode 100644 index 00000000..fb0ef2e7 --- /dev/null +++ b/migrations/2018-05-02-113930_drop_instance_local_domain/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE instances ADD COLUMN local_domain VARCHAR NOT NULL; diff --git a/migrations/2018-05-02-113930_drop_instance_local_domain/up.sql b/migrations/2018-05-02-113930_drop_instance_local_domain/up.sql new file mode 100644 index 00000000..2d78cffa --- /dev/null +++ b/migrations/2018-05-02-113930_drop_instance_local_domain/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE instances DROP COLUMN local_domain; diff --git a/src/activity_pub/inbox.rs b/src/activity_pub/inbox.rs index 739ae69e..fd125be5 100644 --- a/src/activity_pub/inbox.rs +++ b/src/activity_pub/inbox.rs @@ -1,5 +1,4 @@ use diesel::PgConnection; -use diesel::associations::Identifiable; use serde_json; use activity_pub::activity::Activity; diff --git a/src/main.rs b/src/main.rs index 465ce315..7dbf19cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ extern crate hex; #[macro_use] extern crate diesel; extern crate dotenv; +#[macro_use] +extern crate lazy_static; extern crate openssl; extern crate reqwest; extern crate rocket; @@ -33,15 +35,22 @@ mod schema; mod routes; mod utils; +lazy_static! { + pub static ref BASE_URL: String = env::var("BASE_URL") + .unwrap_or(format!("127.0.0.1:{}", env::var("ROCKET_PORT").unwrap_or(String::from("8000")))); + + pub static ref DB_URL: String = env::var("DB_URL") + .unwrap_or(format!("DATABASE_URL=postgres://plume:plume@localhost/{}", env::var("DB_TABLE").unwrap_or(String::from("plume")))); +} + type PgPool = Pool>; /// Initializes a database pool. fn init_pool() -> PgPool { dotenv().ok(); - let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); - let manager = ConnectionManager::::new(database_url); - Pool::new(manager).expect("db pool") + let manager = ConnectionManager::::new(DB_URL.as_str()); + Pool::new(manager).expect("DB pool error") } fn main() { diff --git a/src/models/instance.rs b/src/models/instance.rs index 6eefb78b..1ccc0579 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -8,7 +8,6 @@ use schema::{instances, users}; #[derive(Identifiable, Queryable)] pub struct Instance { pub id: i32, - pub local_domain: String, pub public_domain: String, pub name: String, pub local: bool, @@ -19,7 +18,6 @@ pub struct Instance { #[derive(Insertable)] #[table_name = "instances"] pub struct NewInstance { - pub local_domain: String, pub public_domain: String, pub name: String, pub local: bool @@ -44,10 +42,9 @@ impl Instance { Instance::get_local(conn).unwrap().id } - pub fn insert<'a>(conn: &PgConnection, loc_dom: String, pub_dom: String, name: String, local: bool) -> Instance { + pub fn insert<'a>(conn: &PgConnection, pub_dom: String, name: String, local: bool) -> Instance { diesel::insert_into(instances::table) .values(NewInstance { - local_domain: loc_dom, public_domain: pub_dom, name: name, local: local diff --git a/src/models/users.rs b/src/models/users.rs index 5a4d40a2..9bfcf7da 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -10,6 +10,7 @@ use rocket::outcome::IntoOutcome; use serde_json; use url::Url; +use BASE_URL; use activity_pub::activity::Activity; use activity_pub::actor::{ActorType, Actor}; use activity_pub::inbox::Inbox; @@ -138,7 +139,7 @@ impl User { let instance = match Instance::get_by_domain(conn, inst.clone()) { Some(instance) => instance, None => { - Instance::insert(conn, String::from(""), inst.clone(), inst.clone(), false) + Instance::insert(conn, inst.clone(), inst.clone(), false) } }; User::insert(conn, NewUser { @@ -249,7 +250,7 @@ impl Actor for User { None => { // The requested user was not in the DB // We try to fetch it if it is remote - if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != Instance::get_local(conn).unwrap().public_domain { + if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() { Some(User::fetch_from_url(conn, url).unwrap()) } else { None diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 60d20430..ace2597b 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -36,7 +36,6 @@ struct NewBlogForm { #[post("/blogs/new", data = "")] fn create(conn: DbConn, data: Form, user: User) -> Redirect { - let inst = Instance::get_local(&*conn).unwrap(); let form = data.get(); let slug = utils::make_actor_id(form.title.to_string()); @@ -44,7 +43,7 @@ fn create(conn: DbConn, data: Form, user: User) -> Redirect { slug.to_string(), form.title.to_string(), String::from(""), - inst.id + Instance::local_id(&*conn) )); blog.update_boxes(&*conn); diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 4a3b8863..1a33d7c1 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -3,6 +3,7 @@ use rocket::response::Redirect; use rocket_contrib::Template; use std::collections::HashMap; +use BASE_URL; use db_conn::DbConn; use models::instance::*; @@ -25,8 +26,6 @@ fn configure() -> Template { #[derive(FromForm)] struct NewInstanceForm { - local_domain: String, - public_domain: String, name: String } @@ -35,8 +34,7 @@ fn post_config(conn: DbConn, data: Form) -> Redirect { let form = data.get(); let inst = Instance::insert( &*conn, - form.local_domain.to_string(), - form.public_domain.to_string(), + BASE_URL.as_str().to_string(), form.name.to_string(), true); if inst.has_admin(&*conn) { diff --git a/src/routes/well_known.rs b/src/routes/well_known.rs index ba87a44f..c4977478 100644 --- a/src/routes/well_known.rs +++ b/src/routes/well_known.rs @@ -1,21 +1,20 @@ use rocket::http::ContentType; use rocket::response::Content; +use BASE_URL; use activity_pub::webfinger::Webfinger; use db_conn::DbConn; use models::blogs::Blog; -use models::instance::Instance; use models::users::User; #[get("/.well-known/host-meta", format = "application/xml")] -fn host_meta(conn: DbConn) -> String { - let domain = Instance::get_local(&*conn).unwrap().public_domain; +fn host_meta() -> String { format!(r#" - "#, domain = domain) + "#, domain = BASE_URL.as_str()) } #[derive(FromForm)] @@ -34,9 +33,7 @@ fn webfinger(query: WebfingerQuery, conn: DbConn) -> Content Ok(usr.webfinger(&*conn)), None => match Blog::find_by_actor_id(&*conn, String::from(user)) { diff --git a/src/schema.rs b/src/schema.rs index 155d97e9..2e696d2c 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -32,7 +32,6 @@ table! { table! { instances (id) { id -> Int4, - local_domain -> Varchar, public_domain -> Varchar, name -> Varchar, local -> Bool, diff --git a/templates/instance/configure.tera b/templates/instance/configure.tera index 36a6a921..c9332efb 100644 --- a/templates/instance/configure.tera +++ b/templates/instance/configure.tera @@ -7,12 +7,6 @@

Configure your instance

- - - - - -