first prototype with builtin functions

This commit is contained in:
Jonas_Jones 2023-12-08 18:33:02 +01:00
parent 9b75ff869b
commit b852500513
3 changed files with 25 additions and 21 deletions

View file

@ -2,6 +2,7 @@ use std::env;
use warp::Filter;
use crate::v1::get_v1_routes;
use crate::{Logger, parse_ip};
@ -13,18 +14,11 @@ pub async fn serve() {
let socket_addr = parse_ip();
// GET /hello/warp => 200 OK with body "Hello, warp!"
let hello = warp::path!("hello" / String)
.map(|name| format!("Hello, {}!", name));
let root = warp::path::end()
.map(|| warp::reply::html("<h1>jonas_jones-api!</h1>\n<p>this is a placeholder (hopefully)</p>"));
let v1 = warp::path!("v1" / String)
.map(|name| format!("Hello, {}!", name));
// GET (any) => reply with return from handle_path
let routes = get_v1_routes()
.or(warp::any().map(|| warp::reply::with_status("Not Found", warp::http::StatusCode::NOT_FOUND)));
let routes = hello.or(v1).or(root);
warp::serve(routes)
.run(socket_addr)

View file

@ -1,11 +1,19 @@
pub fn help() -> &'static str {
return "Please refer to the wiki at https://wiki.jonasjones.dev/Api/"
}
// pub fn help() -> warp::reply::Response {
pub fn ping() -> &'static str {
return "pong"
}
// return warp::reply::Reply::with_header(
// warp::reply::html(""),
// "Content-Type",
// "text/html",
// );
// }
pub fn version() -> &'static str {
return option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
// create a function help() that returns a response with a ststus code of 200 and a body of "help"
use warp::Filter;
pub fn get_builtin_routes() -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path("v1")
.and((warp::path("help").map(|| "help"))
.or(warp::path("ping").map(|| "pong"))
.or(warp::path("version").map(|| warp::reply::json(&[option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")]))))
}

View file

@ -1,5 +1,7 @@
mod builtin;
pub use builtin::help as builtin_help;
pub use builtin::ping as builtin_ping;
pub use builtin::version as builtin_version;
pub use builtin::get_builtin_routes as get_v1_builtin_routes;
pub fn get_v1_routes() -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
return get_v1_builtin_routes();
}