From 8cb121b5680ead6201dea39eb65fe9aba9f79a3e Mon Sep 17 00:00:00 2001 From: Dietrich Date: Wed, 10 Feb 2021 09:09:17 +0100 Subject: [PATCH] Add creation of the initial user to the main func --- src/main.rs | 40 ++++++++++++++++++++++++++++++++++++++ src/schema.rs | 6 +++++- src/views.rs | 2 +- templates/index_users.html | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index b564e67..58a490a 100644 --- a/src/main.rs +++ b/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"); diff --git a/src/schema.rs b/src/schema.rs index 93e74f5..7f79273 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -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, +); diff --git a/src/views.rs b/src/views.rs index b98b3c8..7b2e3a3 100644 --- a/src/views.rs +++ b/src/views.rs @@ -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 { +pub(super) fn establish_connection() -> Result { dotenv().ok(); let database_url = std::env::var("DATABASE_URL")?; diff --git a/templates/index_users.html b/templates/index_users.html index ddcf4ed..4931148 100644 --- a/templates/index_users.html +++ b/templates/index_users.html @@ -14,7 +14,7 @@ Kürzel - Ziellink + Emailadresse Benutzername