init: Scaffolding for axum serving

This commit is contained in:
Jeremy Wall 2024-02-01 15:25:51 -05:00
parent 06778ccc94
commit f5609bd6b0
4 changed files with 97 additions and 0 deletions

5
.gitignore vendored
View File

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

19
Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
name = "heracles"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-io = "2.3.1"
axum = "0.7.4"
clap = { version = "4.4.18", features = ["derive"] }
maud = { version = "0.26.0", features = ["axum"] }
prometheus-http-api = "0.2.0"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
smol = "2.0.0"
smol-axum = "0.1.0"
smol-macros = "0.1.0"

37
src/main.rs Normal file
View File

@ -0,0 +1,37 @@
// Copyright 2021 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::io;
use std::net::TcpListener;
use std::sync::Arc;
use async_io::Async;
use axum::{self, routing::*, Router};
use smol_macros::main;
mod routes;
main! {
async fn main(ex: &Arc<smol_macros::Executor<'_>>) -> io::Result<()> {
let router = Router::new()
// JSON api endpoints
.nest("/api", routes::mk_api_routes())
// HTMX ui component endpoints
.nest("/ui", routes::mk_ui_routes())
.route("/", get(routes::index));
// TODO(jwall): Take this from clap arguments
let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 3000)).unwrap();
smol_axum::serve(ex.clone(), listener, router).await
}
}

36
src/routes.rs Normal file
View File

@ -0,0 +1,36 @@
// Copyright 2021 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use maud::{html, Markup};
use axum::Router;
pub fn mk_api_routes() -> Router {
Router::new()
}
pub fn mk_ui_routes() -> Router {
Router::new()
}
pub async fn index() -> Markup {
html! {
html {
head {
title { ("Heracles - Prometheus Unshackled") }
}
body {
("hello world")
}
}
}
}