pub struct App { /* private fields */ }Expand description
Builder principal do framework.
Acumula rotas, services de DI, configuração de OpenAPI e middleware
(interceptors). Use run_http para servir local, ou a
trait AppRuntime (do crate serverust-lambda) para o método .run()
que detecta automaticamente entre Lambda e HTTP local.
§Exemplo
use std::sync::Arc;
use serverust_core::App;
use serverust_macros::{get, injectable};
#[injectable]
struct Greeter;
impl Greeter {
fn hi(&self) -> String { "hello".into() }
}
#[get("/")]
async fn root(
axum::extract::State(g): axum::extract::State<Arc<Greeter>>,
) -> String {
g.hi()
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
App::new()
.openapi_info("My API", "0.1.0")
.provide::<Greeter>(Arc::new(Greeter))
.route(root)
.run_http("127.0.0.1:3000")
.await
}§Rotas de documentação
into_router injeta automaticamente três rotas:
/openapi.json (OpenAPI 3.1), /docs (Swagger UI) e /redoc (ReDoc).
Customize os paths via docs e redoc.
Implementations§
Source§impl App
impl App
Sourcepub fn openapi_info(
self,
title: impl Into<String>,
version: impl Into<String>,
) -> Self
pub fn openapi_info( self, title: impl Into<String>, version: impl Into<String>, ) -> Self
Customiza title e version do documento OpenAPI gerado.
Sourcepub fn register_schema<T: ToSchema + PartialSchema>(self) -> Self
pub fn register_schema<T: ToSchema + PartialSchema>(self) -> Self
Registra um schema T: ToSchema em components.schemas do OpenAPI.
Sourcepub fn docs(self, path: &'static str) -> Self
pub fn docs(self, path: &'static str) -> Self
Customiza o path em que o Swagger UI é servido (default /docs).
Sourcepub fn redoc(self, path: &'static str) -> Self
pub fn redoc(self, path: &'static str) -> Self
Customiza o path em que o ReDoc é servido (default /redoc).
Sourcepub fn provide<T: ?Sized + Send + Sync + 'static>(self, value: Arc<T>) -> Self
pub fn provide<T: ?Sized + Send + Sync + 'static>(self, value: Arc<T>) -> Self
Registra um service com lifetime Singleton no container.
T pode ser dyn Trait: app.provide::<dyn MyService>(Arc::new(impl)).
Handlers extraem o serviço via State<Arc<dyn MyService>>.
Sourcepub fn override<T: ?Sized + Send + Sync + 'static>(self, value: Arc<T>) -> Self
pub fn override<T: ?Sized + Send + Sync + 'static>(self, value: Arc<T>) -> Self
API de teste: substitui o provider de T por uma instância mock.
override é palavra reservada — chame como app.r#override::<...>(...).
Sourcepub fn interceptor<I: Interceptor>(self, interceptor: I) -> Self
pub fn interceptor<I: Interceptor>(self, interceptor: I) -> Self
Registra um interceptor (tower middleware) sobre as rotas do usuário.
Aplicado em Self::into_router apenas às rotas registradas via
Self::route — as rotas de documentação (/openapi.json, /docs,
/redoc) ficam de fora intencionalmente, para que não dependam da
pipeline de negócio (ex.: autenticação, rate limiting).
Sourcepub fn config(self, cfg: ServerustConfig) -> Self
pub fn config(self, cfg: ServerustConfig) -> Self
Injeta uma ServerustConfig tipada no container. Handlers podem extraí-la via
State<Arc<ServerustConfig>>.
Sourcepub fn route<R: IntoRoute>(self, handler: R) -> Self
pub fn route<R: IntoRoute>(self, handler: R) -> Self
Registra um handler anotado por #[get], #[post], etc.
Sourcepub fn into_router(self) -> Router
pub fn into_router(self) -> Router
Constrói o axum::Router final adicionando /openapi.json, /docs e /redoc.
Sourcepub async fn run_http<A: ToSocketAddrs>(self, addr: A) -> Result<()>
pub async fn run_http<A: ToSocketAddrs>(self, addr: A) -> Result<()>
Sobe um servidor HTTP local ligado em addr (ex.: "127.0.0.1:3000").
Imprime no stderr o endereço efetivo + URLs de documentação assim que o listener fica pronto, para o desenvolvedor saber onde conectar:
🦀 serverust on http://0.0.0.0:3000
docs: http://0.0.0.0:3000/docs
openapi: http://0.0.0.0:3000/openapi.jsonTrait Implementations§
Auto Trait Implementations§
impl Freeze for App
impl !RefUnwindSafe for App
impl Send for App
impl Sync for App
impl Unpin for App
impl UnsafeUnpin for App
impl !UnwindSafe for App
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);