Skip to content

Commit

Permalink
Add basic API routes (#25)
Browse files Browse the repository at this point in the history
* Add a basic actix-web server scaffolding

* Unimportant changes, removed warning
  • Loading branch information
m-milek authored Apr 7, 2024
1 parent d7fded6 commit 71dbfd1
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 11 deletions.
5 changes: 4 additions & 1 deletion xmoods-api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ Cargo.lock
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
*.pdb

# HTTP test files for Emacs Restclient
*.http
7 changes: 0 additions & 7 deletions xmoods-api/Cargo.lock

This file was deleted.

5 changes: 5 additions & 0 deletions xmoods-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.5.1"
env_logger = "0.11.3"
log = "0.4.21"
serde = "1.0.197"
serde_json = "1.0.115"
23 changes: 23 additions & 0 deletions xmoods-api/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! API module containing all routes
//!
//! Splits into two sub-routers:
//! 1. `reports/` - generating reports using data from X
//! 2. `actions/` - executing actions related X accounts and the user's XMoods account
use actix_web::{get, web, HttpResponse, Responder};

pub mod reports;

/// Welcome the user to our API
#[get("")]
async fn index() -> impl Responder {
HttpResponse::Ok().body("Hello from /api")
}

/// Configure the `api/` sub-router
/// Add all `api/` services to the passed general [`web::ServiceConfig`]
pub fn router(cfg: &mut web::ServiceConfig) {
cfg.service(index)
.service(web::scope("/reports").configure(reports::router));
}

18 changes: 18 additions & 0 deletions xmoods-api/src/api/reports/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Report generation module
//!
//! Use data from the X API to generate reports for the user
use actix_web::{get, web::ServiceConfig, HttpResponse, Responder};

/// Index route for the `reports/` module
/// Returns a welcome message
#[get("")]
async fn index() -> impl Responder {
HttpResponse::Ok().body("Hello from /api/reports")
}

/// Router for the `reports/` module
/// Adds all `reports/` services to the router
pub fn router(cfg: &mut ServiceConfig) {
cfg.service(index);
}
18 changes: 18 additions & 0 deletions xmoods-api/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Authentication module
//!
//! Verify the user's identity
use actix_web::{get, web::ServiceConfig, HttpResponse, Responder};

/// Index route for the `auth/` module
/// Returns a welcome message
#[get("")]
async fn index() -> impl Responder {
HttpResponse::Ok().body("Hello from /auth")
}

/// Router for the `auth/` module
/// Adds all `auth/` services to the router
pub fn router(cfg: &mut ServiceConfig) {
cfg.service(index);
}
39 changes: 36 additions & 3 deletions xmoods-api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
use actix_web::{get, middleware::Logger, web, App, HttpResponse, HttpServer, Responder};
use serde_json::json;

mod api;
mod auth;

/// Index route of the XMoods server.
/// # Returns
/// JSON object with a welcome message and a link to the API documentation.
#[get("/")]
async fn index() -> impl Responder {
let res = json!({
"message": "Welcome to XMoods API!",
"documentation": "https://xmoods.github.io/XMoods/doc/xmoods_api/index.html"
});
HttpResponse::Ok().json(res)
}

/// Entry point of the XMoods server.
/// Initializes the HTTP server and runs it.
fn main() {
println!("Hello, world!");
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "debug");
std::env::set_var("RUST_BACKTRACE", "1");
env_logger::init();

HttpServer::new(|| {
let logger = Logger::default();
App::new()
.wrap(logger)
.service(index)
.service(web::scope("/api").configure(api::router))
.service(web::scope("/auth").configure(auth::router))
})
.bind(("127.0.0.1", 8000))?
.run()
.await
}

#[cfg(test)]
mod tests {
#[test]
fn example_test() {
assert!(2+2 == 4)
assert_eq!(2 + 2, 4);
}
}

0 comments on commit 71dbfd1

Please sign in to comment.