1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::{net::SocketAddr, sync::Arc};
use axum::{routing::get, Extension, Router, Server};
use log::info;
use tera::Tera;
use crate::routes::{get_index, get_preview_ad, get_preview_video};
#[derive(Debug)]
pub struct State {
pub tera: Tera,
}
pub struct Application {
state: Arc<State>,
}
impl Application {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let serve_dir = match std::env::current_dir().unwrap() {
serve_path if serve_path.ends_with("serve") => serve_path,
adview_manager_path if adview_manager_path.ends_with("adview-manager") => {
adview_manager_path.join("serve")
}
workspace_path => workspace_path.join("adview-manager/serve"),
};
let templates_glob = format!("{}/templates/**/*.html", serve_dir.display());
info!("Tera templates glob path: {templates_glob}");
let tera = Tera::new(&templates_glob)?;
let shared_state = Arc::new(State { tera });
Ok(Self {
state: shared_state,
})
}
pub async fn run(&self) -> Result<(), Box<dyn std::error::Error>> {
let app = Router::new()
.route("/", get(get_index))
.route("/preview/ad", get(get_preview_ad))
.route("/preview/video", get(get_preview_video))
.layer(Extension(self.state.clone()));
let socket_addr: SocketAddr = ([127, 0, 0, 1], 3030).into();
info!("Server running on: {socket_addr}");
Server::bind(&socket_addr)
.serve(app.into_make_service())
.await?;
Ok(())
}
}