Skip to content

Commit

Permalink
Remove cors and swagger and fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
phughk committed Mar 3, 2024
1 parent 0e5daee commit b33f14d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
59 changes: 31 additions & 28 deletions engine/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use axum::http::Method;
use axum::routing::{post, IntoMakeService};
use axum::{routing::get, Router};
use std::sync::Arc;
use tower_http::cors::{Any, CorsLayer};
use tower_http::cors::{AllowMethods, Any, CorsLayer};
use tracing_subscriber::filter::FilterExt;
use tracing_subscriber::fmt::writer::MakeWriterExt;
use utoipa::OpenApi;
Expand Down Expand Up @@ -46,37 +46,37 @@ pub fn new(repository: LaerningToolRepository) -> ApiInstance {
#[derive(OpenApi)]
#[openapi(
paths(
dataset::dataset_list,
game::game_new,
game::game_list,
game::game_answer,
dataset::dataset_list,
game::game_new,
game::game_list,
game::game_answer,
),
components(
schemas(
dataset::DatasetJson,
AnswerType,
GameJson,
GameListing,
GameListingError,
GameListingErrorResponse,
GameStats,
GameStatus,
NewGameRequest,
QuestionEntry,
NewGameErrorResponse,
NewGameError,
GameAnswerRequest,
)
schemas(
dataset::DatasetJson,
AnswerType,
GameJson,
GameListing,
GameListingError,
GameListingErrorResponse,
GameStats,
GameStatus,
NewGameRequest,
QuestionEntry,
NewGameErrorResponse,
NewGameError,
GameAnswerRequest,
)
),
tags(
(name = "this is a tag name", description = "This is the tag description"))
(name = "this is a tag name", description = "This is the tag description"))
)]
pub struct ApiDoc;

impl ApiInstance {
pub async fn build_router(self) -> Router {
Router::new()
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
// .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
.route(
"/dataset/list",
get(dataset::dataset_list).with_state(self.state.clone()),
Expand All @@ -93,11 +93,14 @@ impl ApiInstance {
}

pub async fn make_server(self) -> IntoMakeService<Router> {
let cors = CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
.allow_methods([Method::GET, Method::POST])
// allow requests from any origin
.allow_origin(Any);
self.build_router().await.layer(cors).into_make_service()
// let cors = CorsLayer::new()
// // allow `GET` and `POST` when accessing the resource
// .allow_methods(AllowMethods::any())
// // allow requests from any origin
// .allow_origin(Any);
self.build_router()
.await
// .layer(cors)
.into_make_service()
}
}
16 changes: 10 additions & 6 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![recursion_limit = "256"]

mod api;
mod repository;
mod xml;
Expand All @@ -7,11 +8,11 @@ use crate::xml::error::Error;
use crate::xml::LearningModule;

use api::cli::ToolArgs;
use axum::Server;
use clap::Parser;
use hyper::Server;
use std::net::SocketAddr;
use std::str::FromStr;
use surrealdb::engine::local::{Db, File, Mem};
use surrealdb::engine::local::{Db, Mem};

use crate::repository::dataset::Dataset;
use crate::repository::{LaerningToolRepository, Repository};
Expand All @@ -22,8 +23,8 @@ async fn load_data(directory: &str) -> Vec<LearningModule> {
}

async fn start_db(addr: Option<String>) -> Surreal<Db> {
let db: Surreal<Db> = if let Some(address) = addr {
Surreal::new::<File>(&*address).await.unwrap()
let db: Surreal<Db> = if let Some(_address) = addr {
Surreal::new::<Mem>(()).await.unwrap()
} else {
Surreal::new::<Mem>(()).await.unwrap()
};
Expand All @@ -43,15 +44,18 @@ async fn start_db(addr: Option<String>) -> Surreal<Db> {
db
}

async fn start_server(repository: LaerningToolRepository, socket_addr: Option<String>) -> Result<(), Error> {
async fn start_server(
repository: LaerningToolRepository,
socket_addr: Option<String>,
) -> Result<(), Error> {
// Create a new Axum router
let api_state = api::new(repository);
let app = api_state.make_server().await;

// Define the address on which the server will listen
let addr = if let Some(address) = socket_addr {
SocketAddr::from_str(&address)
.map_or(SocketAddr::from(([127, 0, 0, 1], 3000)), |address| address)
.map_or(SocketAddr::from(([127, 0, 0, 1], 3000)), |address| address)
} else {
SocketAddr::from(([127, 0, 0, 1], 3000))
};
Expand Down
2 changes: 1 addition & 1 deletion engine/src/xml/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod error;
pub(crate) mod error;
#[cfg(test)]
mod module_browser_test;

Expand Down

0 comments on commit b33f14d

Please sign in to comment.