Add a basic web service
This commit is contained in:
parent
e934f39d24
commit
38d1cb289e
|
@ -25,7 +25,7 @@ async-circe = { git = "https://git.karx.xyz/circe/async-circe", default-features
|
|||
lazy_static = "1.4"
|
||||
sedregex = "0.2"
|
||||
rusqlite = { version = "0.26", features = ["bundled"] }
|
||||
hyper = "0.14"
|
||||
hyper = { version = "0.14", features = ["server"] }
|
||||
|
||||
[features]
|
||||
tls = ["async-circe/tls"]
|
||||
|
|
|
@ -11,3 +11,6 @@ spotify_client_secret = ""
|
|||
|
||||
# Bot config
|
||||
prefix = "!"
|
||||
|
||||
# Web service config
|
||||
http_listen = "127.0.0.1:8080"
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -1,5 +1,6 @@
|
|||
mod bots;
|
||||
mod database;
|
||||
mod web_service;
|
||||
|
||||
use crate::database::{DbExecutor, ExecutorConnection};
|
||||
use arrayvec::ArrayString;
|
||||
|
@ -13,6 +14,7 @@ use std::fs::File;
|
|||
use std::io::Read;
|
||||
use std::thread;
|
||||
use std::{collections::HashMap, env};
|
||||
use std::net::SocketAddr;
|
||||
use tokio::select;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
|
@ -66,6 +68,7 @@ struct ClientConf {
|
|||
spotify_client_secret: String,
|
||||
prefix: String,
|
||||
db_path: Option<String>,
|
||||
http_listen: Option<SocketAddr>
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
|
@ -114,7 +117,8 @@ async fn main() -> anyhow::Result<()> {
|
|||
db: db_conn,
|
||||
};
|
||||
|
||||
if let Err(e) = executor(state).await {
|
||||
let http_listen = client_config.http_listen.unwrap_or_else(|| SocketAddr::from(([127, 0, 0, 1], 5000)));
|
||||
if let Err(e) = executor(state, http_listen).await {
|
||||
tracing::error!("Error in message loop: {}", e);
|
||||
}
|
||||
|
||||
|
@ -126,8 +130,10 @@ async fn main() -> anyhow::Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn executor(mut state: AppState) -> anyhow::Result<()> {
|
||||
async fn executor(mut state: AppState, http_listen: SocketAddr) -> anyhow::Result<()> {
|
||||
let web_db = state.db.clone();
|
||||
select! {
|
||||
r = web_service::run(web_db, http_listen) => r?,
|
||||
r = message_loop(&mut state) => r?,
|
||||
_ = terminate_signal() => {
|
||||
tracing::info!("Sending QUIT message");
|
||||
|
|
23
src/web_service.rs
Normal file
23
src/web_service.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use std::net::SocketAddr;
|
||||
use crate::ExecutorConnection;
|
||||
use std::convert::Infallible;
|
||||
use std::sync::Arc;
|
||||
use hyper::{Body, Request, Response, Server};
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
|
||||
pub async fn run(db: ExecutorConnection, listen: SocketAddr) -> anyhow::Result<()> {
|
||||
let db = Arc::new(db);
|
||||
|
||||
Server::bind(&listen).serve(make_service_fn(|_| {
|
||||
let db = Arc::clone(&db);
|
||||
async move {
|
||||
Ok::<_, Infallible>(service_fn(move |r| handle(r, Arc::clone(&db))))
|
||||
}
|
||||
})).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle(req: Request<Body>, db: Arc<ExecutorConnection>) -> Result<Response<Body>, Infallible> {
|
||||
Ok(Response::new(Body::from(format!("{:?}", db.get_quote(None).await))))
|
||||
}
|
Loading…
Reference in a new issue