initial test project

This commit is contained in:
Willem Schipper 2023-11-20 16:59:07 +01:00
parent e8f9f722a3
commit f46a9b690c
4 changed files with 77 additions and 0 deletions

5
.gitignore vendored
View File

@ -14,3 +14,8 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Added by cargo
/target

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "url_shortener"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
axum = { version = "0.6.20", features = ["http2"] }
ini = "1.3.0"
redis = { version = "0.23.3", features = ["r2d2", "ahash", "connection-manager"] }
tokio = { version = "1.34.0", features = ["full"] }

6
config.ini Normal file
View File

@ -0,0 +1,6 @@
[database]
type = redis
server = 127.0.0.1:6379
[web_server]
bind_address = 127.0.0.1:12432

54
src/main.rs Normal file
View File

@ -0,0 +1,54 @@
mod url_shortener;
mod database_adapter;
mod redis_adapter;
use std::net::SocketAddr;
use ini::ini;
use axum::{routing::get, Router, response::Html};
#[tokio::main]
async fn main() {
let config = ini!("config.ini");
let server_address = match &config["database"]["server"] {
Some(adr) => adr,
None => panic!("Could not find [database]/server in config")
};
let web_bind_address = match &config["web_server"]["bind_address"] {
Some(adr) => adr,
None => panic!("Could not find [web_server]/bind_address in config")
};
let app = Router::new()
.route("/", get(root))
.route("/test", get(get_test).post(post_test));
let addr: SocketAddr = web_bind_address.parse().expect("Unable to parse [web_server]/bind_address in config");
dbg!(addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn root() -> Html<&'static str> {
Html("root<br>
<a href=\"/test\">test</a>
<br>
<form action=\"test\" method=\"post\">
<input type=\"submit\" />
</form>
")
}
async fn get_test() -> &'static str {
"get_test"
}
async fn post_test() -> &'static str {
"post_test"
}