Add creation of the initial user to the main func
This commit is contained in:
parent
d3465b4e10
commit
8cb121b568
40
src/main.rs
40
src/main.rs
@ -10,8 +10,10 @@ mod views;
|
||||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||
use actix_web::middleware::Logger;
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
use diesel::prelude::*;
|
||||
|
||||
use dotenv::dotenv;
|
||||
use models::NewUser;
|
||||
use tera::Tera;
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -90,6 +92,44 @@ async fn main() -> std::io::Result<()> {
|
||||
dotenv().ok();
|
||||
env_logger::init();
|
||||
|
||||
let connection = views::establish_connection().expect("Failed to connect to database!");
|
||||
let num_users: i64 = schema::users::dsl::users
|
||||
.select(diesel::dsl::count_star())
|
||||
.first(&connection)
|
||||
.expect("Failed to count the users");
|
||||
|
||||
if num_users < 1 {
|
||||
// It is ok to use expect in this block since it is only run on the start. And if something fails it is probably something major.
|
||||
use schema::users;
|
||||
use std::io::{self, BufRead, Write};
|
||||
warn!("No usere available Creating one!");
|
||||
let sin = io::stdin();
|
||||
|
||||
print!("Please enter the Username of the admin: ");
|
||||
io::stdout().flush().unwrap();
|
||||
let username = sin.lock().lines().next().unwrap().unwrap();
|
||||
|
||||
print!("Please enter the emailadress for {}: ", username);
|
||||
io::stdout().flush().unwrap();
|
||||
let email = sin.lock().lines().next().unwrap().unwrap();
|
||||
|
||||
print!("Please enter the password for {}: ", username);
|
||||
io::stdout().flush().unwrap();
|
||||
let password = sin.lock().lines().next().unwrap().unwrap();
|
||||
println!(
|
||||
"Creating {} ({}) with password {}",
|
||||
&username, &email, &password
|
||||
);
|
||||
|
||||
let new_admin =
|
||||
NewUser::new(username, email, password).expect("Invalid Input failed to create User");
|
||||
|
||||
diesel::insert_into(users::table)
|
||||
.values(&new_admin)
|
||||
.execute(&connection)
|
||||
.expect("Failed to create the user!");
|
||||
}
|
||||
|
||||
println!("Running on: http://127.0.0.1:8156/admin/login/");
|
||||
HttpServer::new(|| {
|
||||
let tera = Tera::new("templates/**/*").expect("failed to initialize the templates");
|
||||
|
@ -29,4 +29,8 @@ table! {
|
||||
joinable!(clicks -> links (link));
|
||||
joinable!(links -> users (author));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(clicks, links, users,);
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
clicks,
|
||||
links,
|
||||
users,
|
||||
);
|
||||
|
@ -16,7 +16,7 @@ use super::forms::LinkForm;
|
||||
use super::models::{Count, Link, LoginUser, NewClick, NewLink, NewUser, User};
|
||||
use crate::ServerError;
|
||||
|
||||
fn establish_connection() -> Result<SqliteConnection, ServerError> {
|
||||
pub(super) fn establish_connection() -> Result<SqliteConnection, ServerError> {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = std::env::var("DATABASE_URL")?;
|
||||
|
@ -14,7 +14,7 @@
|
||||
Kürzel
|
||||
</th>
|
||||
<th>
|
||||
Ziellink
|
||||
Emailadresse
|
||||
</th>
|
||||
<th>
|
||||
Benutzername
|
||||
|
Loading…
Reference in New Issue
Block a user