mirror of
https://github.com/mikedilger/chorus.git
synced 2026-01-03 06:15:33 +00:00
Serve HTTP requests
This commit is contained in:
parent
2c4c9a9264
commit
5ef04aeb12
40
src/main.rs
40
src/main.rs
@ -5,12 +5,15 @@ pub mod error;
|
||||
pub mod globals;
|
||||
pub mod store;
|
||||
pub mod types;
|
||||
pub mod web;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::error::Error;
|
||||
use crate::globals::GLOBALS;
|
||||
use crate::store::Store;
|
||||
use hyper::{Body, Request, Response};
|
||||
use std::env;
|
||||
use std::error::Error as StdError;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Read;
|
||||
use tokio::net::TcpListener;
|
||||
@ -39,13 +42,44 @@ async fn main() -> Result<(), Error> {
|
||||
let _ = GLOBALS.store.set(store);
|
||||
|
||||
// Bind listener to port
|
||||
let _listener = TcpListener::bind((&*config.ip_address, config.port)).await?;
|
||||
let listener = TcpListener::bind((&*config.ip_address, config.port)).await?;
|
||||
log::info!("Running on {}:{}", config.ip_address, config.port);
|
||||
|
||||
// Store config into GLOBALS
|
||||
*GLOBALS.config.write().await = config;
|
||||
|
||||
log::error!("No main yet.");
|
||||
let mut http_server = hyper::server::conn::Http::new();
|
||||
http_server.http1_only(true);
|
||||
http_server.http1_keep_alive(true);
|
||||
|
||||
Ok(())
|
||||
// Accepts network connections and spawn a task to serve each one
|
||||
loop {
|
||||
let (tcp_stream, peer_addr) = listener.accept().await?;
|
||||
|
||||
let connection =
|
||||
http_server.serve_connection(tcp_stream, hyper::service::service_fn(handle_request));
|
||||
|
||||
tokio::spawn(async move {
|
||||
// If our service exits with an error, log the error
|
||||
if let Err(he) = connection.await {
|
||||
if let Some(src) = he.source() {
|
||||
if &*format!("{}", src) == "Transport endpoint is not connected (os error 107)"
|
||||
{
|
||||
// do nothing
|
||||
} else {
|
||||
// Print in detail
|
||||
log::info!("{:?}", src);
|
||||
}
|
||||
} else {
|
||||
// Print in less detail
|
||||
let e: Error = he.into();
|
||||
log::info!("{}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_request(_request: Request<Body>) -> Result<Response<Body>, Error> {
|
||||
web::serve_http().await
|
||||
}
|
||||
|
||||
12
src/web.rs
Normal file
12
src/web.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::error::Error;
|
||||
use hyper::{Body, Response, StatusCode};
|
||||
|
||||
pub async fn serve_http() -> Result<Response<Body>, Error> {
|
||||
let response = Response::builder()
|
||||
.header("Access-Control-Allow-Origin", "*")
|
||||
.header("Access-Control-Allow-Headers", "*")
|
||||
.header("Access-Control-Allow-Methods", "*")
|
||||
.status(StatusCode::OK)
|
||||
.body("This is a nostr relay. Please use a nostr client to connect.".into())?;
|
||||
Ok(response)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user