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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
use axum::extract::{Query, State};
use axum::{Router, routing::get};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Clone)]
struct AppState {
reqwest : Client,
config : Config,
}
#[derive(Serialize, Debug)]
struct Webhook {
pub content : String,
}
#[derive(Deserialize, Debug, Clone)]
struct Config {
webhook_public : String,
webhook_private : String,
}
impl Config {
pub fn load(from : PathBuf) -> Config {
toml::from_str(
std::fs::read_to_string(from)
.expect("failed to read config")
.as_str(),
)
.expect("failed to parse config")
}
}
#[derive(Deserialize, Debug)]
struct OpenCloseQueryParam {
public : bool,
}
async fn handle_startup() {
println!("can I haz button plz")
}
async fn handle_open(State(state) : State<AppState>, query : Query<OpenCloseQueryParam>) {
state
.reqwest
.post(
if query.public {
state.config.webhook_public
} else {
state.config.webhook_private
},
)
.json(&Webhook {
content : "The space is now open!".to_string(),
})
.send()
.await
.unwrap();
}
async fn handle_close(State(state) : State<AppState>, query : Query<OpenCloseQueryParam>) {
state
.reqwest
.post(
if query.public {
state.config.webhook_public
} else {
state.config.webhook_private
},
)
.json(&Webhook {
content : "The space is now closed!".to_string(),
})
.send()
.await
.unwrap();
}
#[tokio::main]
async fn main() {
let config = if cfg!(debug_assertions) {
// use local config
Config::load(PathBuf::from("config.toml"))
} else {
// now it's in docker, use the bind config!
Config::load(PathBuf::from("/app/config.toml"))
};
let state = AppState {
reqwest : Client::new(),
config,
};
let app = Router::new()
.route("/startup", get(handle_startup))
.route("/open", get(handle_open))
.route("/close", get(handle_close))
.with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
|