]> Untitled Git - lemmy.git/blob - server/src/lib.rs
Adding login and Register
[lemmy.git] / server / src / lib.rs
1 #[macro_use]
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;
9 pub extern crate rand;
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;
15
16 pub mod schema;
17 pub mod apub;
18 pub mod actions;
19 pub mod websocket_server;
20
21 use diesel::*;
22 use diesel::pg::PgConnection;
23 use diesel::result::Error;
24 use dotenv::dotenv;
25 use std::env;
26 use regex::Regex;
27
28 pub trait Crud<T> {
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;
33 }
34
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;
38 }
39
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;
43 }
44
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;
48 }
49
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))
54 }
55
56 pub struct Settings {
57   db_url: String,
58   hostname: String
59 }
60
61 impl Settings {
62   fn get() -> Self {
63     dotenv().ok();
64     Settings {
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())
68     }
69   }
70   fn api_endpoint(&self) -> String {
71     format!("{}/api/v1", self.hostname)
72   }
73 }
74
75 use chrono::{DateTime, NaiveDateTime, Utc};
76 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
77   DateTime::<Utc>::from_utc(ndt, Utc)
78 }
79
80 pub fn naive_now() -> NaiveDateTime {
81   chrono::prelude::Utc::now().naive_utc()
82 }
83
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();
86   re.is_match(test)
87 }
88
89 #[cfg(test)]
90 mod tests {
91   use {Settings, is_email_regex};
92   #[test]
93   fn test_api() {
94     assert_eq!(Settings::get().api_endpoint(), "http://0.0.0.0/api/v1");
95   }
96
97   #[test]
98   fn test_email() {
99     assert!(is_email_regex("gush@gmail.com"));
100     assert!(!is_email_regex("nada_neutho"));
101   } 
102 }