Use more env vars for config

It will make it easier to test federation
This commit is contained in:
Bat 2018-05-02 12:53:42 +01:00
parent 9fdfb2b25e
commit 5f43f783b6
14 changed files with 30 additions and 31 deletions

2
.env
View File

@ -1 +1 @@
DATABASE_URL=postgres://plume:plume@localhost/plume DB_URL=postgres://plume:plume@localhost/plume

1
Cargo.lock generated
View File

@ -808,6 +808,7 @@ dependencies = [
"dotenv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "rocket 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -8,6 +8,7 @@ bcrypt = "0.2"
dotenv = "*" dotenv = "*"
heck = "0.3.0" heck = "0.3.0"
hex = "0.3" hex = "0.3"
lazy_static = "*"
openssl = "0.10.6" openssl = "0.10.6"
reqwest = "0.8" reqwest = "0.8"
rocket = "*" rocket = "*"

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE instances ADD COLUMN local_domain VARCHAR NOT NULL;

View File

@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE instances DROP COLUMN local_domain;

View File

@ -1,5 +1,4 @@
use diesel::PgConnection; use diesel::PgConnection;
use diesel::associations::Identifiable;
use serde_json; use serde_json;
use activity_pub::activity::Activity; use activity_pub::activity::Activity;

View File

@ -9,6 +9,8 @@ extern crate hex;
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
extern crate dotenv; extern crate dotenv;
#[macro_use]
extern crate lazy_static;
extern crate openssl; extern crate openssl;
extern crate reqwest; extern crate reqwest;
extern crate rocket; extern crate rocket;
@ -33,15 +35,22 @@ mod schema;
mod routes; mod routes;
mod utils; 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<ConnectionManager<PgConnection>>; type PgPool = Pool<ConnectionManager<PgConnection>>;
/// Initializes a database pool. /// Initializes a database pool.
fn init_pool() -> PgPool { fn init_pool() -> PgPool {
dotenv().ok(); dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); let manager = ConnectionManager::<PgConnection>::new(DB_URL.as_str());
let manager = ConnectionManager::<PgConnection>::new(database_url); Pool::new(manager).expect("DB pool error")
Pool::new(manager).expect("db pool")
} }
fn main() { fn main() {

View File

@ -8,7 +8,6 @@ use schema::{instances, users};
#[derive(Identifiable, Queryable)] #[derive(Identifiable, Queryable)]
pub struct Instance { pub struct Instance {
pub id: i32, pub id: i32,
pub local_domain: String,
pub public_domain: String, pub public_domain: String,
pub name: String, pub name: String,
pub local: bool, pub local: bool,
@ -19,7 +18,6 @@ pub struct Instance {
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "instances"] #[table_name = "instances"]
pub struct NewInstance { pub struct NewInstance {
pub local_domain: String,
pub public_domain: String, pub public_domain: String,
pub name: String, pub name: String,
pub local: bool pub local: bool
@ -44,10 +42,9 @@ impl Instance {
Instance::get_local(conn).unwrap().id 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) diesel::insert_into(instances::table)
.values(NewInstance { .values(NewInstance {
local_domain: loc_dom,
public_domain: pub_dom, public_domain: pub_dom,
name: name, name: name,
local: local local: local

View File

@ -10,6 +10,7 @@ use rocket::outcome::IntoOutcome;
use serde_json; use serde_json;
use url::Url; use url::Url;
use BASE_URL;
use activity_pub::activity::Activity; use activity_pub::activity::Activity;
use activity_pub::actor::{ActorType, Actor}; use activity_pub::actor::{ActorType, Actor};
use activity_pub::inbox::Inbox; use activity_pub::inbox::Inbox;
@ -138,7 +139,7 @@ impl User {
let instance = match Instance::get_by_domain(conn, inst.clone()) { let instance = match Instance::get_by_domain(conn, inst.clone()) {
Some(instance) => instance, Some(instance) => instance,
None => { None => {
Instance::insert(conn, String::from(""), inst.clone(), inst.clone(), false) Instance::insert(conn, inst.clone(), inst.clone(), false)
} }
}; };
User::insert(conn, NewUser { User::insert(conn, NewUser {
@ -249,7 +250,7 @@ impl Actor for User {
None => { None => {
// The requested user was not in the DB // The requested user was not in the DB
// We try to fetch it if it is remote // 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()) Some(User::fetch_from_url(conn, url).unwrap())
} else { } else {
None None

View File

@ -36,7 +36,6 @@ struct NewBlogForm {
#[post("/blogs/new", data = "<data>")] #[post("/blogs/new", data = "<data>")]
fn create(conn: DbConn, data: Form<NewBlogForm>, user: User) -> Redirect { fn create(conn: DbConn, data: Form<NewBlogForm>, user: User) -> Redirect {
let inst = Instance::get_local(&*conn).unwrap();
let form = data.get(); let form = data.get();
let slug = utils::make_actor_id(form.title.to_string()); let slug = utils::make_actor_id(form.title.to_string());
@ -44,7 +43,7 @@ fn create(conn: DbConn, data: Form<NewBlogForm>, user: User) -> Redirect {
slug.to_string(), slug.to_string(),
form.title.to_string(), form.title.to_string(),
String::from(""), String::from(""),
inst.id Instance::local_id(&*conn)
)); ));
blog.update_boxes(&*conn); blog.update_boxes(&*conn);

View File

@ -3,6 +3,7 @@ use rocket::response::Redirect;
use rocket_contrib::Template; use rocket_contrib::Template;
use std::collections::HashMap; use std::collections::HashMap;
use BASE_URL;
use db_conn::DbConn; use db_conn::DbConn;
use models::instance::*; use models::instance::*;
@ -25,8 +26,6 @@ fn configure() -> Template {
#[derive(FromForm)] #[derive(FromForm)]
struct NewInstanceForm { struct NewInstanceForm {
local_domain: String,
public_domain: String,
name: String name: String
} }
@ -35,8 +34,7 @@ fn post_config(conn: DbConn, data: Form<NewInstanceForm>) -> Redirect {
let form = data.get(); let form = data.get();
let inst = Instance::insert( let inst = Instance::insert(
&*conn, &*conn,
form.local_domain.to_string(), BASE_URL.as_str().to_string(),
form.public_domain.to_string(),
form.name.to_string(), form.name.to_string(),
true); true);
if inst.has_admin(&*conn) { if inst.has_admin(&*conn) {

View File

@ -1,21 +1,20 @@
use rocket::http::ContentType; use rocket::http::ContentType;
use rocket::response::Content; use rocket::response::Content;
use BASE_URL;
use activity_pub::webfinger::Webfinger; use activity_pub::webfinger::Webfinger;
use db_conn::DbConn; use db_conn::DbConn;
use models::blogs::Blog; use models::blogs::Blog;
use models::instance::Instance;
use models::users::User; use models::users::User;
#[get("/.well-known/host-meta", format = "application/xml")] #[get("/.well-known/host-meta", format = "application/xml")]
fn host_meta(conn: DbConn) -> String { fn host_meta() -> String {
let domain = Instance::get_local(&*conn).unwrap().public_domain;
format!(r#" format!(r#"
<?xml version="1.0"?> <?xml version="1.0"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"> <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Link rel="lrdd" type="application/xrd+xml" template="https://{domain}/.well-known/webfinger?resource={{uri}}"/> <Link rel="lrdd" type="application/xrd+xml" template="https://{domain}/.well-known/webfinger?resource={{uri}}"/>
</XRD> </XRD>
"#, domain = domain) "#, domain = BASE_URL.as_str())
} }
#[derive(FromForm)] #[derive(FromForm)]
@ -34,9 +33,7 @@ fn webfinger(query: WebfingerQuery, conn: DbConn) -> Content<Result<String, &'st
let user = parsed_res.next().unwrap(); let user = parsed_res.next().unwrap();
let res_dom = parsed_res.next().unwrap(); let res_dom = parsed_res.next().unwrap();
let domain = Instance::get_local(&*conn).unwrap().public_domain; if res_dom == BASE_URL.as_str() {
if res_dom == domain {
let res = match User::find_local(&*conn, String::from(user)) { let res = match User::find_local(&*conn, String::from(user)) {
Some(usr) => Ok(usr.webfinger(&*conn)), Some(usr) => Ok(usr.webfinger(&*conn)),
None => match Blog::find_by_actor_id(&*conn, String::from(user)) { None => match Blog::find_by_actor_id(&*conn, String::from(user)) {

View File

@ -32,7 +32,6 @@ table! {
table! { table! {
instances (id) { instances (id) {
id -> Int4, id -> Int4,
local_domain -> Varchar,
public_domain -> Varchar, public_domain -> Varchar,
name -> Varchar, name -> Varchar,
local -> Bool, local -> Bool,

View File

@ -7,12 +7,6 @@
<body> <body>
<h1>Configure your instance</h1> <h1>Configure your instance</h1>
<form method="post"> <form method="post">
<label for="local_domain">Internal domain</label>
<input name="local_domain">
<label for="public_domain">Public domain</label>
<input name="public_domain">
<label for="name">Name</label> <label for="name">Name</label>
<input name="name"> <input name="name">