Skip to content

Commit

Permalink
Use versioned url scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
gueldenstone committed May 12, 2024
1 parent 07bb283 commit 9b4e6f4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

This is a simple rust server which uses [actix-web](https://actix.rs/) and builds on [spaceapi-rs](https://github.com/spaceapi-community/spaceapi-rs).

To retrieve the status query the `/status` endpoint using `GET`.
To retrieve the status query the `/spaceapi/14/status` endpoint using `GET`.

To set the state `POST` json encoded data for example:
To set the state `POST` json encoded data to /spaceapi/14/status/state for example:

```
'{"open": false, "message": "closed"}'
Expand Down
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod errors;
mod settings;

use actix_cors::Cors;
use actix_web::{http, middleware::Logger, web::Data, App, HttpServer};
use actix_web::{http, middleware::Logger, web, web::Data, App, HttpServer};
use spaceapi::Status;
use std::sync::Mutex;

Expand All @@ -23,7 +23,12 @@ async fn main() -> std::io::Result<()> {

log::info!("Listening on {}", settings.endpoint);

let status = Status { ..settings.status };
let status = Status {
..settings.status.clone()
};

let api_version = settings.get_api_version().unwrap();
let path_prefix = settings.path_prefix;

let app_state_data = Data::new(AppState {
status: Mutex::new(status),
Expand All @@ -38,8 +43,11 @@ async fn main() -> std::io::Result<()> {
App::new()
.wrap(logger)
.wrap(cors)
.service(api::get_status)
.service(api::set_state)
.service(
web::scope(format!("{}/{}", path_prefix, api_version).as_str())
.service(api::get_status)
.service(api::set_state),
)
.app_data(app_state_data.clone())
})
.bind(settings.endpoint)?
Expand Down
1 change: 1 addition & 0 deletions src/settings/default_config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
endpoint = "127.0.0.1:8080"
path_prefix = "spaceapi"

[status]
api_compatibility = ["14"]
Expand Down
18 changes: 16 additions & 2 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use config::{Config, ConfigError, Environment, File, FileFormat};
use serde::{Deserialize, Serialize};
use spaceapi::Status;
use spaceapi::{ApiVersion, Status};

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Settings {
pub endpoint: String,
pub status: Status,
pub path_prefix: String,
}

impl Settings {
Expand All @@ -23,4 +24,17 @@ impl Settings {
}
builder.build()?.try_deserialize()
}

pub fn get_api_version(&self) -> Result<String, ConfigError> {
match self
.status
.api_compatibility
.as_ref()
.ok_or(ConfigError::NotFound("api compatibility".to_string()))?
.first()
.ok_or(ConfigError::NotFound("api compatibility".to_string()))?
{
ApiVersion::V14 => Ok("14".to_string()),
}
}
}

0 comments on commit 9b4e6f4

Please sign in to comment.