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);
tokio::spawn(async move {
let app = Router::new()
.route("/create", post(create_achievement))
.route("/delete", post(delete_achievement))
.route("/toggle", post(toggle_achievement))
.route("/create-milestone", post(create_milestone))
.route("/delete-milestone", post(delete_milestone))
.route("/ws", get(ws_handler))
.route("/api/v1/create", post(create_achievement))
.route("/api/v1/delete", post(delete_achievement))
.route("/api/v1/toggle", post(toggle_achievement))
.route("/api/v1/create-milestone", post(create_milestone))
.route("/api/v1/delete-milestone", post(delete_milestone))
.route("/api/ws", get(ws_handler))
.layer(
ServiceBuilder::new()
.layer(Extension(app_state))

View File

@ -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
}
}

View File

@ -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();

View File

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