Authentication
This commit is contained in:
parent
2722c7d40a
commit
a5e3fb2850
19 changed files with 723 additions and 98 deletions
69
src/main.rs
69
src/main.rs
|
|
@ -1,21 +1,66 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
use samey::get_router;
|
||||
use samey::{create_user, get_router};
|
||||
use sea_orm::Database;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Config {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Run,
|
||||
Migrate,
|
||||
AddUser {
|
||||
#[arg(short, long)]
|
||||
username: String,
|
||||
#[arg(short, long)]
|
||||
password: String,
|
||||
},
|
||||
AddAdminUser {
|
||||
#[arg(short, long)]
|
||||
username: String,
|
||||
#[arg(short, long)]
|
||||
password: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let db = Database::connect("sqlite:db.sqlite3?mode=rwc")
|
||||
.await
|
||||
.expect("Unable to connect to database");
|
||||
Migrator::up(&db, None)
|
||||
.await
|
||||
.expect("Unable to apply migrations");
|
||||
let app = get_router(db, "files")
|
||||
.await
|
||||
.expect("Unable to start router");
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
|
||||
.await
|
||||
.expect("Unable to listen to port");
|
||||
println!("Listening on http://localhost:3000");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
let config = Config::parse();
|
||||
match config.command {
|
||||
Some(Commands::Migrate) => {
|
||||
Migrator::up(&db, None)
|
||||
.await
|
||||
.expect("Unable to apply migrations");
|
||||
}
|
||||
Some(Commands::AddUser { username, password }) => {
|
||||
create_user(db, username, password, false)
|
||||
.await
|
||||
.expect("Unable to add user");
|
||||
}
|
||||
Some(Commands::AddAdminUser { username, password }) => {
|
||||
create_user(db, username, password, true)
|
||||
.await
|
||||
.expect("Unable to add admin");
|
||||
}
|
||||
Some(Commands::Run) | None => {
|
||||
Migrator::up(&db, None)
|
||||
.await
|
||||
.expect("Unable to apply migrations");
|
||||
let app = get_router(db, "files")
|
||||
.await
|
||||
.expect("Unable to start router");
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
|
||||
.await
|
||||
.expect("Unable to listen to port");
|
||||
println!("Listening on http://localhost:3000");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue