cargo fmt
This commit is contained in:
parent
9b8999e0f3
commit
92979a7391
14
src/main.rs
14
src/main.rs
@ -1,6 +1,6 @@
|
|||||||
use clap::{CommandFactory, Parser, Subcommand};
|
use clap::{CommandFactory, Parser, Subcommand};
|
||||||
use clap_complete::Shell;
|
use clap_complete::Shell;
|
||||||
use sea_orm::{Database};
|
use sea_orm::Database;
|
||||||
use sea_orm_migration::prelude::*;
|
use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
#[derive(Parser, Clone)]
|
#[derive(Parser, Clone)]
|
||||||
@ -10,10 +10,10 @@ struct Opts {
|
|||||||
#[clap(short, long = "database", value_parser, env)]
|
#[clap(short, long = "database", value_parser, env)]
|
||||||
database_url: String,
|
database_url: String,
|
||||||
|
|
||||||
#[clap(short, long, value_parser, env, default_value="0.0.0.0")]
|
#[clap(short, long, value_parser, env, default_value = "0.0.0.0")]
|
||||||
address: String,
|
address: String,
|
||||||
|
|
||||||
#[clap(short, long, value_parser, env, default_value="8888")]
|
#[clap(short, long, value_parser, env, default_value = "8888")]
|
||||||
port: u16,
|
port: u16,
|
||||||
|
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
@ -37,9 +37,7 @@ enum Commands {
|
|||||||
#[derive(Subcommand, Clone)]
|
#[derive(Subcommand, Clone)]
|
||||||
enum MigrationDirection {
|
enum MigrationDirection {
|
||||||
/// Migrate database up
|
/// Migrate database up
|
||||||
Up {
|
Up { steps: Option<u32> },
|
||||||
steps: Option<u32>,
|
|
||||||
},
|
|
||||||
/// Do a fresh install
|
/// Do a fresh install
|
||||||
Fresh,
|
Fresh,
|
||||||
/// Remove previous tables and do a fresh install
|
/// Remove previous tables and do a fresh install
|
||||||
@ -72,7 +70,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
if let Some(command) = args.commands.clone() {
|
if let Some(command) = args.commands.clone() {
|
||||||
match command {
|
match command {
|
||||||
Commands::Migrate { direction } => match direction {
|
Commands::Migrate { direction } => match direction {
|
||||||
MigrationDirection::Up{steps} => migrations::Migrator::up(&database, steps).await?,
|
MigrationDirection::Up { steps } => {
|
||||||
|
migrations::Migrator::up(&database, steps).await?
|
||||||
|
}
|
||||||
MigrationDirection::Fresh => migrations::Migrator::fresh(&database).await?,
|
MigrationDirection::Fresh => migrations::Migrator::fresh(&database).await?,
|
||||||
MigrationDirection::Refresh => migrations::Migrator::refresh(&database).await?,
|
MigrationDirection::Refresh => migrations::Migrator::refresh(&database).await?,
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
pub(crate) struct UserController;
|
pub(crate) struct UserController;
|
||||||
|
|
||||||
impl UserController {
|
impl UserController {}
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,31 +1,30 @@
|
|||||||
use actix_web::{HttpServer, App};
|
use actix_web::{App, HttpServer};
|
||||||
use sea_orm::DatabaseConnection;
|
use sea_orm::DatabaseConnection;
|
||||||
|
|
||||||
use crate::Opts;
|
use crate::Opts;
|
||||||
|
|
||||||
mod urls;
|
mod urls;
|
||||||
|
|
||||||
|
mod controller;
|
||||||
mod model;
|
mod model;
|
||||||
mod view;
|
mod view;
|
||||||
mod controller;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
database: DatabaseConnection,
|
database: DatabaseConnection,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn run(database: DatabaseConnection, opts: Opts) -> Result<(), Box<dyn std::error::Error>> {
|
pub(crate) async fn run(
|
||||||
let data = actix_web::web::Data::new(AppState {
|
database: DatabaseConnection,
|
||||||
database: database,
|
opts: Opts,
|
||||||
});
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let data = actix_web::web::Data::new(AppState { database: database });
|
||||||
|
|
||||||
let server = HttpServer::new(move|| {
|
let server =
|
||||||
App::new()
|
HttpServer::new(move || App::new().configure(urls::configure).app_data(data.clone()))
|
||||||
.configure(urls::configure)
|
.bind((opts.address, opts.port))?;
|
||||||
.app_data(data.clone())
|
|
||||||
}).bind((opts.address, opts.port))?;
|
|
||||||
|
|
||||||
let result = server.run().await?;
|
let result = server.run().await?;
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
@ -2,4 +2,4 @@ use actix_web::web::ServiceConfig;
|
|||||||
|
|
||||||
pub(crate) fn configure(cfg: &mut ServiceConfig) -> () {
|
pub(crate) fn configure(cfg: &mut ServiceConfig) -> () {
|
||||||
cfg.service(super::view::index_page);
|
cfg.service(super::view::index_page);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use actix_web::{Responder, HttpRequest};
|
use actix_web::{HttpRequest, Responder};
|
||||||
use sea_orm::*;
|
use sea_orm::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@ -13,10 +13,16 @@ pub struct Params {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::get("/")]
|
#[actix_web::get("/")]
|
||||||
async fn index_page(req: HttpRequest, data: actix_web::web::Data<AppState>) -> Result<impl Responder, Box<dyn std::error::Error>> {
|
async fn index_page(
|
||||||
|
req: HttpRequest,
|
||||||
|
data: actix_web::web::Data<AppState>,
|
||||||
|
) -> Result<impl Responder, Box<dyn std::error::Error>> {
|
||||||
let params = actix_web::web::Query::<Params>::from_query(req.query_string()).unwrap();
|
let params = actix_web::web::Query::<Params>::from_query(req.query_string()).unwrap();
|
||||||
|
|
||||||
let users = User::find().paginate(&data.database, params.posts_per_page.unwrap_or(10)).fetch_page(params.page.unwrap_or(0)).await?;
|
let users = User::find()
|
||||||
|
.paginate(&data.database, params.posts_per_page.unwrap_or(10))
|
||||||
|
.fetch_page(params.page.unwrap_or(0))
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(actix_web::web::Json(users))
|
Ok(actix_web::web::Json(users))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user