basic stuff
This commit is contained in:
39
src/main.rs
39
src/main.rs
@@ -1,19 +1,34 @@
|
||||
use actix_web::{get, web, App, HttpServer, Responder};
|
||||
use actix_web::{post, web, App, HttpServer, Responder};
|
||||
use database::Database;
|
||||
use log::info;
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
"Hello, World!"
|
||||
mod database;
|
||||
|
||||
struct AppState {
|
||||
db: Database,
|
||||
}
|
||||
|
||||
#[get("/{name}")]
|
||||
async fn hello(name: web::Path<String>) -> impl Responder {
|
||||
format!("Hello {}!", &name)
|
||||
#[post("/telemetry/{device_id}")]
|
||||
async fn receive_telemetry(device_id: web::Path<String>, data: web::Data<AppState>) -> impl Responder {
|
||||
data.db.add_telementry();
|
||||
format!("Hello {}!", &device_id)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(index).service(hello))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
env_logger::init();
|
||||
info!("Starting");
|
||||
|
||||
info!("Connecting to Database");
|
||||
let db = Database::init("db", "dev", "dev", "iot").await;
|
||||
db.init_db().await;
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::Data::new(AppState { db: db.clone() }))
|
||||
.service(receive_telemetry)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user