dynamic environment
This commit is contained in:
@@ -26,19 +26,11 @@ pub enum RestServiceError {
|
||||
}
|
||||
|
||||
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>(
|
||||
payload: P,
|
||||
path: &str,
|
||||
url: &str,
|
||||
) -> Result<T, RestServiceError> {
|
||||
let req = Request::post(&Self::url_to(path))
|
||||
let req = Request::post(url)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(serde_json::to_string(&payload)?);
|
||||
|
||||
@@ -57,22 +49,22 @@ impl RestService {
|
||||
}
|
||||
|
||||
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> {
|
||||
Self::post_json(payload, "/create").await
|
||||
Self::post_json(payload, "/api/v1/create").await
|
||||
}
|
||||
|
||||
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> {
|
||||
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> {
|
||||
Self::post_json(payload, "/delete-milestone").await
|
||||
Self::post_json(payload, "/api/v1/delete-milestone").await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,15 +17,22 @@ impl WebsocketService {
|
||||
pub fn connect() -> Self {
|
||||
let window = web_sys::window().expect("no global `window` exists");
|
||||
let location = window.location();
|
||||
let protocol = location.protocol().expect("should have a protocol");
|
||||
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",
|
||||
_ => "ws",
|
||||
};
|
||||
|
||||
let ws_addr = format!("{protocol}://{hostname}:4000/ws");
|
||||
let ws = WebSocket::open(&ws_addr).unwrap();
|
||||
log::info!("Opened websocket connection to {ws_addr}");
|
||||
let port = match location.port() {
|
||||
Ok(port) if !port.is_empty() => format!(":{}", port),
|
||||
_ => 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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user