Skip to content

Commit

Permalink
feat: required password for new user
Browse files Browse the repository at this point in the history
  • Loading branch information
hn275 committed Apr 10, 2023
1 parent 2ba7cc4 commit 1edcdea
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 231 deletions.
170 changes: 69 additions & 101 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,28 @@ mod masked_oid;
mod middleware;
mod services;
mod types;
mod util;

use std::env;
use std::error::Error;
use std::fs::File;
use actix_cors::Cors;
use actix_web::{
App,
HttpServer,
};
use actix_web::http::header;
use actix_web::middleware::Logger;
use actix_web::web;
use futures::{
try_join,
};
use log::{
info,
warn,
};
use actix_web::{App, HttpServer};
use futures::try_join;
use log::{info, warn};
use memmap::Mmap;
use mongodb::{
Client as MongoClient,
Database,
IndexModel,
};
use mongodb::bson::{
doc,
};
use mongodb::bson::doc;
use mongodb::options::{
Collation,
CollationStrength,
IndexOptions,
ReadConcernLevel,
UpdateOptions,
Collation, CollationStrength, IndexOptions, ReadConcernLevel, UpdateOptions,
};
use mongodb::{Client as MongoClient, Database, IndexModel};
use std::env;
use std::error::Error;
use std::fs::File;

use crate::masked_oid::MaskingKey;
use crate::middleware::HostCheckWrap;
use crate::types::{
Post,
School,
Session,
User,
Vote,
};
use crate::types::{Post, School, Session, User, Vote};

pub type GeoIpReader = &'static maxminddb::Reader<Mmap>;

Expand Down Expand Up @@ -81,13 +58,7 @@ async fn initialize_database(db: &Database) -> mongodb::error::Result<()> {
.build(),
None,
),

sessions.create_index(
IndexModel::builder()
.keys(doc! {"user": 1})
.build(),
None,
),
sessions.create_index(IndexModel::builder().keys(doc! {"user": 1}).build(), None,),
sessions.create_index(
IndexModel::builder()
.keys(doc! {"last_used": 1})
Expand All @@ -99,7 +70,6 @@ async fn initialize_database(db: &Database) -> mongodb::error::Result<()> {
.build(),
None,
),

posts.create_index(
IndexModel::builder()
.keys(doc! {"sequential_id": -1})
Expand All @@ -118,62 +88,58 @@ async fn initialize_database(db: &Database) -> mongodb::error::Result<()> {
.build(),
None,
),

async {
schools.create_index(
IndexModel::builder()
.keys(doc! {"position": "2dsphere"})
.build(),
None,
).await?;
schools
.create_index(
IndexModel::builder()
.keys(doc! {"position": "2dsphere"})
.build(),
None,
)
.await?;

schools.update_one(
doc! {
"_id": {"$eq": "UVIC"},
},
doc! {
"$set": {
"name": "University of Victoria",
"position": {
"type": "Point",
"coordinates": [-123.3117, 48.4633],
schools
.update_one(
doc! {
"_id": {"$eq": "UVIC"},
},
doc! {
"$set": {
"name": "University of Victoria",
"position": {
"type": "Point",
"coordinates": [-123.3117, 48.4633],
},
},
},
},
UpdateOptions::builder()
.upsert(true)
.build(),
).await?;
UpdateOptions::builder().upsert(true).build(),
)
.await?;

schools.update_one(
doc! {
"_id": {"$eq": "UBC"},
},
doc! {
"$set": {
"name": "University of British Columbia",
"position": {
"type": "Point",
"coordinates": [-123.2460, 49.2606],
schools
.update_one(
doc! {
"_id": {"$eq": "UBC"},
},
doc! {
"$set": {
"name": "University of British Columbia",
"position": {
"type": "Point",
"coordinates": [-123.2460, 49.2606],
},
},
},
},
UpdateOptions::builder()
.upsert(true)
.build(),
).await?;
UpdateOptions::builder().upsert(true).build(),
)
.await?;

Ok(())
},

votes.create_index(
IndexModel::builder()
.keys(doc! {"post": 1, "user": 1})
.options(
IndexOptions::builder()
.unique(true)
.build()
)
.options(IndexOptions::builder().unique(true).build())
.build(),
None,
),
Expand Down Expand Up @@ -203,12 +169,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
info!("Initializing database");

let mongo_client = MongoClient::with_uri_str(env::var("DB_CONNECT")?).await?;
let db =
mongo_client
let db = mongo_client
.default_database()
.expect("no default database");

assert_eq!(db.read_concern().map(|c| &c.level), Some(&ReadConcernLevel::Majority));
assert_eq!(
db.read_concern().map(|c| &c.level),
Some(&ReadConcernLevel::Majority)
);

initialize_database(&db).await?;

Expand All @@ -223,16 +191,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
};

HttpServer::new(move || {
let cors =
Cors::default()
.allowed_origin_fn(|origin, _req_head| {
origin.to_str()
.map(|origin| conf::PERMITTED_ORIGINS.contains(&origin))
.unwrap_or(false)
})
.allow_any_method()
.allowed_header(header::AUTHORIZATION)
.allowed_header(header::CONTENT_TYPE);
let cors = Cors::default()
.allowed_origin_fn(|origin, _req_head| {
origin
.to_str()
.map(|origin| conf::PERMITTED_ORIGINS.contains(&origin))
.unwrap_or(false)
})
.allow_any_method()
.allowed_header(header::AUTHORIZATION)
.allowed_header(header::CONTENT_TYPE);

App::new()
.wrap(cors)
Expand All @@ -257,9 +225,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
.service(services::profile::add_watched)
.service(services::profile::delete_watched)
})
.bind(("0.0.0.0", 3000))?
.run()
.await?;
.bind(("0.0.0.0", 3000))?
.run()
.await?;

Ok(())
}
Loading

0 comments on commit 1edcdea

Please sign in to comment.