Compare commits

...

3 Commits

13 changed files with 1814 additions and 12 deletions

1667
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,9 @@ axum = { version = "0.6.18", features = ["http2"] }
clap = { version = "4.3.12", features = ["env", "derive"] }
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"] }

View File

@ -0,0 +1,33 @@
use axum::extract::{Query, State};
use axum::routing::get;
use axum::Json;
use axum::Router;
use sea_orm::entity::EntityTrait;
use sea_orm::query::QueryOrder;
use sea_orm::PaginatorTrait;
use serde::Deserialize;
use crate::db::entities::repo;
#[derive(Deserialize)]
pub struct Pagination {
page: Option<u64>,
per_page: Option<u64>,
}
pub fn router() -> Router<crate::Global> {
Router::new().route("/repos", get(get_repos))
}
async fn get_repos(
State(global): State<crate::Global>,
Query(params): Query<Pagination>,
) -> crate::Result<Json<Vec<repo::Model>>> {
let repos = repo::Entity::find()
.order_by_asc(repo::Column::Id)
.paginate(&global.db, params.per_page.unwrap_or(25))
.fetch_page(params.page.unwrap_or(0))
.await?;
Ok(Json(repos))
}

View File

@ -47,6 +47,8 @@ impl Cli {
pub async fn run(&self) {
self.init_tracing();
let db = crate::db::init("sqlite://test.db").await.unwrap();
let config = Config {
repo_dir: self.repo_dir.clone(),
pkg_dir: self.pkg_dir.clone(),
@ -56,10 +58,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(&global))
.with_state(global)
.layer(TraceLayer::new_for_http());

View File

@ -0,0 +1,5 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
pub mod prelude;
pub mod repo;

View File

@ -0,0 +1,3 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
pub use super::repo::Entity as Repo;

View File

@ -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 {}

View File

@ -0,0 +1,47 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m_20230730_000001_create_repo_tables"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
// Define how to apply this migration: Create the Bakery table.
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Repo::Table)
.col(
ColumnDef::new(Repo::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Repo::Name).string().not_null().unique_key())
.col(ColumnDef::new(Repo::Description).string())
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Repo::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Repo {
Table,
Id,
Name,
Description,
}

View File

@ -0,0 +1,12 @@
use sea_orm_migration::prelude::*;
pub struct Migrator;
mod m20230730_000001_create_repo_tables;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20230730_000001_create_repo_tables::Migration)]
}
}

View File

@ -0,0 +1,17 @@
pub mod entities;
mod migrator;
use migrator::Migrator;
use sea_orm::ConnectOptions;
use sea_orm::Database;
use sea_orm_migration::MigratorTrait;
pub async fn init<C: Into<ConnectOptions>>(
opt: C,
) -> Result<sea_orm::DatabaseConnection, sea_orm::DbErr> {
let db = Database::connect(opt).await?;
Migrator::up(&db, None).await?;
Ok(db)
}

View File

@ -10,6 +10,7 @@ pub type Result<T> = std::result::Result<T, ServerError>;
pub enum ServerError {
IO(io::Error),
Axum(axum::Error),
Db(sea_orm::DbErr),
}
impl fmt::Display for ServerError {
@ -17,6 +18,7 @@ impl fmt::Display for ServerError {
match self {
ServerError::IO(err) => write!(fmt, "{}", err),
ServerError::Axum(err) => write!(fmt, "{}", err),
ServerError::Db(err) => write!(fmt, "{}", err),
}
}
}
@ -28,6 +30,10 @@ impl IntoResponse for ServerError {
match self {
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => {
StatusCode::NOT_FOUND.into_response()
}
ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
}
}
}
@ -49,3 +55,9 @@ impl From<tokio::task::JoinError> for ServerError {
ServerError::IO(err.into())
}
}
impl From<sea_orm::DbErr> for ServerError {
fn from(err: sea_orm::DbErr) -> Self {
ServerError::Db(err)
}
}

View File

@ -1,10 +1,13 @@
mod api;
mod cli;
mod db;
mod error;
mod repo;
use clap::Parser;
pub use error::{Result, ServerError};
use repo::RepoGroupManager;
use sea_orm::DatabaseConnection;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
@ -18,6 +21,7 @@ pub struct Config {
pub struct Global {
config: Config,
repo_manager: Arc<RwLock<RepoGroupManager>>,
db: DatabaseConnection,
}
#[tokio::main]

View File

@ -26,7 +26,6 @@ pub fn router(global: &crate::Global) -> Router<crate::Global> {
delete(delete_package).get(serve_repos.clone()),
)
.fallback(serve_repos)
.with_state(global.clone())
}
async fn post_package_archive(