added protected routes and file restructure
This commit is contained in:
parent
5214b6b88a
commit
0db17273df
@ -1,22 +1,39 @@
|
|||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
// use actix_files as fs;
|
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
mod handlers;
|
mod handlers; // Ensure handlers.rs exists
|
||||||
|
mod middleware; // Ensure middleware.rs exists // Ensure db.rs exists
|
||||||
mod models;
|
mod models;
|
||||||
|
|
||||||
|
use crate::middleware::AuthMiddleware;
|
||||||
|
use handlers::{admin_login, create_admin, create_form, get_forms, get_submissions, submit_form};
|
||||||
|
|
||||||
|
pub fn configure_routes(cfg: &mut web::ServiceConfig) {
|
||||||
|
cfg.service(
|
||||||
|
web::scope("")
|
||||||
|
.route("/forms/{id}", web::post().to(submit_form))
|
||||||
|
.route("/admin/login", web::post().to(admin_login))
|
||||||
|
.route("/admin/create", web::post().to(create_admin)),
|
||||||
|
);
|
||||||
|
|
||||||
|
cfg.service(
|
||||||
|
web::scope("")
|
||||||
|
.wrap(AuthMiddleware)
|
||||||
|
.route("/forms", web::get().to(get_forms))
|
||||||
|
.route("/forms", web::post().to(create_form))
|
||||||
|
.route("/forms/{id}/submissions", web::get().to(get_submissions)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
// Initialize the database connection
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let db = Arc::new(Mutex::new(
|
let db = Arc::new(Mutex::new(
|
||||||
db::init_db().expect("Failed to initialize the database"),
|
db::init_db().expect("Failed to initialize the database"),
|
||||||
));
|
));
|
||||||
|
|
||||||
// Start the Actix-Web server
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(
|
.wrap(
|
||||||
@ -26,17 +43,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.allow_any_method(),
|
.allow_any_method(),
|
||||||
)
|
)
|
||||||
.app_data(web::Data::new(db.clone()))
|
.app_data(web::Data::new(db.clone()))
|
||||||
// .service(fs::Files::new("/", "./frontend/public").index_file("index.html"))
|
.configure(configure_routes)
|
||||||
.route("/forms", web::post().to(handlers::create_form))
|
|
||||||
.route("/forms", web::get().to(handlers::get_forms))
|
|
||||||
.route(
|
|
||||||
"/forms/{id}/submissions",
|
|
||||||
web::post().to(handlers::submit_form),
|
|
||||||
)
|
|
||||||
.route(
|
|
||||||
"/forms/{id}/submissions",
|
|
||||||
web::get().to(handlers::get_submissions),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
|
Loading…
Reference in New Issue
Block a user