changeset 698: |
96958d3eb5b0 |
parent: |
94d358919982
|
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Fri, 04 Oct 2024 22:04:59 -0400 |
permissions: |
-rw-r--r-- |
description: |
fixes |
1 /// app/cli/alik/lib.rs --- Alik Lib 11 use db::{rocksdb, Db, DbConfigExt}; 16 http::{HeaderMap, HeaderName, HeaderValue, StatusCode}, 17 response::{IntoResponse, Response}, 21 http::tower::trace::TraceLayer, 25 use serde::{Deserialize, Serialize}; 30 path::{Path, PathBuf}, 34 pub use krypt::KryptConfig; 36 use logger::{info, log, tracing::Span}; 38 use obj::{Configure, NetworkConfig, Objective}; 40 use std::time::Duration; 42 #[derive(Serialize, Deserialize, Clone, Debug, Default)] 43 pub struct AlikConfig { 45 db_path: Option<PathBuf>, 46 db_opts: HashMap<String, String>, 50 pub fn new() -> Self { 53 pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, obj::Error> { 54 let s = fs::read_to_string(path)?; 55 AlikConfig::from_json_str(&s) 59 obj::impl_config!(AlikConfig); 61 impl DbConfigExt for AlikConfig { 62 fn db_path(self) -> Option<PathBuf> { 65 fn db_user(self) -> Option<String> { 68 fn set_db_config_value(&mut self, key: &str, val: &str) -> Option<String> { 69 self.db_opts.insert(key.to_string(), val.to_string()) 71 fn get_db_config_value(self, key: &str) -> Option<String> { 72 self.db_opts.get(key).cloned() 76 pub trait AlikService {} 80 db: Option<rocksdb::DB>, 81 config: Arc<AlikConfig>, 82 router: Option<Router>, 86 pub fn new() -> Alik { 89 config: Arc::new(AlikConfig::new()), 93 pub fn with_config(cfg: &AlikConfig) -> Alik { 96 config: Arc::new(cfg.to_owned()), 100 pub fn network_init(&mut self) -> Result<(), net::Error> { 101 let socket = &self.config.net.socket; 102 let peers = self.config.net.peers.as_ref(); 103 info!("initializing on socket {:?}", socket); 104 info!("initializing with peers {:?}", peers.unwrap()); 105 self.router = Some(Router::new().route("/", get(""))); 111 fn db_init(&self) -> Result<rocksdb::DB, db::Error> { 112 let path = self.config.db_path.as_ref(); 113 let opts = &self.config.db_opts; 115 rocksdb::DB::open(&rocksdb::Options::default(), path.unwrap()).unwrap(); 116 Ok(rocksdb::DB::open_default("").unwrap()) 118 fn db_init_mut(&mut self) -> Result<(), db::Error> { 119 self.db = Some(self.db_init().unwrap()); 122 fn db_open(&self) -> Result<(), db::Error> { 125 fn db_close(&self) -> Result<(), db::Error> { 126 if let Some(db) = &self.db { 127 db.cancel_all_background_work(true) 131 fn db_close_mut(&mut self) -> Result<(), db::Error> { 132 self.db_close().unwrap(); 136 fn db_query(&self) -> Result<(), db::Error> { 139 fn db_transaction(&self) -> Result<(), db::Error> { 145 pub async fn proxy_via_reqwest(State(client): State<Client>) -> Response { 146 let reqwest_response = 147 match client.get("https://compiler.company").send().await { 150 log::error!("{} {}", &err, "request failed"); 151 return (StatusCode::BAD_REQUEST, Body::empty()).into_response(); 155 let response_builder = 156 Response::builder().status(reqwest_response.status().as_u16()); 158 // different http crate versions? 159 let mut headers = HeaderMap::with_capacity(reqwest_response.headers().len()); 160 headers.extend(reqwest_response.headers().into_iter().map( 162 let name = HeaderName::from_bytes(name.as_ref()).unwrap(); 163 let value = HeaderValue::from_bytes(value.as_ref()).unwrap(); 169 .body(Body::from_stream(reqwest_response.bytes_stream())) 170 // body is empty, no error 174 pub async fn start_http_proxy(addr: &str) { 175 let client = Client::new(); 176 let app = Router::new() 177 .route("/", get(proxy_via_reqwest)) 178 .layer(TraceLayer::new_for_http().on_body_chunk( 179 |chunk: &Bytes, _latency: Duration, _span: &Span| { 180 log::debug!("streaming {} bytes", chunk.len()); 184 let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); 186 "http_proxy running on: http://{}", 187 listener.local_addr().unwrap() 189 net::axum::serve(listener, app).await.unwrap(); 192 // pub async fn stream_some_data() -> Body { 193 // let stream = net::stream::iter(0..5) 194 // .throttle(Duration::from_secs(1)) 195 // .map(|n| n.to_string()) 196 // .map(Ok::<_, Infallible>); 197 // Body::from_stream(stream)