-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a basic actix-web server scaffolding * Unimportant changes, removed warning
- Loading branch information
Showing
7 changed files
with
104 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |