Sfoglia il codice sorgente

Create outline for user create route

Lee Morgan 2 mesi fa
parent
commit
358fbecf41

+ 1 - 0
src/controllers/mod.rs

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

+ 20 - 0
src/controllers/user/create.rs

@@ -0,0 +1,20 @@
+use actix_web::{HttpResponse, web, post};
+use serde::Serialize;
+use sqlx::{PgPool, types::Uuid};
+use crate::{
+    app_error::AppError
+};
+
+#[derive(Serialize)]
+struct Body {
+    name: String,
+    email: String,
+    password: String,
+    confirm_password: String
+}
+
+pub async fn route(
+    db: web::Data<PgPool>,
+    body: web::Json<Body>
+) -> Result<HttpResponse, AppError> {
+}

+ 1 - 0
src/controllers/user/mod.rs

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

+ 11 - 2
src/main.rs

@@ -1,9 +1,16 @@
-use actix_web::{HttpServer, web, App};
+use actix_web::{HttpServer, web, middleware, App};
 use actix_cors::Cors;
+use sqlx::PgPool;
+use crate::app_error::AppError;
+
+mod app_error;
+mod routes;
+mod controllers;
 
 #[actix_web::main]
 async fn main() -> std::io::Result<()> {
     let app_env = std::env::var("APP_ENV").unwrap_or_else(|_| "development".to_string());
+    let pool = PgPool::connect("postgres://leemorgan@localhost:5432/homo").await?;
 
     HttpServer::new(move || {
         let cors = if app_env == "development" {
@@ -19,13 +26,15 @@ async fn main() -> std::io::Result<()> {
         };
 
         App::new()
+            .wrap(middleware::Compress::default())
             .wrap(cors)
+            .app_data(web::Data::new(pool))
             .app_data(
                 web::JsonConfig::default().error_handler(|err, _req| {
                     AppError::JsonDeserializationError(err.to_string()).into()
                 })
             )
-            .configure(routes::other::config)
+            .configure(routes::user::config)
     })
         .bind(("0.0.0.0", 8000))?
         .run()

+ 1 - 0
src/routes/mod.rs

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

+ 8 - 0
src/routes/user.rs

@@ -0,0 +1,8 @@
+use actix_web::web;
+use crate::controllers::user::{
+    create
+};
+
+pub fn config(cfg: &mut web::ServiceConfig) {
+    cfg.service(create::route);
+}