api skeleton and docs
This commit is contained in:
parent
3100948f3a
commit
aff10840fd
2 changed files with 124 additions and 43 deletions
|
@ -5,16 +5,35 @@ use serde::{Deserialize, Serialize};
|
|||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct IncomingHeaders {
|
||||
host: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Factions {
|
||||
tr: u32,
|
||||
nc: u32,
|
||||
vs: u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct WorldPopulation {
|
||||
world_id: u32,
|
||||
total: u32,
|
||||
factions: Factions,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct MultipleWorldPopulation {
|
||||
worlds: Vec<WorldPopulation>,
|
||||
}
|
||||
|
||||
#[handler]
|
||||
async fn info(req: &mut Request, res: &mut Response) {
|
||||
let headers: IncomingHeaders = req.parse_headers().unwrap();
|
||||
let json = json!({
|
||||
"@": "Saerro Listening Post",
|
||||
"@": "Saerro Listening Post - PlanetSide 2 Live Population API",
|
||||
"@GitHub": "https://github.com/genudine/saerro",
|
||||
"@Disclaimer": "Genudine Dynamics is not responsible for any damages caused by this software. Use at your own risk.",
|
||||
"@Support": "#api-dev in https://discord.com/servers/planetside-2-community-251073753759481856",
|
||||
|
@ -34,15 +53,60 @@ async fn info(req: &mut Request, res: &mut Response) {
|
|||
res.render(serde_json::to_string_pretty(&json).unwrap());
|
||||
}
|
||||
|
||||
#[handler]
|
||||
async fn get_world(req: &mut Request, res: &mut Response) {
|
||||
let world_id: String = req.param("worldID").unwrap();
|
||||
let response = get_world_pop(world_id).await;
|
||||
|
||||
res.render(Json(response));
|
||||
}
|
||||
|
||||
#[handler]
|
||||
async fn get_world_multi(req: &mut Request, res: &mut Response) {
|
||||
let world_ids_raw = req.query::<String>("ids").unwrap();
|
||||
let world_ids: Vec<&str> = world_ids_raw.split(",").collect();
|
||||
|
||||
let mut response = MultipleWorldPopulation { worlds: Vec::new() };
|
||||
|
||||
for world_id in world_ids {
|
||||
response
|
||||
.worlds
|
||||
.push(get_world_pop(world_id.to_string()).await);
|
||||
}
|
||||
|
||||
res.render(Json(response));
|
||||
}
|
||||
|
||||
async fn get_world_pop(world_id: String) -> WorldPopulation {
|
||||
let response = WorldPopulation {
|
||||
world_id: world_id.parse().unwrap(),
|
||||
total: 0,
|
||||
factions: Factions {
|
||||
tr: 0,
|
||||
nc: 0,
|
||||
vs: 0,
|
||||
},
|
||||
};
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let port = ::std::env::var("PORT").unwrap_or("7878".to_string());
|
||||
let addr = format!("127.0.0.1:{}", port);
|
||||
|
||||
let cors_handler = Cors::builder()
|
||||
.allow_any_origin()
|
||||
.allow_method("GET")
|
||||
.build();
|
||||
|
||||
let router = Router::new().hoop(cors_handler).get(info);
|
||||
Server::new(TcpListener::bind("127.0.0.1:7878"))
|
||||
.serve(router)
|
||||
.await;
|
||||
println!("Listening on http://localhost:{}", port);
|
||||
|
||||
let router = Router::new()
|
||||
.hoop(cors_handler)
|
||||
.push(Router::with_path("/").get(info))
|
||||
.push(Router::with_path("/w/<worldID>").get(get_world))
|
||||
.push(Router::with_path("/m/").get(get_world_multi));
|
||||
Server::new(TcpListener::bind(&addr)).serve(router).await;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue