-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose config through admin api
- Loading branch information
Showing
9 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use async_graphql::SimpleObject; | ||
|
||
#[derive(SimpleObject)] | ||
pub struct Config { | ||
pub sdl: String, | ||
} | ||
|
||
#[derive(SimpleObject)] | ||
pub struct Query { | ||
pub config: Config, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod graphql; | ||
mod server; | ||
|
||
pub use server::AdminServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::net::SocketAddr; | ||
|
||
use anyhow::Result; | ||
use async_graphql::{EmptyMutation, EmptySubscription, Request, Schema}; | ||
use http::{Method, Response, StatusCode}; | ||
use hyper::service::{make_service_fn, service_fn}; | ||
use hyper::Server; | ||
|
||
use super::graphql::{Config, Query}; | ||
use crate::core::async_graphql_hyper::GraphQLResponse; | ||
use crate::core::blueprint::Blueprint; | ||
use crate::core::config::ConfigModule; | ||
use crate::core::Errata; | ||
|
||
#[derive(Debug)] | ||
pub struct AdminServer { | ||
addr: SocketAddr, | ||
sdl: String, | ||
} | ||
|
||
impl AdminServer { | ||
pub fn new(config_module: &ConfigModule) -> Result<Option<Self>> { | ||
if let Some(admin) = config_module.server.admin.as_ref() { | ||
let blueprint = Blueprint::try_from(config_module).map_err(Errata::from)?; | ||
let sdl = crate::core::document::print(config_module.config().into()); | ||
let addr = (blueprint.server.hostname, admin.port.get()).into(); | ||
|
||
Ok(Some(Self { addr, sdl })) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
pub async fn start(self) -> Result<()> { | ||
let server = Server::try_bind(&self.addr)?; | ||
let config = Config { sdl: self.sdl }; | ||
let query = Query { config }; | ||
let schema = Schema::new(query, EmptyMutation, EmptySubscription); | ||
|
||
server | ||
.serve(make_service_fn(|_| { | ||
let schema = schema.clone(); | ||
async move { | ||
Result::<_>::Ok(service_fn(move |req| { | ||
let (parts, body) = req.into_parts(); | ||
let schema = schema.clone(); | ||
|
||
async move { | ||
match parts.method { | ||
Method::POST if parts.uri.path() == "/graphql" => { | ||
let body = hyper::body::to_bytes(body).await?; | ||
let request: Request = serde_json::from_slice(&body)?; | ||
|
||
let res = schema.execute(request).await; | ||
let res = GraphQLResponse::from(res); | ||
|
||
Result::<_>::Ok(res.into_response()?) | ||
} | ||
_ => { | ||
let mut response = Response::default(); | ||
|
||
*response.status_mut() = StatusCode::NOT_FOUND; | ||
|
||
Result::<_>::Ok(response) | ||
} | ||
} | ||
} | ||
})) | ||
} | ||
})) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod admin_server; | ||
pub mod http_1; | ||
pub mod http_2; | ||
pub mod http_server; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters