1
0
mirror of https://github.com/actix/examples synced 2025-01-22 22:05:57 +01:00
Milan Zivkovic 8dab533b40
Simple Todo API using actix-web and sqlx with postgresql database (#303)
* Simple Todo application using Actix-web with SQLx and PostgreSQL

* fix issue with not matching project name

* remove comment

* - downgrade on actix-web 2.0.0
- fixed typo
- renamed .env-example to .env.example
- removed comments
2020-04-23 17:03:04 +09:00

59 lines
1.9 KiB
Rust

#[macro_use]
extern crate log;
use dotenv::dotenv;
use listenfd::ListenFd;
use std::env;
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use sqlx::PgPool;
use anyhow::Result;
// import todo module (routes and model)
mod todo;
// default / handler
async fn index() -> impl Responder {
HttpResponse::Ok().body(r#"
Welcome to Actix-web with SQLx Todos example.
Available routes:
GET /todos -> list of all todos
POST /todo -> create new todo, example: { "description": "learn actix and sqlx", "done": false }
GET /todo/{id} -> show one todo with requested id
PUT /todo/{id} -> update todo with requested id, example: { "description": "learn actix and sqlx", "done": true }
DELETE /todo/{id} -> delete todo with requested id
"#
)
}
#[actix_rt::main]
async fn main() -> Result<()> {
dotenv().ok();
env_logger::init();
// this will enable us to keep application running during recompile: systemfd --no-pid -s http::5000 -- cargo watch -x run
let mut listenfd = ListenFd::from_env();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
let db_pool = PgPool::new(&database_url).await?;
let mut server = HttpServer::new(move || {
App::new()
.data(db_pool.clone()) // pass database pool to application so we can access it inside handlers
.route("/", web::get().to(index))
.configure(todo::init) // init todo routes
});
server = match listenfd.take_tcp_listener(0)? {
Some(listener) => server.listen(listener)?,
None => {
let host = env::var("HOST").expect("HOST is not set in .env file");
let port = env::var("PORT").expect("PORT is not set in .env file");
server.bind(format!("{}:{}", host, port))?
}
};
info!("Starting server");
server.run().await?;
Ok(())
}