add skeleton for endpoints

This commit is contained in:
anna 2022-12-05 02:59:48 +01:00
parent f8f685bd4e
commit 6192aa76c8
Signed by: fef
GPG key ID: EC22E476DC2D3D84
5 changed files with 47 additions and 10 deletions

View file

@ -1,17 +1,13 @@
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
use sqlx::postgres::PgPoolOptions;
use sqlx::{migrate, query, PgPool, Pool};
mod conf;
mod route;
mod state;
use conf::Config;
use state::AppState;
#[get("/")]
async fn hello(state: AppState) -> impl Responder {
HttpResponse::Ok().body(format!("hello world from port {}", state.config.bind_port))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenvy::dotenv().ok();
@ -21,10 +17,15 @@ async fn main() -> std::io::Result<()> {
let bind_params = (config.bind_addr.clone(), config.bind_port.clone());
let state = state::new(config, db_pool);
HttpServer::new(move || App::new().app_data(state.clone()).service(hello))
.bind(bind_params)?
.run()
.await
HttpServer::new(move || {
App::new()
.app_data(state.clone())
.service(hello)
.configure(route::configure)
})
.bind(bind_params)?
.run()
.await
}
async fn init_db(conf: &Config) -> sqlx::Result<PgPool> {

7
src/route/api/mod.rs Normal file
View file

@ -0,0 +1,7 @@
use actix_web::web;
pub mod v1;
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/v1").configure(v1::configure));
}

View file

@ -0,0 +1,15 @@
use crate::state::AppState;
use actix_web::{get, web, HttpRequest, HttpResponse, Responder};
#[get("/{id}")]
async fn get_by_id(path: web::Path<i32>, state: AppState) -> impl Responder {
let id = path.into_inner();
HttpResponse::Ok().body(format!(
"hello uid {}, listening on port {}",
id, state.config.bind_port
))
}
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(get_by_id);
}

7
src/route/api/v1/mod.rs Normal file
View file

@ -0,0 +1,7 @@
use actix_web::web;
pub mod accounts;
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/accounts").configure(accounts::configure));
}

7
src/route/mod.rs Normal file
View file

@ -0,0 +1,7 @@
use actix_web::web;
pub mod api;
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/api").configure(api::configure));
}