use rustlavel::prelude::*;
mod orders;
mod tokens;
#[path = "../database/migrations/mod.rs"]
mod migrations;
pub use {{crate_name}}_shared::{Caller, problem};
#[rustlavel::main]
async fn main() -> Result<()> {
let app = App::new()?;
let url = rustlavel::env::env_or("API_DATABASE_URL", "");
if url.is_empty() {
return Err(Error::msg(
"API_DATABASE_URL is not set. The resource server keeps its domain in its own database, \
separate from the authorization server's.",
));
}
let db = Database::connect(&url).await?;
let cache = CacheStore::from_config(app.config())?;
let registrar = {{crate_name}}_shared::join("api", app.config());
app.on_shutdown({{crate_name}}_shared::farewell(registrar))
.state(db.clone())
.state(cache)
.state(tokens::Verifier::from_env())
.middleware(tokens::RequireCaller)
.migrations(migrations::all())
.routes(routes)
.run()
.await
}
fn routes(r: &mut Router) {
r.get("/health", |_req: Request| async { Json::object([("status", Json::from("up"))]) });
r.get("/api/me", |req: Request| async move {
match req.extension::<Caller>() {
Some(caller) => Response::json(caller.to_json()),
None => Response::new(Status::UNAUTHORIZED).with_json(problem("Unauthenticated.")),
}
});
orders::routes(r);
}