From 1e0fb0a8fab5c9f2f8a58c2637830522863ef04f Mon Sep 17 00:00:00 2001 From: Jokler Date: Sun, 9 Feb 2020 22:19:38 +0100 Subject: Webserver: Add API documentation and basic css --- src/web_server.rs | 12 +++++ static/fonts/.gitkeep | 0 static/style.css | 63 +++++++++++++++++++++++++ templates/base.htm | 1 + templates/docs/api.htm | 126 +++++++++++++++++++++++++++++++++++++++++++++++++ templates/index.htm | 4 ++ 6 files changed, 206 insertions(+) create mode 100644 static/fonts/.gitkeep create mode 100644 static/style.css create mode 100644 templates/docs/api.htm diff --git a/src/web_server.rs b/src/web_server.rs index 0342a38..0097fb1 100644 --- a/src/web_server.rs +++ b/src/web_server.rs @@ -5,6 +5,8 @@ use actix::{Addr, SyncArbiter}; use actix_web::{ get, http::header, middleware::Logger, post, web, App, HttpResponse, HttpServer, Responder, }; +use askama::actix_web::TemplateIntoResponse; +use askama::Template; use serde::{Deserialize, Serialize}; use crate::bot::MasterBot; @@ -41,6 +43,7 @@ pub async fn start(args: WebServerArgs) -> std::io::Result<()> { .service(api::get_bot_list) .service(api::get_bot), ) + .service(web::scope("/docs").service(get_api_docs)) .service(actix_files::Files::new("/static", "static/")) }) .bind(args.bind_address)? @@ -93,6 +96,15 @@ async fn get_bot( } } +#[derive(Template)] +#[template(path = "docs/api.htm")] +struct ApiDocsTemplate; + +#[get("/api")] +async fn get_api_docs() -> impl Responder { + ApiDocsTemplate.into_response() +} + mod filters { use std::time::Duration; diff --git a/static/fonts/.gitkeep b/static/fonts/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..09a985c --- /dev/null +++ b/static/style.css @@ -0,0 +1,63 @@ +@font-face { + font-family: 'roboto-regular'; + src: url('fonts/Roboto-Regular.ttf') format('truetype'); +} + +@font-face { + font-family: 'roboto-light'; + src: url('fonts/Roboto-Light.ttf') format('truetype'); +} + +@font-face { + font-family: 'roboto-bold'; + src: url('fonts/Roboto-Bold.ttf') format('truetype'); +} + +body { + background-color: #151515; +} + +main { + margin: auto; + max-width: 800px; + padding: 1em; + background-color: #202020; + color: #eee; + font-family: 'roboto-regular', Arial; +} + +nav > a { + font-size: 1.4rem; +} + +a, a:visited { + color: #eee; +} + +a:hover { + color: #ccc; +} + +pre { + font-size: 0.9rem; + font-family: monospace; + background-color: #151515; + overflow: auto; + padding: 1em; +} + +.code-background { + background-color: #151515; +} + +.code-normal { + color: #c0c5ce; +} + +.code-string { + color:#a3be8c; +} + +.code-number { + color: #d08770; +} diff --git a/templates/base.htm b/templates/base.htm index 7810f21..b8b2f49 100644 --- a/templates/base.htm +++ b/templates/base.htm @@ -4,6 +4,7 @@ + {% block title %}{{ title }} - PokeBot{% endblock %} diff --git a/templates/docs/api.htm b/templates/docs/api.htm new file mode 100644 index 0000000..a973272 --- /dev/null +++ b/templates/docs/api.htm @@ -0,0 +1,126 @@ +{% extends "base.htm" %} + +{% block title %}API Documentation{% endblock %} + +{% block content %} +

API Documentation

+ + +

Bot list

+

Show a list of all bots.

+ +

URL: /api/bots

+

Method: GET

+

Auth required: NO

+ +

Success Response

+ +

Code: 200 OK

+ +

Content example

+ + +
+[
+  {
+    "name": "MusicBot",
+    "state": "Playing",
+    "volume": 0.5,
+    "position": {
+      "secs": 10,
+      "nanos": 63573687
+    },
+    "currently_playing": {
+      "url": "<temp_url>",
+      "webpage_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
+      "title": "Rick Astley - Never Gonna Give You Up (Video)",
+      "thumbnail": "https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg",
+      "duration": {
+        "secs": 212,
+        "nanos": 0
+      }
+    },
+    "playlist": [
+      {
+        "url": "<temp_url>",
+        "webpage_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
+        "title": "Rick Astley - Never Gonna Give You Up (Video)",
+        "thumbnail": "https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg",
+        "duration": {
+          "secs": 212,
+          "nanos": 0
+        }
+      }
+    ]
+  }
+]
+
+ + +

Show Bot

+

Show a specific bot.

+ +

URL: /api/bots/:botname

+

Method: GET

+

Auth required: NO

+ +

Success Response

+

Code: 200 OK

+ +

Content example

+ + +
+{
+  "name": "MusicBot",
+  "state": "Playing",
+  "volume": 0.5,
+  "position": {
+    "secs": 142,
+    "nanos": 690911766
+  },
+  "currently_playing": {
+    "url": "<temp_url>",
+    "webpage_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
+    "title": "Rick Astley - Never Gonna Give You Up (Video)",
+    "thumbnail": "https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg",
+    "duration": {
+      "secs": 212,
+      "nanos": 0
+    }
+  },
+  "playlist": [
+    {
+      "url": "<temp_url>",
+      "webpage_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
+      "title": "Rick Astley - Never Gonna Give You Up (Video)",
+      "thumbnail": "https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg",
+      "duration": {
+        "secs": 212,
+        "nanos": 0
+      }
+    }
+  ]
+}
+
+ +

Error Response

+ +

Condition: If ':botname' is not connected to TeamSpeak.

+ +

Code: 404 NOT FOUND

+ +Content: + + +
+{
+  "error": "Not Found",
+  "description": "The requested resource was not found"
+}
+
+ +{% endblock %} diff --git a/templates/index.htm b/templates/index.htm index 3183b52..eed31f3 100644 --- a/templates/index.htm +++ b/templates/index.htm @@ -8,6 +8,10 @@ +