Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

feat: pokedex showcase #70

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions showcases/rocket/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions showcases/rocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ uuid = { version = "1.8", features = ["serde"], default-features = false }
jsonwebtoken = { version = "9.3", default-features = false }

reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-pki-types"] }
askama = {version = "0.12.1" }


[profile.dev.build-override]
Expand Down
2 changes: 1 addition & 1 deletion showcases/rocket/api_tests/hurl.env.test
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
target=http://localhost:8001
target=http://localhost:8002
token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjEyMDkxMDkyOTAifQ.eyJhdWQiOlsibG9jYWwubXlfZG9tYWluLmNvbSJdLCJhenAiOiJkMTlYcDhEWEkxQVE4ZUNjeDdweHM5bkZiUTRUMHUwdCIsImVtYWlsIjoiam9obi5kb2VAb3B0cmF2aXMuY29tIiwiZXhwIjozMjUwMzY3NjQwMCwiaWF0IjowLCJpc3MiOiJodHRwczovL2F1dGgubXlfZG9tYWluLmNvbS8iLCJteV9vdGhlcl9zcGVjaWFsX2NsYWltIjoiaGVsbG8gd29ybGQiLCJuYW1lIjoiSm9obiIsInN1YiI6IjcwZTQ5M2Q5LTg0ZDAtNDliMS05YjYzLWJkNTVmOTVhOTI3ZSJ9.NQQFnHJlL7xiKP-6tk3gYrKpYPW8eVga_u5x6t3-l8U
2 changes: 2 additions & 0 deletions showcases/rocket/api_tests/implemented/create_pokemons.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POST {{target}}/api/pokemons
HTTP 200
6 changes: 6 additions & 0 deletions showcases/rocket/api_tests/implemented/pokemon_html.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# try to get pokemons
GET {{target}}
HTTP 200

[Asserts]
xpath "string(/html/head/title)" contains "Pokédex"
4 changes: 2 additions & 2 deletions showcases/rocket/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ services:
service:
image: hurl-rocket:latest
environment:
- 'ROCKET_PORT=8001'
- 'ROCKET_PORT=8002'
- 'AUTH_HS256_SECRET=CTRKew35ltwdWhGv9WF10lJ06oYBZKzACYhANx7QXPZpvBvCNZbq161xHg2rKhcp'
- 'AUTH_JWT_AUD=local.my_domain.com'
ports:
- '8001:8001'
- '8002:8002'
9 changes: 7 additions & 2 deletions showcases/rocket/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ set dotenv-load
image_name := "hurl-rocket:latest"
hurl_opts := "--variables-file api_tests/hurl.env.test --test"


export ROCKET_PORT := "8002"
export AUTH_HS256_SECRET := "CTRKew35ltwdWhGv9WF10lJ06oYBZKzACYhANx7QXPZpvBvCNZbq161xHg2rKhcp"
export AUTH_JWT_AUD := "local.my_domain.com"

@_list:
just --list --unsorted

# Perform all verifications (compile, test, lint, etc.)
verify: test htests lint
verify: test api_tests lint
docker compose down

# Run the service locally (from sources)
run:
cargo run

htests: build (up "-d") api-test
api_tests: build (up "-d") api-test
docker compose down

up c="": build
Expand Down
12 changes: 11 additions & 1 deletion showcases/rocket/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
extern crate rocket;

mod auth;
mod poke_endpoints;
mod pokedex;

#[get("/healthz")]
fn healthz() {}
Expand All @@ -13,7 +15,15 @@ fn protected(_access_token: auth::AccessToken) {}

#[launch]
fn rocket() -> _ {
let pokedex = pokedex::Pokedex::new();
pokedex.fake_data();

rocket::build()
.attach(auth::fairing())
.mount("/api", routes![healthz, protected])
.manage(pokedex)
.mount(
"/api",
routes![healthz, protected, poke_endpoints::add_pokemons],
)
.mount("/", routes![poke_endpoints::get_all_pokemons])
}
31 changes: 31 additions & 0 deletions showcases/rocket/src/poke_endpoints/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::pokedex::{
pokemon::{DPokemon, PokeType, Pokemon},
Pokedex,
};
use askama::Template;
use rocket::http::ContentType;
use rocket::State;

#[derive(Template)]
#[template(path = "pokemons.html")]
pub struct PokemonTemplate {
pokemons: Vec<DPokemon>,
}

#[get("/")]
pub fn get_all_pokemons(dex: &State<Pokedex>) -> (ContentType, String) {
let pokemons: Vec<DPokemon> = dex.inner().get_all().into_iter().map(Into::into).collect();
let html = PokemonTemplate { pokemons }.render().unwrap();
(ContentType::HTML, html)
}

#[post("/pokemons")]
pub fn add_pokemons(dex: &State<Pokedex>) {
let special2 = Pokemon::new(
"9999991".to_string(),
"Special other Pokemon".to_string(),
vec![PokeType::Poison()],
Box::new(Some(dex.inner().get_all().first().unwrap().clone())),
);
dex.inner().add(special2);
}
47 changes: 47 additions & 0 deletions showcases/rocket/src/pokedex/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};

use pokemon::{PokeType, Pokemon};

pub mod pokemon;

pub struct Pokedex {
pokemons: Arc<Mutex<HashMap<String, Pokemon>>>,
}

impl Pokedex {
pub fn new() -> Self {
Self {
pokemons: Arc::new(Mutex::new(HashMap::new())),
}
}

pub fn fake_data(&self) {
let mut pokemons = self.pokemons.lock().unwrap();
let special1 = Pokemon::new(
"99999999".to_string(),
"Special Pokemon".to_string(),
vec![PokeType::Plant()],
Box::new(None),
);
let special2 = Pokemon::new(
"999999991".to_string(),
"Special Pokemon".to_string(),
vec![PokeType::Plant()],
Box::new(None),
);
pokemons.insert("99999999".to_string(), special1);
pokemons.insert("999999991".to_string(), special2);
}

pub fn add(&self, pokemon: Pokemon) {
let mut pokemons = self.pokemons.lock().unwrap();
pokemons.insert(pokemon.id(), pokemon);
}

pub fn get_all(&self) -> Vec<Pokemon> {
self.pokemons.lock().unwrap().values().cloned().collect()
}
}
Loading