Add files for web apis
This commit is contained in:
parent
9b98d35f26
commit
256885e07f
6 changed files with 42 additions and 6 deletions
0
src/apis/ap.rs
Normal file
0
src/apis/ap.rs
Normal file
0
src/apis/key.rs
Normal file
0
src/apis/key.rs
Normal file
0
src/apis/masto.rs
Normal file
0
src/apis/masto.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
pub mod ap;
|
||||
pub mod key;
|
||||
pub mod oma;
|
||||
pub mod masto;
|
0
src/apis/oma.rs
Normal file
0
src/apis/oma.rs
Normal file
|
@ -8,25 +8,57 @@
|
|||
// See the Licence for the specific language governing permissions and limitations under the Licence.
|
||||
//
|
||||
|
||||
mod diesel_db;
|
||||
pub mod diesel_db;
|
||||
|
||||
use anyhow::{Error, bail};
|
||||
use diesel::{prelude::*, pg::{PgConnection, Pg}};
|
||||
use diesel::{prelude::*, pg::{PgConnection, Pg}, sqlite::Sqlite};
|
||||
use diesel_migrations::{EmbeddedMigrations, embed_migrations, MigrationHarness};
|
||||
|
||||
|
||||
pub const POSTGRES_MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations/postgres");
|
||||
|
||||
pub fn establish_connection(db_url: &str) -> Result<PgConnection, Error> {
|
||||
pub enum DB {
|
||||
Postgres(PgConnection),
|
||||
Sqlite(SqliteConnection)
|
||||
}
|
||||
|
||||
/// Try and connect to a database at the given url
|
||||
/// The url can be a path to an sqlite db file or an url to a postgres db
|
||||
pub fn establish_connection(db_url: &str) -> Result<DB, Error> {
|
||||
if db_url.starts_with("postgres") {
|
||||
Ok(DB::Postgres(establish_connection_postgres(db_url)?))
|
||||
} else {
|
||||
Ok(DB::Sqlite(establish_connection_sqlite(db_url)?))
|
||||
}
|
||||
}
|
||||
|
||||
/// Try and connect to a postgres db
|
||||
pub fn establish_connection_postgres(db_url: &str) -> Result<PgConnection, Error> {
|
||||
//SqliteConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e))
|
||||
let mut conn = PgConnection::establish(db_url)
|
||||
.unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e));
|
||||
run_migrations(&mut conn)?;
|
||||
run_migrations_postgres(&mut conn)?;
|
||||
//c.run_pending_migrations(MIGRATIONS)?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
fn run_migrations(conn: &mut impl MigrationHarness<Pg>) -> Result<(), Error> {
|
||||
fn run_migrations_postgres(conn: &mut impl MigrationHarness<Pg>) -> Result<(), Error> {
|
||||
match conn.run_pending_migrations(POSTGRES_MIGRATIONS) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => bail!("Migrations failed. Error {}", e)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try and connect to a sqlite db
|
||||
pub fn establish_connection_sqlite(db_url: &str) -> Result<SqliteConnection, Error> {
|
||||
//SqliteConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e))
|
||||
let mut conn = SqliteConnection::establish(db_url)
|
||||
.unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e));
|
||||
run_migrations_sqlite(&mut conn)?;
|
||||
//c.run_pending_migrations(MIGRATIONS)?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
fn run_migrations_sqlite(conn: &mut impl MigrationHarness<Sqlite>) -> Result<(), Error> {
|
||||
match conn.run_pending_migrations(POSTGRES_MIGRATIONS) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => bail!("Migrations failed. Error {}", e)
|
||||
|
|
Loading…
Reference in a new issue