Skip to content

Instantly share code, notes, and snippets.

@dgr113
Last active February 13, 2021 11:25
Show Gist options
  • Save dgr113/8f441d40ce36d78d62bb6e9498bce077 to your computer and use it in GitHub Desktop.
Save dgr113/8f441d40ce36d78d62bb6e9498bce077 to your computer and use it in GitHub Desktop.
Actix-web v3 use with app factory and paperclip openapi doc ( in progress... )
[package]
name = "testapp"
version = "0.0.1"
authors = ["Author <[email protected]>"]
edition = "2018"
[dependencies]
futures = "0.3.12"
serde_json = "1.0.62"
serde = { version = "1.0.123", features = ["derive"] }
paperclip = { git = "https://github.com/dgr113/paperclip.git", features = ["actix"] }
actix-web = { version = "3.3.2", features = ["rustls"] }
actix-service = "1.0"
[profile.release]
lto = true
use paperclip::v2::models::DefaultApiRaw;
use paperclip::actix::{ OpenApiExt, web };
use actix_service::ServiceFactory;
use actix_web::{
App, Error, body::Body,
dev::{ ServiceResponse, ServiceRequest },
};
use crate::routes;
#[derive(Clone)]
pub struct AppBindData ( Vec<String> );
pub struct AppFactory;
impl AppFactory {
pub(crate) fn build_app()
-> (
App<
impl ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>,
Body
>,
DefaultApiRaw
)
{
App::new()
.wrap_api()
.route("/", web::get().to( routes::index ))
.build_with_spec() /// At the moment, I'm using my own fork for paperclip ("https://github.com/dgr113/paperclip.git")
}
}
#[macro_use] extern crate serde_json;
use std::env;
use serde_json::json;
use actix_web::HttpServer;
use crate::core::errors::ApiError;
use crate::factory::{ AppFactory, AppBindData };
#[derive(Clone)]
pub struct AppBindData ( Vec<String> );
#[actix_web::main]
async fn bind_app(bind_data: AppBindData) -> Result<(), ApiError> {
HttpServer::new(move || {
let bind_data = bind_data.clone();
let (app, spec) = AppFactory::build_app();
app
.data( spec )
.data( bind_data.0 )
})
.bind("0.0.0.0:8888") ?
.run()
.await ?;
Ok(())
}
fn main() -> Result<(), ApiError> {
let test_share_data = vec!["TEST", "SHARE", "DATA"];
let app_data = AppBindData( test_share_data );
bind_app( app_data ).expect("Error with App binding!");
Ok(())
}
use actix_web::web;
use paperclip::actix::{ api_v2_operation, web::Json };
use crate::core::errors::ApiError;
#[api_v2_operation(tags("Api reference"), description = "Index page")]
pub(crate) async fn index(data: web::Data<DefaultApiRaw>) -> Result<Json<Vec<String>>, ApiError> {
Ok( Json( &data ) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment