Poem-openapi 1.0 released!

Poem-openapi is an OpenAPI server-side framework based on Poem. If you have used FastAPI, they are very similar in use.

The 1.0 version simplifies some macro attributes, making it more convenient to use.

How to use it

Poem-openapi book

Quick start

use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{param::Query, payload::PlainText, OpenApi, OpenApiService};

struct Api;

#[OpenApi]
impl Api {
  #[oai(path = "/hello", method = "get")]
  async fn index(
    &self,
    #[oai(validator(max_length = 32))] name: Query<Option<String>>,
  ) -> PlainText<String> {
    match name.0 {
      Some(name) => PlainText(format!("hello, {}!", name)),
      None => PlainText("hello!".to_string()),
    }
  }
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
  let api_service = OpenApiService::new(Api, "Hello World", "1.0")
      .server("http://localhost:3000/api");
  let ui = api_service.swagger_ui();

  Server::new(TcpListener::bind("127.0.0.1:3000"))
    .run(Route::new().nest("/api", api_service).nest("/", ui))
    .await
}

More examples

https://github.com/poem-web/poem/tree/master/examples/openapi

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.