aboutsummaryrefslogtreecommitdiff
path: root/api/src
diff options
context:
space:
mode:
Diffstat (limited to 'api/src')
-rw-r--r--api/src/db.rs22
-rw-r--r--api/src/main.rs41
-rw-r--r--api/src/routes.rs17
3 files changed, 59 insertions, 21 deletions
diff --git a/api/src/db.rs b/api/src/db.rs
new file mode 100644
index 0000000..59b86c5
--- /dev/null
+++ b/api/src/db.rs
@@ -0,0 +1,22 @@
+use mongodb::{error::Error, options::ClientOptions, Client};
+use std::env;
+
+pub struct User {
+ pub name: String,
+ pub id: String,
+ pub secret: String,
+ pub pass_salt: String,
+ pub pass_hash: String,
+}
+
+pub async fn init() -> Result<Client, Error> {
+ let host = env::var("MONGO_HOST").expect("MONGO_HOST is not set");
+ let port = env::var("MONGO_PORT").expect("MONGO_PORT is not set");
+
+ let mut client_options = ClientOptions::parse(format!("mongodb://{}:{}", host, port)).await?;
+
+ client_options.app_name = Some("pressure-api".to_string());
+
+ let client = Client::with_options(client_options)?;
+ Ok(client)
+}
diff --git a/api/src/main.rs b/api/src/main.rs
index b2e75c9..8d4f537 100644
--- a/api/src/main.rs
+++ b/api/src/main.rs
@@ -1,31 +1,30 @@
extern crate log;
-extern crate mongodb;
extern crate simple_logger;
extern crate tokio;
-use mongodb::{bson::doc, options::ClientOptions, Client};
+use actix_web::{web, App, HttpServer};
use simple_logger::SimpleLogger;
+use std::io::Result;
+use std::sync::*;
-#[tokio::main]
-async fn main() -> mongodb::error::Result<()> {
+mod db;
+mod routes;
+
+fn init_log() {
SimpleLogger::new().init().unwrap();
log::set_max_level(log::LevelFilter::Info);
+}
- let mut client_options = ClientOptions::parse("mongodb://localhost:27017").await?;
-
- client_options.app_name = Some("pressure-api".to_string());
-
- let client = Client::with_options(client_options)?;
-
- client
- .database("admin")
- .run_command(doc! {"ping": 1}, None)
- .await?;
- log::info!("connected to mongodb");
-
- for db_name in client.list_database_names(None, None).await? {
- log::info!("{}", db_name);
- }
-
- Ok(())
+#[actix_rt::main]
+async fn main() -> Result<()> {
+ init_log();
+ let client = web::Data::new(Mutex::new(db::init()));
+ HttpServer::new(move || {
+ App::new()
+ .app_data(client.clone())
+ .service(web::scope("/").configure(routes::export_routes))
+ })
+ .bind("127.0.0.1:8080")?
+ .run()
+ .await
}
diff --git a/api/src/routes.rs b/api/src/routes.rs
new file mode 100644
index 0000000..4af3f72
--- /dev/null
+++ b/api/src/routes.rs
@@ -0,0 +1,17 @@
+use actix_web::{web, Responder};
+use mongodb::{Client, Collection};
+use std::sync::Mutex;
+
+#[path = "./db.rs"]
+mod db;
+
+pub fn export_routes(cfg: &mut web::ServiceConfig) {
+ cfg.service(web::resource("/hello").route(web::get().to(hello)));
+}
+
+pub async fn hello(data: web::Data<Mutex<Client>>) -> impl Responder {
+ let test_db: Collection<db::User> =
+ data.lock().unwrap().database("pressure").collection("gert");
+
+ return format!("Hello world!");
+}