feat!: start implementing repo listing & auth middleware
This commit is contained in:
@@ -1,11 +1,53 @@
|
||||
use actix_web::{App, HttpServer};
|
||||
mod account;
|
||||
mod auth;
|
||||
mod endpoints;
|
||||
mod error;
|
||||
mod middleware;
|
||||
|
||||
struct AppState {}
|
||||
use std::env;
|
||||
|
||||
use actix_web::{App, HttpServer, middleware::from_fn, web};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::auth::{Auth, JWT};
|
||||
|
||||
struct AppState {
|
||||
reqwest_client: reqwest::Client,
|
||||
pool: PgPool,
|
||||
auth: Auth,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new())
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
let reqwest_client = reqwest::Client::new();
|
||||
let app_data = web::Data::new(AppState {
|
||||
reqwest_client: reqwest_client.clone(),
|
||||
pool: PgPool::connect(
|
||||
&env::var("DATABASE_URL").expect("DATABASE_URL environment variable must be set"),
|
||||
)
|
||||
.await
|
||||
.expect("error connecting to db"),
|
||||
auth: Auth::JWT(JWT::new(reqwest_client.clone())),
|
||||
});
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(app_data.clone())
|
||||
.route(
|
||||
"/",
|
||||
web::get()
|
||||
.to(async || concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))),
|
||||
)
|
||||
.service(
|
||||
web::scope("/api")
|
||||
.route(
|
||||
"/{user_id}/repos",
|
||||
web::get().to(endpoints::get_repos::get_repos),
|
||||
)
|
||||
.wrap(from_fn(middleware::protected)),
|
||||
)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user