summaryrefslogtreecommitdiff
path: root/src/public/docs.rs
blob: 397e696635affed738b56d51860b63160b9437b9 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use axum::response::Response;
use lazy_static::lazy_static;
use tokio::sync::Mutex;

use crate::{
    api::{admin, auth, posts, users},
    types::http::ResponseCode,
};

use super::console::beautify;

pub enum EndpointMethod {
    Post,
    Put,
    Patch,
}

impl ToString for EndpointMethod {
    fn to_string(&self) -> String {
        match self {
            Self::Post => "POST".to_owned(),
            Self::Put => "PUT".to_owned(),
            Self::Patch => "PATCH".to_owned(),
        }
    }
}

pub struct EndpointDocumentation<'a> {
    pub uri: &'static str,
    pub method: EndpointMethod,
    pub description: &'static str,
    pub body: Option<&'static str>,
    pub responses: &'a [(u16, &'static str)],
    pub cookie: Option<&'static str>,
}

lazy_static! {
    static ref ENDPOINTS: Mutex<Vec<String>> = Mutex::new(Vec::new());
}

fn generate_body(body: Option<&'static str>) -> String {
    let Some(body) = body else {
        return String::new()
    };
    let html = r#"
    <h2>Body</h2>
    <div class="body">
        $body
    </div>
    "#
    .to_string();

    let body = body.trim();
    if body.starts_with('{') {
        return html.replace(
            "$body",
            &beautify(body)
                .replace("<span class='key'", "<br><span class='key'")
                .replace('}', "<br>}"),
        );
    }
    html.replace("$body", body)
}

fn generate_responses(responses: &[(u16, &'static str)]) -> String {
    let mut html = r#"
    <h2>Responses</h2>
    $responses
    "#
    .to_string();

    for response in responses {
        let res = format!(
            r#"
            <div>
                <span class="ptype">{}</span>
                <span class="pdesc">{}</span>
            </div>
            $responses
            "#,
            response.0, response.1
        );
        html = html.replace("$responses", &res);
    }

    html.replace("$responses", "")
}

fn generate_cookie(cookie: Option<&'static str>) -> String {
    let Some(cookie) = cookie else {
        return String::new()
    };
    format!(
        r#"<span class="auth"><span>{cookie}</span> cookie is required for authentication</span>"#
    )
}

fn generate_endpoint(doc: &EndpointDocumentation) -> String {
    let html = r#"
    <div>
        <div class="endpoint">
            <span class="method $method_class">$method</span>
            <span class="uri">$uri</span>
            <span class="desc">$description</span>
            $cookie
        </div>
        <div class="info">
            $body
            $responses
        </div>
    </div>
    "#;

    html.replace("$method_class", &doc.method.to_string().to_lowercase())
        .replace("$method", &doc.method.to_string())
        .replace("$uri", doc.uri)
        .replace("$description", doc.description)
        .replace("$cookie", &generate_cookie(doc.cookie))
        .replace("$body", &generate_body(doc.body))
        .replace("$responses", &generate_responses(doc.responses))
}

pub async fn init() {
    let docs = vec![
        auth::AUTH_REGISTER,
        auth::AUTH_LOGIN,
        auth::AUTH_LOGOUT,
        posts::POSTS_CREATE,
        posts::POSTS_PAGE,
        posts::COMMENTS_PAGE,
        posts::POSTS_USER,
        posts::POSTS_COMMENT,
        posts::POSTS_LIKE,
        users::USERS_LOAD,
        users::USERS_PAGE,
        users::USERS_SELF,
        users::USERS_AVATAR,
        users::USERS_BANNER,
        users::USERS_FOLLOW,
        users::USERS_FOLLOW_STATUS,
        users::USERS_FRIENDS,
        admin::ADMIN_AUTH,
        admin::ADMIN_QUERY,
        admin::ADMIN_POSTS,
        admin::ADMIN_USERS,
        admin::ADMIN_SESSIONS,
        admin::ADMIN_COMMENTS,
        admin::ADMIN_LIKES,
    ];
    let mut endpoints = ENDPOINTS.lock().await;
    for doc in docs {
        endpoints.push(generate_endpoint(&doc));
    }
}

pub async fn generate() -> Response {
    let mut data = String::new();
    {
        let endpoints = ENDPOINTS.lock().await;
        endpoints.iter().for_each(|endpoint| {
            data.push_str(endpoint);
        });
    }

    let html = format!(
        r#"
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/header.css">
            <link rel="stylesheet" href="/css/console.css">
            <link rel="stylesheet" href="/css/api.css">
            <title>XSSBook - API Documentation</title>
        </head>
        <body>
            <div id="header">
                <span class="logo"><a href="/">xssbook</a></span>
                <span class="gtext desc" style="margin-left: 6em; font-size: 2em; color: #606770">API Documentation</span>
            </div>
            <div id="docs">
            {data}
            </div>
        </body>
        </html>
        "#
    );

    ResponseCode::Success.html(&html)
}