Add creation of the initial user to the main func

This commit is contained in:
Dietrich 2021-02-10 09:09:17 +01:00
parent d3465b4e10
commit 8cb121b568
Signed by: dietrich
GPG Key ID: 9F3C20C0F85DF67C
4 changed files with 47 additions and 3 deletions

View File

@ -10,8 +10,10 @@ mod views;
use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::middleware::Logger; use actix_web::middleware::Logger;
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer};
use diesel::prelude::*;
use dotenv::dotenv; use dotenv::dotenv;
use models::NewUser;
use tera::Tera; use tera::Tera;
#[derive(Debug)] #[derive(Debug)]
@ -90,6 +92,44 @@ async fn main() -> std::io::Result<()> {
dotenv().ok(); dotenv().ok();
env_logger::init(); 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/"); println!("Running on: http://127.0.0.1:8156/admin/login/");
HttpServer::new(|| { HttpServer::new(|| {
let tera = Tera::new("templates/**/*").expect("failed to initialize the templates"); let tera = Tera::new("templates/**/*").expect("failed to initialize the templates");

View File

@ -29,4 +29,8 @@ table! {
joinable!(clicks -> links (link)); joinable!(clicks -> links (link));
joinable!(links -> users (author)); joinable!(links -> users (author));
allow_tables_to_appear_in_same_query!(clicks, links, users,); allow_tables_to_appear_in_same_query!(
clicks,
links,
users,
);

View File

@ -16,7 +16,7 @@ use super::forms::LinkForm;
use super::models::{Count, Link, LoginUser, NewClick, NewLink, NewUser, User}; use super::models::{Count, Link, LoginUser, NewClick, NewLink, NewUser, User};
use crate::ServerError; use crate::ServerError;
fn establish_connection() -> Result<SqliteConnection, ServerError> { pub(super) fn establish_connection() -> Result<SqliteConnection, ServerError> {
dotenv().ok(); dotenv().ok();
let database_url = std::env::var("DATABASE_URL")?; let database_url = std::env::var("DATABASE_URL")?;

View File

@ -14,7 +14,7 @@
Kürzel Kürzel
</th> </th>
<th> <th>
Ziellink Emailadresse
</th> </th>
<th> <th>
Benutzername Benutzername