GithubHelp home page GithubHelp logo

deftype's Introduction

new.deftype.com

deftype's People

Contributors

itang avatar

Watchers

 avatar  avatar  avatar

deftype's Issues

[server-rust]ResultDTO 数据类型

  • 类型定义
#[derive(Serialize, Debug)]
pub struct ResultDTO<T: ser::Serialize> {
    pub ok: bool,
    pub code: i32,
    pub message: String,
    pub data: T,
}

impl<T: ser::Serialize> ResultDTO<T> {
    pub fn ok(data: T) -> Self {
        ResultDTO {
            ok: true,
            code: 0,
            data: data,
            message: "".to_owned(),
        }
    }

    pub fn err(data: T) -> Self {
        ResultDTO {
            ok: false,
            code: 1,
            data: data,
            message: "".to_owned(),
        }
    }

    #[allow(dead_code)]
    pub fn code(mut self, value: i32) -> Self {
        self.code = value;
        self
    }

    #[allow(dead_code)]
    pub fn message(mut self, message: &str) -> Self {
        self.message = message.to_owned();
        self
    }
}

ext:

impl<T: ser::Serialize> ResultDTO<T> {
    pub fn to_json_result(&self) -> IronResult<Response> {
        json(self)
    }
}

[server-rust]Db操作错误类型

  • 定义 DbError

    UpdateError ,QueryError, SaveError, Delete Error ?

  • 修改 models api返回为类型为 Result<T, DBError>

  • DBError 与IronError适配

[server-rust]User Manager

  1. bcrypt加密强度降低 2016-02-27

    static MIN_COST: u32 = 4;
    static MAX_COST: u32 = 31;
    pub static DEFAULT_COST: u32 = 12;

    cost >= MIN_CONST && cost <= MAX_COST

    目前使用cost = 4

  2. change password

[server-rust]Json body-parser

let parsed = req.get::<bodyparser::Struct<models::NewUser>>();
let parsed = try!(parsed.map_err(|err| CustomBodyError { cause: err }));
match parsed {
    Some(ref new_user) => json(&models::create_user(&models::establish_connection(), new_user)),
    None => json(&"".to_owned()),
}

[server-rust]判断是否为Ajax请求

header! { (XMLHttpRequest, "X-Requested-With") => [String] }

#[allow(dead_code)]
pub fn is_ajax_request(req: &Request) -> bool {
    req.headers.has::<XMLHttpRequest>()
}

[server-rust]使上数据库连接池

impl:

use std::env;
use r2d2;
use r2d2_diesel::ConnectionManager;
use dotenv::dotenv;
use diesel::pg::PgConnection;

lazy_static! {
    pub static ref POOL: r2d2::Pool<ConnectionManager<PgConnection>> = make_pool();
}

pub fn conn_pool<'a>() -> &'a r2d2::Pool<ConnectionManager<PgConnection>> {
    &(*POOL)
}

fn make_pool() -> r2d2::Pool<ConnectionManager<PgConnection>> {
    dotenv().ok();

    let config = r2d2::Config::default();
    let manager = ConnectionManager::<PgConnection>::new(env::var("DATABASE_URL")
                                                             .expect("DATABASE_URL must be set"));

    r2d2::Pool::new(config, manager).expect("Failed to create pool.")
}

How to use:

let conn = try!(global::conn_pool().get().map_err(GetTimeoutWrapper));
json(&models::create_user(&conn, new_user))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.