This commit is contained in:
2022-07-18 22:57:33 +02:00
parent 86cc2ea889
commit ea61ba9ed7
17 changed files with 1050 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
axum = { version = "0.5.6" }
axum-extra = { version = "0.3.6", features = ["spa"] }
futures = "0.3.21"
tower-http = { version = "0.3.3", features = ["cors", "trace"] }
async-graphql = { version = "4.0.0", features = [
@@ -27,5 +28,6 @@ reqwest = { version = "*", default-features = false, features = [
"rustls-tls",
"json",
] }
hyper = { version = "*" }
scel_core = {path = "../scel_core"}
scel_core = { path = "../scel_core" }

View File

@@ -1,7 +1,7 @@
mod auth;
mod graphql;
use std::{net::SocketAddr, sync::Arc};
use std::{io, net::SocketAddr, sync::Arc};
use async_graphql::{
extensions::{Logger, Tracing},
@@ -16,21 +16,24 @@ use axum::{
headers,
http::{header, Method},
response::{Html, IntoResponse, Redirect},
routing, Extension, Json, Router, TypedHeader,
routing::{self, get_service},
Extension, Json, Router, TypedHeader,
};
use graphql::{
mutation::MutationRoot, query::QueryRoot, schema::ScelSchema, subscription::SubscriptionRoot,
};
use hyper::{client::HttpConnector, Body, StatusCode, Uri};
use scel_core::App;
use serde::{Deserialize, Serialize};
use tower_http::{
cors::CorsLayer,
services::ServeDir,
trace::{DefaultMakeSpan, TraceLayer},
};
async fn graphql_playground() -> impl IntoResponse {
Html(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
GraphQLPlaygroundConfig::new("/graphql").subscription_endpoint("/ws"),
))
}
async fn graphql_handler(
@@ -46,6 +49,8 @@ pub struct Server {
addr: SocketAddr,
}
type Client = hyper::client::Client<HttpConnector, Body>;
impl Server {
pub fn new(app: Arc<App>) -> Server {
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
@@ -64,12 +69,21 @@ impl Server {
];
let app = Router::new()
.route("/", routing::get(graphql_playground).post(graphql_handler))
.route(
"/graphql",
routing::get(graphql_playground).post(graphql_handler),
)
.route("/ws", GraphQLSubscription::new(schema.clone()))
.route("/auth/gitea", routing::get(gitea))
.route("/auth/authorized", routing::get(authorized))
// .merge(axum_extra::routing::SpaRouter::new(
// "/assets",
// "src/web/dist/assets",
// ))
.fallback(get_service(ServeDir::new("./src/web/dist/")).handle_error(handle_error))
.layer(Extension(schema))
.layer(Extension(MemoryStore::new()))
.layer(Extension(Client::new()))
.layer(Extension(auth::oauth_client()))
.layer(
CorsLayer::new()
@@ -77,10 +91,7 @@ impl Server {
.allow_headers([axum::http::header::CONTENT_TYPE])
.allow_methods([Method::GET, Method::POST, Method::OPTIONS]),
)
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::default().include_headers(true)),
);
.layer(TraceLayer::new_for_http().make_span_with(DefaultMakeSpan::default()));
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
@@ -157,3 +168,7 @@ where
Ok(user)
}
}
async fn handle_error(_err: io::Error) -> impl IntoResponse {
(StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong...")
}