From f5609bd6b0382bba46e3ff6edae5e630e3545d12 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 1 Feb 2024 15:25:51 -0500 Subject: [PATCH] init: Scaffolding for axum serving --- .gitignore | 5 +++++ Cargo.toml | 19 +++++++++++++++++++ src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ src/routes.rs | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/routes.rs diff --git a/.gitignore b/.gitignore index 6985cf1..196e176 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,8 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..bb86556 --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6e13556 --- /dev/null +++ b/src/main.rs @@ -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>) -> 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::::bind(([127, 0, 0, 1], 3000)).unwrap(); + smol_axum::serve(ex.clone(), listener, router).await + } +} diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..91b9285 --- /dev/null +++ b/src/routes.rs @@ -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") + } + } + } +}