mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-20 14:57:01 +00:00
add proc macro derive for openapitype
This commit is contained in:
parent
a4185a5665
commit
4ef216e8c8
17 changed files with 273 additions and 47 deletions
28
example/Cargo.toml
Normal file
28
example/Cargo.toml
Normal file
|
@ -0,0 +1,28 @@
|
|||
# -*- eval: (cargo-minor-mode 1) -*-
|
||||
|
||||
[package]
|
||||
name = "example"
|
||||
version = "0.0.1"
|
||||
authors = ["Dominic Meiser <git@msrd0.de>"]
|
||||
edition = "2018"
|
||||
license = "Unlicense"
|
||||
readme = "README.md"
|
||||
include = ["src/**/*", "Cargo.toml", "LICENSE"]
|
||||
repository = "https://gitlab.com/msrd0/gotham-restful"
|
||||
|
||||
[badges]
|
||||
gitlab = { repository = "msrd0/gotham-restful", branch = "master" }
|
||||
|
||||
[dependencies]
|
||||
fake = "2.2"
|
||||
gotham = "0.4"
|
||||
gotham_restful = { path = "../gotham_restful", features = ["openapi"] }
|
||||
gotham_restful_derive = { path = "../gotham_restful_derive", features = ["openapi"] }
|
||||
log = "0.4"
|
||||
log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
|
||||
serde = "1"
|
||||
|
||||
[dev-dependencies]
|
||||
fake = "2.2"
|
||||
log = "0.4"
|
||||
log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
|
24
example/LICENSE
Normal file
24
example/LICENSE
Normal file
|
@ -0,0 +1,24 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org>
|
131
example/src/main.rs
Normal file
131
example/src/main.rs
Normal file
|
@ -0,0 +1,131 @@
|
|||
#[macro_use] extern crate log;
|
||||
#[macro_use] extern crate gotham_restful_derive;
|
||||
|
||||
use fake::{faker::internet::en::Username, Fake};
|
||||
use gotham::{
|
||||
middleware::logger::RequestLogger,
|
||||
pipeline::{new_pipeline, single::single_pipeline},
|
||||
router::builder::*,
|
||||
state::State
|
||||
};
|
||||
use gotham_restful::*;
|
||||
use log::LevelFilter;
|
||||
use log4rs::{
|
||||
append::console::ConsoleAppender,
|
||||
config::{Appender, Config, Root},
|
||||
encode::pattern::PatternEncoder
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
rest_resource!{Users, route => {
|
||||
route.read_all::<Self, _>();
|
||||
route.read::<Self, _, _>();
|
||||
route.create::<Self, _, _>();
|
||||
route.update_all::<Self, _, _>();
|
||||
route.update::<Self, _, _, _>();
|
||||
}}
|
||||
|
||||
#[derive(Deserialize, OpenapiType, Serialize)]
|
||||
struct User {
|
||||
username : String
|
||||
}
|
||||
|
||||
impl ResourceReadAll<Success<Vec<Option<User>>>> for Users
|
||||
{
|
||||
fn read_all(_state : &mut State) -> Success<Vec<Option<User>>>
|
||||
{
|
||||
vec![Username().fake(), Username().fake()]
|
||||
.into_iter()
|
||||
.map(|username| Some(User { username }))
|
||||
.collect::<Vec<Option<User>>>()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ResourceRead<u64, Success<User>> for Users
|
||||
{
|
||||
fn read(_state : &mut State, id : u64) -> Success<User>
|
||||
{
|
||||
let username : String = Username().fake();
|
||||
User { username: format!("{}{}", username, id) }.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ResourceCreate<User, Success<()>> for Users
|
||||
{
|
||||
fn create(_state : &mut State, body : User) -> Success<()>
|
||||
{
|
||||
info!("Created User: {}", body.username);
|
||||
().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ResourceUpdateAll<Vec<User>, Success<()>> for Users
|
||||
{
|
||||
fn update_all(_state : &mut State, body : Vec<User>) -> Success<()>
|
||||
{
|
||||
info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::<Vec<String>>());
|
||||
().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ResourceUpdate<u64, User, Success<()>> for Users
|
||||
{
|
||||
fn update(_state : &mut State, id : u64, body : User) -> Success<()>
|
||||
{
|
||||
info!("Change User {} to {}", id, body.username);
|
||||
().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ResourceDeleteAll<Success<()>> for Users
|
||||
{
|
||||
fn delete_all(_state : &mut State) -> Success<()>
|
||||
{
|
||||
info!("Delete all Users");
|
||||
().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ResourceDelete<u64, Success<()>> for Users
|
||||
{
|
||||
fn delete(_state : &mut State, id : u64) -> Success<()>
|
||||
{
|
||||
info!("Delete User {}", id);
|
||||
().into()
|
||||
}
|
||||
}
|
||||
|
||||
const ADDR : &str = "127.0.0.1:18080";
|
||||
|
||||
fn main()
|
||||
{
|
||||
let encoder = PatternEncoder::new("{d(%Y-%m-%d %H:%M:%S%.3f %Z)} [{l}] {M} - {m}\n");
|
||||
let config = Config::builder()
|
||||
.appender(
|
||||
Appender::builder()
|
||||
.build("stdout", Box::new(
|
||||
ConsoleAppender::builder()
|
||||
.encoder(Box::new(encoder))
|
||||
.build()
|
||||
)))
|
||||
.build(Root::builder().appender("stdout").build(LevelFilter::Info))
|
||||
.unwrap();
|
||||
log4rs::init_config(config).unwrap();
|
||||
|
||||
let logging = RequestLogger::new(log::Level::Info);
|
||||
let (chain, pipelines) = single_pipeline(
|
||||
new_pipeline()
|
||||
.add(logging)
|
||||
.build()
|
||||
);
|
||||
|
||||
gotham::start(ADDR, build_router(chain, pipelines, |route| {
|
||||
route.with_openapi("Users Example", "0.0.1", format!("http://{}", ADDR), |mut route| {
|
||||
route.resource::<Users, _>("users");
|
||||
route.get_openapi("openapi");
|
||||
});
|
||||
}));
|
||||
println!("Gotham started on {} for testing", ADDR);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue