forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.mustache
98 lines (75 loc) · 2.94 KB
/
README.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Rust API for {{{packageName}}}
{{#appDescriptionWithNewLines}}
{{{.}}}
{{/appDescriptionWithNewLines}}
## Overview
This server was generated by the [openapi-generator]
(https://openapi-generator.tech) project. By using the
[OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) from a remote
server, you can easily generate a server stub.
To see how to make this your own, look here: [README]((https://openapi-generator.tech))
- API version: {{{appVersion}}}
{{^hideGenerationTimestamp}}
- Build date: {{{generatedDate}}}
{{/hideGenerationTimestamp}}
- Generator version: {{generatorVersion}}
{{#infoUrl}}For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}){{/infoUrl}}
This autogenerated project defines an API crate `{{{packageName}}}` which contains:
* An `Api` trait defining the API in Rust.
* Data types representing the underlying data model.
* Axum router which accepts HTTP requests and invokes the appropriate `Api` method for each operation.
* Request validations (path, query, body params) are included.
## Using the generated library
The generated library has a few optional features that can be activated through Cargo.
* `server`
* This defaults to enabled and creates the basic skeleton of a server implementation based on Axum.
* To create the server stack you'll need to provide an implementation of the API trait to provide the server function.
* `conversions`
* This defaults to disabled and creates extra derives on models to allow "transmogrification" between objects of structurally similar types.
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section for how to use features in your `Cargo.toml`.
### Example
```rust
struct ServerImpl {
// database: sea_orm::DbConn,
}
#[allow(unused_variables)]
#[async_trait]
impl {{{externCrateName}}}::apis::default::Api for ServerImpl {
// API implementation goes here
}
impl {{{externCrateName}}}::apis::ErrorHandler for ServerImpl {}
pub async fn start_server(addr: &str) {
// initialize tracing
tracing_subscriber::fmt::init();
// Init Axum router
let app = {{{externCrateName}}}::server::new(Arc::new(ServerImpl));
// Add layers to the router
let app = app.layer(...);
// Run the server with graceful shutdown
let listener = TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
}
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}
```