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)",
"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)",

View file

@ -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 = "*"

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::associations::Identifiable;
use serde_json;
use activity_pub::activity::Activity;

View file

@ -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<ConnectionManager<PgConnection>>;
/// 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::<PgConnection>::new(database_url);
Pool::new(manager).expect("db pool")
let manager = ConnectionManager::<PgConnection>::new(DB_URL.as_str());
Pool::new(manager).expect("DB pool error")
}
fn main() {

View file

@ -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

View file

@ -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

View file

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

View file

@ -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<NewInstanceForm>) -> 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) {

View file

@ -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#"
<?xml version="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}}"/>
</XRD>
"#, domain = domain)
"#, domain = BASE_URL.as_str())
}
#[derive(FromForm)]
@ -34,9 +33,7 @@ fn webfinger(query: WebfingerQuery, conn: DbConn) -> Content<Result<String, &'st
let user = parsed_res.next().unwrap();
let res_dom = parsed_res.next().unwrap();
let domain = Instance::get_local(&*conn).unwrap().public_domain;
if res_dom == domain {
if res_dom == BASE_URL.as_str() {
let res = match User::find_local(&*conn, String::from(user)) {
Some(usr) => Ok(usr.webfinger(&*conn)),
None => match Blog::find_by_actor_id(&*conn, String::from(user)) {

View file

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

View file

@ -7,12 +7,6 @@
<body>
<h1>Configure your instance</h1>
<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>
<input name="name">