Added a way to receive all maps

main
BurnyLlama 2023-04-15 21:31:39 +02:00
parent 34b35e0795
commit c277eb5ebd
5 changed files with 24 additions and 3 deletions

View File

@ -1,5 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml",
"./Cargo.toml",
"./Cargo.toml",
"./Cargo.toml"

3
Rocket.toml Normal file
View File

@ -0,0 +1,3 @@
[global]
address = "0.0.0.0"
port = 12345

15
src/api/maps.rs Normal file
View File

@ -0,0 +1,15 @@
use rocket::{serde::json::Json, Route, State};
use crate::database::{models::map::Map, DatabaseHandler};
#[get("/")]
async fn get_all_maps(db: &State<DatabaseHandler>) -> Result<Json<Vec<Map>>, String> {
match Map::get_all_maps(db).await {
Ok(maps) => Ok(Json(maps)),
Err(err) => Err(format!("Error: {}", err)),
}
}
pub fn get_all_routes() -> Vec<Route> {
routes![get_all_maps]
}

1
src/api/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod maps;

View File

@ -3,11 +3,12 @@ use database::DatabaseHandler;
#[macro_use]
extern crate rocket;
mod api;
mod database;
#[rocket::main]
async fn main() {
let client = match DatabaseHandler::create().await {
let db = match DatabaseHandler::create().await {
Ok(client) => client,
Err(err) => panic!(
"Encountered an error while connecting to the database!\n{}",
@ -16,8 +17,8 @@ async fn main() {
};
match rocket::build()
.manage(client)
.mount("/", routes![])
.manage(db)
.mount("/api/v1/maps", api::maps::get_all_routes())
.launch()
.await
{