Initial commit

This commit is contained in:
franklinblanco 2022-06-23 14:45:13 -04:00
commit c08f0874bd
14 changed files with 117 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
Cargo.lock
.env

14
Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "corpos-backend"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenv = "0.15.0"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "mysql" ] }
actix-web = "4"
chrono = "0.4"

0
src/dao/main_dao.rs Normal file
View File

2
src/dao/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod main_dao;
pub mod user_dao;

6
src/dao/user_dao.rs Normal file
View File

@ -0,0 +1,6 @@
use crate::r#do::user::User;
pub async fn insert_user(user: User) -> Result<(), sqlx::Error>{
Ok(())
}

1
src/do/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod user;

7
src/do/user.rs Normal file
View File

@ -0,0 +1,7 @@
use serde::Serialize;
#[derive(Serialize)]
pub struct User{
pub id: u16,
pub name: String
}

17
src/main.rs Normal file
View File

@ -0,0 +1,17 @@
mod r#do; mod dao;
mod routes; mod service;
mod util;
use util::env_util;
use routes::main_router::{start_all_routes, after_startup_fn};
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
// Retrieve env variables and send to services that need them.
let env_vars = env_util::get_dot_env_map();
// Start database
// Start server
start_all_routes(&after_startup_fn, env_vars).await
}

41
src/routes/main_router.rs Normal file
View File

@ -0,0 +1,41 @@
use std::collections::HashMap;
use actix_web::{HttpServer, App};
use super::user_routes;
// This function is to be used in case code is meant to be run after server startup
pub fn after_startup_fn(){
println!("{}", "Started server.");
}
pub async fn start_all_routes(after_startup_fn_call: &dyn Fn(), env_vars: HashMap<String, String>)
-> Result<(), std::io::Error>
{
// Get env variables to build server address
let host_addr: &str = match env_vars.get("HOST_ADDRESS") {
Some(str) => {str},
None => {panic!("HOST_ADDRESS env variable not found.");},
};
let host_port: u16 = match env_vars.get("HOST_PORT") {
Some(str) => {
match str.parse::<u16>() {
Ok(resolved_port) => {resolved_port},
Err(e) => {panic!("{:?}", e);}
}
},
None => {panic!("HOST_PORT env variable not found.");},
};
// Start server
let server_future = HttpServer::new( || {
App::new()
// Define routes
.service(user_routes::get_user_from_db)
})
.bind((host_addr, host_port))?
.run();
let (server_start_result, _after_startup_value) =
tokio::join!(server_future, async {after_startup_fn_call();});
return server_start_result; // Return server
}

3
src/routes/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod main_router;
pub mod user_routes;

View File

@ -0,0 +1,8 @@
use actix_web::{get, web, Responder, HttpResponse, body::MessageBody};
use crate::r#do::user::User;
#[get("/user/{id}")]
pub async fn get_user_from_db(id: web::Path<u16>) -> HttpResponse {
HttpResponse::Ok().json(web::Json(User {id: *id, name: "nigga".to_string()}))
}

0
src/service/mod.rs Normal file
View File

14
src/util/env_util.rs Normal file
View File

@ -0,0 +1,14 @@
extern crate dotenv;
use dotenv::dotenv;
use std::{env, collections::HashMap};
pub fn get_dot_env_map() -> HashMap<String, String>{
dotenv().ok();
let mut dotenv_vars: HashMap<String, String> = HashMap::new();
for (key, val) in env::vars() {
// Use pattern bindings instead of testing .is_some() followed by .unwrap()
dotenv_vars.insert(key, val);
}
dotenv_vars
}

1
src/util/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod env_util;