dynamic environment

This commit is contained in:
Asger Juul Brunshøj 2023-06-13 23:08:35 +02:00
parent c7d1674055
commit d6650ffb30
5 changed files with 31 additions and 26 deletions

6
Trunk.toml Normal file
View File

@ -0,0 +1,6 @@
[[proxy]]
backend = "http://localhost:4000/api/v1"
[[proxy]]
backend = "ws://localhost:4000/api/ws"
ws = true

View File

@ -126,12 +126,12 @@ async fn main() {
let app_state = Arc::clone(&app_state); let app_state = Arc::clone(&app_state);
tokio::spawn(async move { tokio::spawn(async move {
let app = Router::new() let app = Router::new()
.route("/create", post(create_achievement)) .route("/api/v1/create", post(create_achievement))
.route("/delete", post(delete_achievement)) .route("/api/v1/delete", post(delete_achievement))
.route("/toggle", post(toggle_achievement)) .route("/api/v1/toggle", post(toggle_achievement))
.route("/create-milestone", post(create_milestone)) .route("/api/v1/create-milestone", post(create_milestone))
.route("/delete-milestone", post(delete_milestone)) .route("/api/v1/delete-milestone", post(delete_milestone))
.route("/ws", get(ws_handler)) .route("/api/ws", get(ws_handler))
.layer( .layer(
ServiceBuilder::new() ServiceBuilder::new()
.layer(Extension(app_state)) .layer(Extension(app_state))

View File

@ -26,19 +26,11 @@ pub enum RestServiceError {
} }
impl RestService { impl RestService {
fn hostname() -> String {
web_sys::window().unwrap().location().hostname().unwrap()
}
fn url_to(endpoint: &str) -> String {
format!("http://{}:4000{endpoint}", Self::hostname())
}
async fn post_json<P: Serialize, T: DeserializeOwned>( async fn post_json<P: Serialize, T: DeserializeOwned>(
payload: P, payload: P,
path: &str, url: &str,
) -> Result<T, RestServiceError> { ) -> Result<T, RestServiceError> {
let req = Request::post(&Self::url_to(path)) let req = Request::post(url)
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.body(serde_json::to_string(&payload)?); .body(serde_json::to_string(&payload)?);
@ -57,22 +49,22 @@ impl RestService {
} }
pub async fn toggle_achievement(payload: ToggleAchievement) -> Result<(), RestServiceError> { pub async fn toggle_achievement(payload: ToggleAchievement) -> Result<(), RestServiceError> {
Self::post_json(payload, "/toggle").await Self::post_json(payload, "/api/v1/toggle").await
} }
pub async fn create_achievement(payload: CreateAchievement) -> Result<(), RestServiceError> { pub async fn create_achievement(payload: CreateAchievement) -> Result<(), RestServiceError> {
Self::post_json(payload, "/create").await Self::post_json(payload, "/api/v1/create").await
} }
pub async fn delete_achievement(payload: DeleteAchievement) -> Result<(), RestServiceError> { pub async fn delete_achievement(payload: DeleteAchievement) -> Result<(), RestServiceError> {
Self::post_json(payload, "/delete").await Self::post_json(payload, "/api/v1/delete").await
} }
pub async fn create_milestone(payload: CreateMilestone) -> Result<(), RestServiceError> { pub async fn create_milestone(payload: CreateMilestone) -> Result<(), RestServiceError> {
Self::post_json(payload, "/create-milestone").await Self::post_json(payload, "/api/v1/create-milestone").await
} }
pub async fn delete_milestone(payload: DeleteMilestone) -> Result<(), RestServiceError> { pub async fn delete_milestone(payload: DeleteMilestone) -> Result<(), RestServiceError> {
Self::post_json(payload, "/delete-milestone").await Self::post_json(payload, "/api/v1/delete-milestone").await
} }
} }

View File

@ -17,15 +17,22 @@ impl WebsocketService {
pub fn connect() -> Self { pub fn connect() -> Self {
let window = web_sys::window().expect("no global `window` exists"); let window = web_sys::window().expect("no global `window` exists");
let location = window.location(); let location = window.location();
let protocol = location.protocol().expect("should have a protocol");
let hostname = location.hostname().expect("should have a hostname"); let hostname = location.hostname().expect("should have a hostname");
let protocol = match location.protocol().expect("should have a protocol").as_str() {
let ws_protocol = match protocol.as_str() {
"https:" => "wss", "https:" => "wss",
_ => "ws", _ => "ws",
}; };
let ws_addr = format!("{protocol}://{hostname}:4000/ws"); let port = match location.port() {
let ws = WebSocket::open(&ws_addr).unwrap(); Ok(port) if !port.is_empty() => format!(":{}", port),
log::info!("Opened websocket connection to {ws_addr}"); _ => String::new(),
};
let ws_url = format!("{}://{}{}/api/ws", ws_protocol, hostname, port);
let ws = WebSocket::open(&ws_url).unwrap();
log::info!("Opened websocket connection to {ws_url}");
let (_write, mut read) = ws.split(); let (_write, mut read) = ws.split();

View File

@ -26,7 +26,7 @@ create-gul-bus:
# ws://<host>/ws # ws://<host>/ws
subscribe-ws: subscribe-ws:
websocat ws://127.0.0.1:4000/ws websocat ws://127.0.0.1:4000/api/ws
trunk-serve: trunk-serve:
trunk serve --address=0.0.0.0 crates/frontend/index.html trunk serve --address=0.0.0.0 crates/frontend/index.html