feat(server): start api using CRUD operations
							parent
							
								
									e08048d0f0
								
							
						
					
					
						commit
						37218536c5
					
				| 
						 | 
				
			
			@ -1629,6 +1629,7 @@ dependencies = [
 | 
			
		|||
 "libarchive",
 | 
			
		||||
 "sea-orm",
 | 
			
		||||
 "sea-orm-migration",
 | 
			
		||||
 "serde",
 | 
			
		||||
 "sha256",
 | 
			
		||||
 "tokio",
 | 
			
		||||
 "tokio-util",
 | 
			
		||||
| 
						 | 
				
			
			@ -1965,18 +1966,18 @@ checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
 | 
			
		|||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "serde"
 | 
			
		||||
version = "1.0.171"
 | 
			
		||||
version = "1.0.178"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9"
 | 
			
		||||
checksum = "60363bdd39a7be0266a520dab25fdc9241d2f987b08a01e01f0ec6d06a981348"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "serde_derive",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "serde_derive"
 | 
			
		||||
version = "1.0.171"
 | 
			
		||||
version = "1.0.178"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "389894603bd18c46fa56231694f8d827779c0951a667087194cf9de94ed24682"
 | 
			
		||||
checksum = "f28482318d6641454cb273da158647922d1be6b5a2fcc6165cd89ebdd7ed576b"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "proc-macro2",
 | 
			
		||||
 "quote",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,6 +13,7 @@ futures = "0.3.28"
 | 
			
		|||
libarchive = { path = "../libarchive" }
 | 
			
		||||
sea-orm = { version = "0.12.1", features = ["sqlx-sqlite", "runtime-tokio-rustls", "macros"] }
 | 
			
		||||
sea-orm-migration = "0.12.1"
 | 
			
		||||
serde = { version = "1.0.178", features = ["derive"] }
 | 
			
		||||
sha256 = "1.1.4"
 | 
			
		||||
tokio = { version = "1.29.1", features = ["full"] }
 | 
			
		||||
tokio-util = { version = "0.7.8", features = ["io"] }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,19 @@
 | 
			
		|||
use axum::Router;
 | 
			
		||||
use axum::extract::State;
 | 
			
		||||
use axum::routing::get;
 | 
			
		||||
use sea_orm::entity::EntityTrait;
 | 
			
		||||
use sea_orm::query::QueryOrder;
 | 
			
		||||
use axum::Json;
 | 
			
		||||
 | 
			
		||||
use crate::db::entities::repo;
 | 
			
		||||
 | 
			
		||||
pub fn router() -> Router<crate::Global> {
 | 
			
		||||
    Router::new()
 | 
			
		||||
        .route("/repos", get(get_repos))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn get_repos(State(global): State<crate::Global>) -> crate::Result<Json<Vec<repo::Model>>> {
 | 
			
		||||
    let repos = repo::Entity::find().order_by_asc(repo::Column::Id).all(&global.db).await?;
 | 
			
		||||
 | 
			
		||||
    Ok(Json(repos))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -56,10 +56,12 @@ impl Cli {
 | 
			
		|||
        let global = Global {
 | 
			
		||||
            config,
 | 
			
		||||
            repo_manager: Arc::new(RwLock::new(repo_manager)),
 | 
			
		||||
            db
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        // build our application with a single route
 | 
			
		||||
        let app = Router::new()
 | 
			
		||||
            .nest("/api", crate::api::router())
 | 
			
		||||
            .merge(crate::repo::router())
 | 
			
		||||
            .with_state(global)
 | 
			
		||||
            .layer(TraceLayer::new_for_http());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
 | 
			
		||||
 | 
			
		||||
pub mod prelude;
 | 
			
		||||
 | 
			
		||||
pub mod repo;
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
 | 
			
		||||
 | 
			
		||||
pub use super::repo::Entity as Repo;
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
 | 
			
		||||
 | 
			
		||||
use sea_orm::entity::prelude::*;
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize)]
 | 
			
		||||
#[sea_orm(table_name = "repo")]
 | 
			
		||||
pub struct Model {
 | 
			
		||||
    #[sea_orm(primary_key)]
 | 
			
		||||
    pub id: i32,
 | 
			
		||||
    pub name: String,
 | 
			
		||||
    pub description: Option<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
 | 
			
		||||
pub enum Relation {}
 | 
			
		||||
 | 
			
		||||
impl ActiveModelBehavior for ActiveModel {}
 | 
			
		||||
| 
						 | 
				
			
			@ -23,7 +23,7 @@ impl MigrationTrait for Migration {
 | 
			
		|||
                            .auto_increment()
 | 
			
		||||
                            .primary_key(),
 | 
			
		||||
                    )
 | 
			
		||||
                    .col(ColumnDef::new(Repo::Name).string().not_null())
 | 
			
		||||
                    .col(ColumnDef::new(Repo::Name).string().not_null().unique_key())
 | 
			
		||||
                    .col(ColumnDef::new(Repo::Description).string())
 | 
			
		||||
                    .to_owned(),
 | 
			
		||||
            )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,5 @@
 | 
			
		|||
mod migrator;
 | 
			
		||||
pub mod entities;
 | 
			
		||||
 | 
			
		||||
use migrator::Migrator;
 | 
			
		||||
use sea_orm::ConnectOptions;
 | 
			
		||||
| 
						 | 
				
			
			@ -8,7 +9,7 @@ use sea_orm_migration::MigratorTrait;
 | 
			
		|||
pub async fn init<C: Into<ConnectOptions>>(
 | 
			
		||||
    opt: C,
 | 
			
		||||
) -> Result<sea_orm::DatabaseConnection, sea_orm::DbErr> {
 | 
			
		||||
    let mut db = Database::connect(opt).await?;
 | 
			
		||||
    let db = Database::connect(opt).await?;
 | 
			
		||||
 | 
			
		||||
    Migrator::refresh(&db).await?;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,6 +11,7 @@ pub enum ServerError {
 | 
			
		|||
    IO(io::Error),
 | 
			
		||||
    Axum(axum::Error),
 | 
			
		||||
    Status(StatusCode),
 | 
			
		||||
    Db(sea_orm::DbErr),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl fmt::Display for ServerError {
 | 
			
		||||
| 
						 | 
				
			
			@ -19,6 +20,7 @@ impl fmt::Display for ServerError {
 | 
			
		|||
            ServerError::IO(err) => write!(fmt, "{}", err),
 | 
			
		||||
            ServerError::Axum(err) => write!(fmt, "{}", err),
 | 
			
		||||
            ServerError::Status(status) => write!(fmt, "{}", status),
 | 
			
		||||
            ServerError::Db(err) => write!(fmt, "{}", err),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -33,6 +35,8 @@ impl IntoResponse for ServerError {
 | 
			
		|||
            ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
 | 
			
		||||
            ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
 | 
			
		||||
            ServerError::Status(status) => status.into_response(),
 | 
			
		||||
            ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => StatusCode::NOT_FOUND.into_response(),
 | 
			
		||||
            ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -60,3 +64,9 @@ impl From<StatusCode> for ServerError {
 | 
			
		|||
        Self::Status(status)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<sea_orm::DbErr> for ServerError {
 | 
			
		||||
    fn from(err: sea_orm::DbErr) -> Self {
 | 
			
		||||
        ServerError::Db(err)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,12 +2,14 @@ mod cli;
 | 
			
		|||
mod db;
 | 
			
		||||
mod error;
 | 
			
		||||
mod repo;
 | 
			
		||||
mod api;
 | 
			
		||||
 | 
			
		||||
use clap::Parser;
 | 
			
		||||
pub use error::{Result, ServerError};
 | 
			
		||||
use repo::RepoGroupManager;
 | 
			
		||||
use std::path::PathBuf;
 | 
			
		||||
use std::sync::{Arc, RwLock};
 | 
			
		||||
use sea_orm::DatabaseConnection;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub struct Config {
 | 
			
		||||
| 
						 | 
				
			
			@ -19,6 +21,7 @@ pub struct Config {
 | 
			
		|||
pub struct Global {
 | 
			
		||||
    config: Config,
 | 
			
		||||
    repo_manager: Arc<RwLock<RepoGroupManager>>,
 | 
			
		||||
    db: DatabaseConnection
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue