### Create a basic HTTP server with fire-http Source: https://github.com/fire-lib/fire-http/blob/master/fire-http/README.md Demonstrates defining a route with global data injection and starting the server on a specified address. ```rust # use fire_http as fire; use fire::{get, Res}; struct GlobalName(String); // handle a simple get request #[get("/")] fn root(global_name: Res) -> String { format!("Hi, this is {}", global_name.0) } #[tokio::main] async fn main() { let mut server = fire::build("0.0.0.0:3000").await .expect("Failed to parse address"); server.add_data(GlobalName("fire".into())); server.add_route(root); server.ignite().await.unwrap(); } ``` -------------------------------- ### Initialize GraphiQL Component Source: https://github.com/fire-lib/fire-http/blob/master/fire-http/src/graphql/graphiql.html Uses ReactDOM to render the GraphiQL component into the DOM with a specified fetcher URL. ```javascript const root = ReactDOM.createRoot(document.getElementById('graphiql')); root.render( React.createElement(GraphiQL, { fetcher: GraphiQL.createFetcher({ url: '{graphql_url}', }), defaultEditorToolsVisibility: true, }), ); ``` -------------------------------- ### Define and Implement a Web API with fire-http-api Source: https://github.com/fire-lib/fire-http/blob/master/fire-http-api/README.md Defines request structures, custom error types, and API handlers, then initializes a server with shared state. ```rust # use fire_http_api as api; use std::fmt; use std::sync::{Arc, Mutex}; use api::{Request, Method}; use api::error::{self, Error as ApiError, StatusCode}; use api::{fire, api}; use fire::impl_res_extractor; use serde::{Serialize, Deserialize}; // -- Type definitions #[derive(Debug, Clone, Serialize)] pub enum Error { Internal(String), Request(String) } impl error::ApiError for Error { fn from_error(e: ApiError) -> Self { match e { ApiError::HeadersMissing(_) | ApiError::Deserialize(_) => { Self::Request(e.to_string()) } e => Self::Internal(e.to_string()), }, } fn status_code(&self) -> StatusCode { match self { Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, Self::Request(_) => StatusCode::BAD_REQUEST } } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self, f) } } #[derive(Debug, Clone, Serialize, Deserialize)] struct NameReq; #[derive(Debug, Clone, Serialize, Deserialize)] struct Name { firstname: String, lastname: String } impl Request for NameReq { type Response = Name; type Error = Error; const PATH: &'static str = "/name"; const METHOD: Method = Method::GET; } #[derive(Debug, Clone, Serialize, Deserialize)] struct SetNameReq { name: Name } impl Request for SetNameReq { type Response = (); type Error = Error; const PATH: &'static str = "/name"; const METHOD: Method = Method::PUT; } // -- implementations struct SharedName(Mutex); impl_res_extractor!(SharedName); #[api(NameReq)] fn get_name(req: NameReq, name: &SharedName) -> Result { let lock = name.0.lock().unwrap(); Ok(lock.clone()) } #[api(SetNameReq)] fn set_name(req: SetNameReq, name: &SharedName) -> Result<(), Error> { let mut lock = name.0.lock().unwrap(); *lock = req.name; Ok(()) } #[tokio::main] async fn main() { let name = SharedName(Mutex::new(Name { firstname: "Albert".into(), lastname: "Einstein".into() })); let mut server = fire::build("0.0.0.0:3000").await .expect("Failed to parse address"); server.add_data(name); server.add_route(get_name); server.add_route(set_name); server.ignite().await.unwrap(); } ``` -------------------------------- ### GraphiQL CSS Styles Source: https://github.com/fire-lib/fire-http/blob/master/fire-http/src/graphql/graphiql.html Defines the layout constraints for the GraphiQL container and body. ```css body { height: 100%; margin: 0; width: 100%; overflow: hidden; } #graphiql { height: 100vh; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.