2 pub extern crate diesel;
3 pub extern crate dotenv;
4 pub extern crate chrono;
5 pub extern crate serde;
6 pub extern crate serde_json;
7 pub extern crate actix;
8 pub extern crate actix_web;
10 pub extern crate strum;
11 pub extern crate jsonwebtoken;
12 pub extern crate bcrypt;
13 pub extern crate regex;
14 #[macro_use] pub extern crate strum_macros;
19 pub mod websocket_server;
22 use diesel::pg::PgConnection;
23 use diesel::result::Error;
29 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
30 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> where Self: Sized;
31 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> where Self: Sized;
32 fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> where Self: Sized;
35 pub trait Followable<T> {
36 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
37 fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
40 pub trait Joinable<T> {
41 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
42 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
45 pub trait Likeable<T> {
46 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
47 fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
50 pub fn establish_connection() -> PgConnection {
51 let db_url = Settings::get().db_url;
52 PgConnection::establish(&db_url)
53 .expect(&format!("Error connecting to {}", db_url))
65 db_url: env::var("DATABASE_URL")
66 .expect("DATABASE_URL must be set"),
67 hostname: env::var("HOSTNAME").unwrap_or("http://0.0.0.0".to_string())
70 fn api_endpoint(&self) -> String {
71 format!("{}/api/v1", self.hostname)
75 use chrono::{DateTime, NaiveDateTime, Utc};
76 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
77 DateTime::<Utc>::from_utc(ndt, Utc)
80 pub fn naive_now() -> NaiveDateTime {
81 chrono::prelude::Utc::now().naive_utc()
84 pub fn is_email_regex(test: &str) -> bool {
85 let re = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
91 use {Settings, is_email_regex};
94 assert_eq!(Settings::get().api_endpoint(), "http://0.0.0.0/api/v1");
99 assert!(is_email_regex("gush@gmail.com"));
100 assert!(!is_email_regex("nada_neutho"));