Skip to content

Commit

Permalink
Merge pull request #26 from Pugma/refactor/repo
Browse files Browse the repository at this point in the history
ファイル分割の見直し / 関数からimplへの移行
  • Loading branch information
Pugma authored Jul 11, 2024
2 parents 402a006 + 55da0e6 commit 2458561
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 184 deletions.
11 changes: 11 additions & 0 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ tags:
description: related to authentication
- name: user
description: related to user data
- name: group
description: related to group
- name: schedule
description: related to scheduling

Expand Down Expand Up @@ -66,6 +68,15 @@ paths:
401:
description: Not authorized

/group:
post:
summary: make group
tags:
- group
responses:
200:
description: Success

/schedule/{groupId}:
get:
summary: schedule view
Expand Down
9 changes: 9 additions & 0 deletions server/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ once_cell = "1.19.0"
uuid = { version = "1.8.0", features = ["v7"] }
tower-http = {version = "0.5.2", features = ["fs"]}
bcrypt = "0.15.1"
async-session = "3.0.0"
anyhow = "1.0.86"
serde = "1.0.204"

[dependencies.async-sqlx-session]
git = "https://github.com/maxcountryman/async-sqlx-session.git"
default-features = false
branch = "sqlx-0.7"
features = ["mysql"]
138 changes: 0 additions & 138 deletions server/app/src/db.rs

This file was deleted.

42 changes: 20 additions & 22 deletions server/app/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
use axum_extra::extract::CookieJar;
use core::{future::Future, marker, pin};
use sqlx::{mysql::MySqlQueryResult, MySql, Pool};
use sqlx::mysql::MySqlQueryResult;

use crate::db::{self};
use crate::repository::Repository;
use axum::async_trait;
use openapi::{
models::{
GroupItem, PostLogin, ScheduleGroupIdGetPathParams, ScheduleGroupIdPostPathParams,
ScheduleGroupIdPutPathParams, ScheduleItem,
},
Api, LoginPostResponse, MeGetResponse, ScheduleGroupIdGetResponse, ScheduleGroupIdPostResponse,
ScheduleGroupIdPutResponse, SignUpPostResponse,
Api, GroupPostResponse, LoginPostResponse, MeGetResponse, ScheduleGroupIdGetResponse,
ScheduleGroupIdPostResponse, ScheduleGroupIdPutResponse, SignUpPostResponse,
};

#[derive(Clone)]
pub struct Count(pub Pool<MySql>);

impl AsRef<Count> for Count {
fn as_ref(&self) -> &Count {
self
}
}

#[async_trait]
impl Api for Count {
impl Api for Repository {
fn sign_up_post<'life0, 'async_trait>(
&'life0 self,
_method: axum::http::Method,
Expand All @@ -38,13 +29,12 @@ impl Api for Count {
Self: 'async_trait,
{
let db_result: Result<MySqlQueryResult, String> = tokio::task::block_in_place(move || {
tokio::runtime::Handle::current().block_on(async move {
db::add_user(self.0.clone(), body.user_name, body.password).await
})
tokio::runtime::Handle::current()
.block_on(async move { self.add_user(body.user_name, body.password).await })
});

let result = match db_result {
Ok(_) => Ok(SignUpPostResponse::Status200_Success),
core::result::Result::Ok(_) => Ok(SignUpPostResponse::Status200_Success),
Err(a) => {
println!("{}", a);
Ok(SignUpPostResponse::Status400_BadRequest)
Expand All @@ -70,9 +60,8 @@ impl Api for Count {
{
let copied_password: String = body.password.clone();
let db_result: Result<bool, String> = tokio::task::block_in_place(move || {
tokio::runtime::Handle::current().block_on(async move {
db::check_user(self.0.clone(), body.user_name, body.password).await
})
tokio::runtime::Handle::current()
.block_on(async move { self.check_user(body.user_name, body.password).await })
});

let result = match db_result {
Expand Down Expand Up @@ -106,7 +95,7 @@ impl Api for Count {

let db_result: Result<Vec<GroupItem>, String> = tokio::task::block_in_place(move || {
tokio::runtime::Handle::current()
.block_on(async move { db::get_groups_by_user(self.0.clone(), user_name).await })
.block_on(async move { self.get_groups_by_user(user_name).await })
});

let result = match db_result {
Expand All @@ -117,6 +106,15 @@ impl Api for Count {
Box::pin(async { result })
}

async fn group_post(
&self,
_method: axum::http::Method,
_host: axum::extract::Host,
_cookies: CookieJar,
) -> Result<GroupPostResponse, String> {
Ok(GroupPostResponse::Status200_Success)
}

fn schedule_group_id_get<'life0, 'async_trait>(
&'life0 self,
_method: axum::http::Method,
Expand Down
38 changes: 14 additions & 24 deletions server/app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
pub mod db;
pub mod handler;
mod handler;
mod repository;

use anyhow::Ok;

extern crate openapi;
// use sqlx::migrate::Migrator;
// use std::{path::Path, thread};
use std::thread;
use tower_http::services::{ServeDir, ServeFile};

use db::setup_db;

use crate::handler::Count;
use repository::Repository;

#[tokio::main]
async fn main() {
let db_pool = thread::spawn(|| match setup_db() {
Ok(a) => {
println!("Correctly connected to DB!");
a
}
Err(a) => panic!("{a}"),
})
.join()
.expect("Thread panicked");

// let _m = Migrator::new(Path::new("./migrations")).await.unwrap();

let state = Count(db_pool);
async fn main() -> anyhow::Result<()> {
// DB 初期化
let state = Repository::setup_db().await?;
// state.migrate().await?;

// 静的ファイル配信設定
let static_dir = ServeFile::new("/dist/index.html");
let serve_dir_from_assets = ServeDir::new("/dist/assets/");

// 最初に初期化をする
// API 初期化
let app = openapi::server::new(state)
.nest_service("/assets", serve_dir_from_assets)
.fallback_service(static_dir);
Expand All @@ -38,5 +27,6 @@ async fn main() {
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();

// start listening
axum::serve(listener, app).await.unwrap();
axum::serve(listener, app).await?;
Ok(())
}
Loading

0 comments on commit 2458561

Please sign in to comment.