-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.rs
38 lines (36 loc) · 1.2 KB
/
api.rs
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
use anyhow::Result;
use axum::{
routing::{get, post},
Router,
};
use crate::routes::{compile, create_gist, evaluate, static_css, static_html, static_js};
use crate::GithubClient;
use std::net::SocketAddr;
/// serve the api
pub async fn serve(addr: SocketAddr, github_client: GithubClient) -> Result<()> {
let static_routes = Router::new()
.route(
"/web.css",
get(|| async { static_css(include_bytes!("../static/web.css")) }),
)
.route(
"/web.js",
get(|| async { static_js(include_bytes!("../static/web.js")) }),
)
.route(
"/mode-pony.js",
get(|| async { static_js(include_bytes!("../static/mode-pony.js")) }),
);
let router = Router::new()
.route(
"/",
get(|| async { static_html(include_bytes!("../static/web.html")) }),
)
.route("/evaluate.json", post(evaluate))
.route("/compile.json", post(compile))
.route("/gist.json", post(create_gist))
.with_state(github_client)
.nest("/static", static_routes);
let listener = tokio::net::TcpListener::bind(&addr).await?;
Ok(axum::serve(listener, router).await?)
}