// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ // ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX) ┃ // ┃ SPDX-License-Identifier: MPL-2.0 ┃ // ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ // ┃ ┃ // ┃ This Source Code Form is subject to the terms of the Mozilla Public ┃ // ┃ License, v. 2.0. If a copy of the MPL was not distributed with this ┃ // ┃ file, You can obtain one at https://mozilla.org/MPL/2.0/. ┃ // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ use lexa_framework::extract::Path; use lexa_framework::http::response::{Html, Json}; use lexa_framework::http::{HttpContext, HttpContextInterface}; use crate::view_render_site::views::HelloNameView; // Pour l'exemple ;-) static mut COUNTER_MEM: usize = 0; // --------- // // Structure // // --------- // pub struct PagesController; // -------------- // // Implémentation // -> Interface // -------------- // #[async_trait::async_trait] impl HttpContextInterface for PagesController { type State = (); async fn new( _: &lexa_framework::extract::Extensions, _: Self::State, ) -> Option { Some(Self) } } // -------------- // // Implémentation // -> Actions // -------------- // impl PagesController { /// Accès à la page `/html` en GET. pub async fn html(this: HttpContext) -> Html<&'static str> { let raw_html = "Hello "; this.response.html(raw_html) } /// Accès à la page `/html/{name}` en GET. pub async fn html_arg( this: HttpContext, Path(name): Path, ) -> Html { this.response.html(HelloNameView { name }) } } #[derive(serde::Serialize)] pub struct MyJsonResponse { counter: usize, } impl PagesController { /// Accès à la page `/json` en GET. pub async fn json(this: HttpContext) -> Json { let response = MyJsonResponse { counter: unsafe { COUNTER_MEM }, }; unsafe { COUNTER_MEM += 1 }; this.response.json(response) } }