boxy/examples/example-server/src/main.rs

58 lines
1.4 KiB
Rust

use std::{collections::HashMap, fmt::format};
use rocket::{build, get, launch, routes};
use serde_json::json;
extern crate rocket;
const BOXY_ADDRESS: &str = "localhost";
const BOXY_PORT: u16 = 8006;
const CLIENT_NAME: &str = "eu-central-1";
const CLIENT_SECRET: &str = "password123";
#[get("/")]
fn index() -> &'static str {
"Hello world!"
}
#[launch]
fn rocket() -> _ {
// This is the example backend client. (The stereo.cat backend for example).
let client = reqwest::blocking::Client::new();
// We define the port of the server running locally and the hostname we want to route to it.
let body = json!({
"port": 8000,
"hosts": ["localhost:8005"],
});
// Send it to Boxy's API
let res = client
.post(format!("http://{}:{}/register", BOXY_ADDRESS, BOXY_PORT))
.basic_auth(CLIENT_NAME, Some(CLIENT_SECRET))
.json(&body)
.send()
.unwrap();
let id = res.text().unwrap();
println!("{}", id);
let body2 = json!({});
// Send it to Boxy's API
let res2 = client
.delete(format!(
"http://{}:{}/endpoint/{}",
BOXY_ADDRESS, BOXY_PORT, id
))
.basic_auth(CLIENT_NAME, Some(CLIENT_SECRET))
.json(&body2)
.send()
.unwrap();
println!("{}", res2.text().unwrap());
build().mount("/", routes![index])
}