1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 23:07:01 +00:00

update to gotham 0.5 and start using rustfmt

This commit is contained in:
Dominic 2020-09-15 15:10:41 +02:00
parent 5317e50961
commit d55b0897e9
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
39 changed files with 1798 additions and 2095 deletions

View file

@ -1,5 +1,7 @@
#[macro_use] extern crate gotham_derive;
#[macro_use] extern crate log;
#[macro_use]
extern crate gotham_derive;
#[macro_use]
extern crate log;
use fake::{faker::internet::en::Username, Fake};
use gotham::{
@ -20,25 +22,19 @@ use serde::{Deserialize, Serialize};
#[derive(Resource)]
#[resource(read_all, read, search, create, change_all, change, remove, remove_all)]
struct Users
{
}
struct Users {}
#[derive(Resource)]
#[resource(ReadAll)]
struct Auth
{
}
struct Auth {}
#[derive(Deserialize, OpenapiType, Serialize, StateData, StaticResponseExtender)]
struct User
{
username : String
struct User {
username: String
}
#[read_all(Users)]
fn read_all() -> Success<Vec<Option<User>>>
{
fn read_all() -> Success<Vec<Option<User>>> {
vec![Username().fake(), Username().fake()]
.into_iter()
.map(|username| Some(User { username }))
@ -47,113 +43,101 @@ fn read_all() -> Success<Vec<Option<User>>>
}
#[read(Users)]
fn read(id : u64) -> Success<User>
{
let username : String = Username().fake();
User { username: format!("{}{}", username, id) }.into()
fn read(id: u64) -> Success<User> {
let username: String = Username().fake();
User {
username: format!("{}{}", username, id)
}
.into()
}
#[search(Users)]
fn search(query : User) -> Success<User>
{
fn search(query: User) -> Success<User> {
query.into()
}
#[create(Users)]
fn create(body : User)
{
fn create(body: User) {
info!("Created User: {}", body.username);
}
#[change_all(Users)]
fn update_all(body : Vec<User>)
{
info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::<Vec<String>>());
fn update_all(body: Vec<User>) {
info!(
"Changing all Users to {:?}",
body.into_iter().map(|u| u.username).collect::<Vec<String>>()
);
}
#[change(Users)]
fn update(id : u64, body : User)
{
fn update(id: u64, body: User) {
info!("Change User {} to {}", id, body.username);
}
#[remove_all(Users)]
fn remove_all()
{
fn remove_all() {
info!("Delete all Users");
}
#[remove(Users)]
fn remove(id : u64)
{
fn remove(id: u64) {
info!("Delete User {}", id);
}
#[read_all(Auth)]
fn auth_read_all(auth : AuthStatus<()>) -> AuthSuccess<String>
{
fn auth_read_all(auth: AuthStatus<()>) -> AuthSuccess<String> {
match auth {
AuthStatus::Authenticated(data) => Ok(format!("{:?}", data)),
_ => Err(Forbidden)
}
}
const ADDR : &str = "127.0.0.1:18080";
const ADDR: &str = "127.0.0.1:18080";
#[derive(Clone, Default)]
struct Handler;
impl<T> AuthHandler<T> for Handler
{
fn jwt_secret<F : FnOnce() -> Option<T>>(&self, _state : &mut State, _decode_data : F) -> Option<Vec<u8>>
{
impl<T> AuthHandler<T> for Handler {
fn jwt_secret<F: FnOnce() -> Option<T>>(&self, _state: &mut State, _decode_data: F) -> Option<Vec<u8>> {
None
}
}
fn main()
{
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()
)))
.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 cors = CorsConfig {
origin: Origin::Copy,
headers: vec![CONTENT_TYPE],
credentials: true,
..Default::default()
};
let auth = <AuthMiddleware<(), Handler>>::from_source(AuthSource::AuthorizationHeader);
let logging = RequestLogger::new(log::Level::Info);
let (chain, pipelines) = single_pipeline(
new_pipeline()
.add(auth)
.add(logging)
.add(cors)
.build()
);
let (chain, pipelines) = single_pipeline(new_pipeline().add(auth).add(logging).add(cors).build());
gotham::start(ADDR, build_router(chain, pipelines, |route| {
let info = OpenapiInfo {
title: "Users Example".to_owned(),
version: "0.0.1".to_owned(),
urls: vec![format!("http://{}", ADDR)]
};
route.with_openapi(info, |mut route| {
route.resource::<Users>("users");
route.resource::<Auth>("auth");
route.get_openapi("openapi");
});
}));
gotham::start(
ADDR,
build_router(chain, pipelines, |route| {
let info = OpenapiInfo {
title: "Users Example".to_owned(),
version: "0.0.1".to_owned(),
urls: vec![format!("http://{}", ADDR)]
};
route.with_openapi(info, |mut route| {
route.resource::<Users>("users");
route.resource::<Auth>("auth");
route.get_openapi("openapi");
});
})
);
println!("Gotham started on {} for testing", ADDR);
}