use acton_service::prelude::*;
#[derive(Debug, Serialize, Deserialize)]
struct UserV1 {
id: u64,
username: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct UserV2 {
id: u64,
username: String,
email: String, }
#[derive(Debug, Serialize, Deserialize)]
struct UserV3 {
id: String, username: String,
email: String,
created_at: String, }
async fn list_users_v1() -> Json<Vec<UserV1>> {
Json(vec![UserV1 {
id: 1,
username: "alice".to_string(),
}])
}
async fn get_user_v1(Path(id): Path<u64>) -> Json<UserV1> {
Json(UserV1 {
id,
username: format!("user{}", id),
})
}
async fn list_users_v2() -> Json<Vec<UserV2>> {
Json(vec![UserV2 {
id: 1,
username: "alice".to_string(),
email: "alice@example.com".to_string(),
}])
}
async fn get_user_v2(Path(id): Path<u64>) -> Json<UserV2> {
Json(UserV2 {
id,
username: format!("user{}", id),
email: format!("user{}@example.com", id),
})
}
async fn list_users_v3() -> Json<Vec<UserV3>> {
Json(vec![UserV3 {
id: "550e8400-e29b-41d4-a716-446655440000".to_string(),
username: "alice".to_string(),
email: "alice@example.com".to_string(),
created_at: "2025-01-01T00:00:00Z".to_string(),
}])
}
async fn get_user_v3(Path(id): Path<String>) -> Json<UserV3> {
Json(UserV3 {
id: id.clone(),
username: "alice".to_string(),
email: "alice@example.com".to_string(),
created_at: "2025-01-01T00:00:00Z".to_string(),
})
}
#[tokio::main]
async fn main() -> Result<()> {
let routes = VersionedApiBuilder::new()
.with_base_path("/api")
.add_version_deprecated(
ApiVersion::V1,
|routes| {
routes
.route("/users", get(list_users_v1))
.route("/users/{id}", get(get_user_v1))
},
DeprecationInfo::new(ApiVersion::V1, ApiVersion::V3)
.with_sunset_date("2025-12-31T23:59:59Z")
.with_message("V1 is deprecated. Please migrate to V3 for UUID support."),
)
.add_version_deprecated(
ApiVersion::V2,
|routes| {
routes
.route("/users", get(list_users_v2))
.route("/users/{id}", get(get_user_v2))
},
DeprecationInfo::new(ApiVersion::V2, ApiVersion::V3)
.with_sunset_date("2026-06-30T23:59:59Z")
.with_message("V2 is deprecated. Migrate to V3 for improved ID handling."),
)
.add_version(ApiVersion::V3, |routes| {
routes
.route("/users", get(list_users_v3))
.route("/users/{id}", get(get_user_v3))
})
.build_routes();
ServiceBuilder::new()
.with_routes(routes) .build() .serve()
.await?;
Ok(())
}