Skip to content

Commit

Permalink
feat: server
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenLoc committed Oct 15, 2024
1 parent d6825e0 commit c3943e2
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 18 deletions.
356 changes: 356 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ clap = { version = "4.4.8", features = ["derive", "unstable-doc", "env"] }
clap-markdown = "0.1.3"
clap-help = "1.3"

webbrowser = "1.0.2"

[profile.dev.package."*"]
opt-level = 1
3 changes: 2 additions & 1 deletion askama.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dirs = ["files"]
[general]
dirs = ["files/templates"]
31 changes: 17 additions & 14 deletions files/assets/app.css
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@


.qr-table{
overflow-y: auto;
}

.fixed-container{
height: 300px;
padding-bottom: 3px;
}

.qr-container{
padding-bottom: 30px;
display: flex;
flex-direction: row;
.sidebar{
margin: 0;
padding: 0;
width: 100px;
height: 100%;
}

.generate-form{
margin-left: 20px;
.flexcontainer{
display: flex;
flex-direction: row;
}

.qrcode{
margin-top: 10px;
width: 300px;
height: 300px;
.border{
border-style: solid;
border-width: 2px;
border-color: white;
}


.full{
width: 100%;
height: 100%;
}

:root {
--pico-font-family-sans-serif: Inter, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, Helvetica, Arial, "Helvetica Neue", sans-serif, var(--pico-font-family-emoji);
Expand Down
6 changes: 6 additions & 0 deletions files/templates/content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<article class="full">
<p>Some content</p>

</article>

55 changes: 55 additions & 0 deletions files/templates/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html data-theme="dark">
<link rel="stylesheet" href="_assets/pico.min.css">
<link rel="stylesheet" href="_assets/app.css">
<script src="/_assets/htmx.min.js"></script>

<head>
<title>{{ title }}</title>
</head>
<body id="full_body">
<div class="container">
<nav>
<ul>
<li><strong>{{ title }}</strong></li>
</ul>
<ul>
<li><button class="primary">Files</button></li>
<li><button class="secondary">Settings</button></li>
</ul>
</nav>
</div>
<div class="container" >
<div class="flexcontainer">
<aside class="sidebar">
<nav>
<ul>
<li><a href="#">Folder1</a></li>
<li><a href="#">Folder2</a></li>
<li><a href="#">Folder3</a></li>
</ul>
</nav>
</aside>

<div id="content"
class="border"
hx-get="/content"
hx-trigger="load"
>


</div>


</div>




</div>




</body>
</html>
13 changes: 10 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use clap::{CommandFactory, Parser, Subcommand};
use clap_help::Printer;

use crate::out::{blue_fog, explain_fog, green_fog, white_fog, Foggy};
use crate::{
out::{blue_fog, explain_fog, white_fog, Foggy},
server,
};

pub async fn figure() -> Foggy {
let cli = Cli::parse();
Expand All @@ -16,7 +19,7 @@ pub async fn figure() -> Foggy {
explain_fog();
white_fog("This should never happen")
}
Commands::Pit => green_fog("Nothing yet"),
Commands::Pit { port } => server::start_server(port).await,
},
}
}
Expand Down Expand Up @@ -46,5 +49,9 @@ enum Commands {
Markdown,
Explain,
/// [Preview] does nothing for now
Pit,
Pit {
// set port of the tool
#[arg(short, long, env = "DY_PORT", default_value_t = String::from("3000"))]
port: String,
},
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod commands;
mod out;
mod server;

#[tokio::main]
async fn main() {
Expand Down
24 changes: 24 additions & 0 deletions src/server/html/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use askama::Template;
use axum::response::Html;

#[derive(Debug, Template)]
#[template(path = "page.html")]
struct PageTemplate {
title: String,
}

pub async fn render_page_html() -> Html<String> {
let page = PageTemplate {
title: "Fog Pit".to_string(),
};
Html(page.render().unwrap())
}

#[derive(Debug, Template)]
#[template(path = "content.html")]
struct ContentTemplate;

pub async fn render_content_html() -> Html<String> {
let content = ContentTemplate;
Html(content.render().unwrap())
}
37 changes: 37 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use axum::{http::StatusCode, response::IntoResponse, routing::get, Router};

use crate::out::green_fog;
mod html;

pub async fn start_server(port: impl Into<String>) -> crate::out::Foggy {
tracing_subscriber::fmt::init();

let app = Router::new()
.route("/healthz", get(health))
.route("/", get(html::render_page_html))
.route("/content", get(html::render_content_html))
.nest("/", assets());

let p = format!("0.0.0.0:{}", port.into());
let listener = tokio::net::TcpListener::bind(p.clone()).await.unwrap();

let path = format!("http://{}", p);
if webbrowser::open(&path).is_ok() {
axum::serve(listener, app).await.unwrap();
}
green_fog("Normal server shutdown")
}

async fn health() -> impl IntoResponse {
StatusCode::OK
}

use tower_http::services::ServeDir;

fn assets() -> Router {
Router::new().nest_service("/_assets", using_serve_dir())
}

pub fn using_serve_dir() -> ServeDir {
ServeDir::new("files/assets")
}

0 comments on commit c3943e2

Please sign in to comment.