add skeleton for endpoints
This commit is contained in:
parent
f8f685bd4e
commit
6192aa76c8
5 changed files with 47 additions and 10 deletions
21
src/main.rs
21
src/main.rs
|
@ -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
7
src/route/api/mod.rs
Normal 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));
|
||||
}
|
15
src/route/api/v1/accounts.rs
Normal file
15
src/route/api/v1/accounts.rs
Normal 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
7
src/route/api/v1/mod.rs
Normal 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
7
src/route/mod.rs
Normal 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));
|
||||
}
|
Loading…
Reference in a new issue