From 22e80893f3892ac83292c0566a8b9fe226767f51 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Fri, 24 May 2024 15:53:11 +0200 Subject: [PATCH] swagger --- main.go | 7 +- openapi/index.html | 25 ++++ openapi/openapi.json | 273 +++++++++++++++++++++++++++++++++++++++++++ server/server.go | 6 +- 4 files changed, 308 insertions(+), 3 deletions(-) create mode 100644 openapi/index.html create mode 100644 openapi/openapi.json diff --git a/main.go b/main.go index 8e68425..dc5f19a 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,9 @@ var ( //go:embed frontend/dist/index.html //go:embed frontend/dist/assets/* frontend embed.FS + + //go:embed openapi/* + swagger embed.FS ) func init() { @@ -61,7 +64,6 @@ func init() { func main() { frontend, err := fs.Sub(frontend, "frontend/dist") - if err != nil { log.Fatalln(err) } @@ -92,9 +94,10 @@ func main() { server.RunBlocking(&server.RunConfig{ Host: c.Host, Port: c.Port, - App: frontend, DBPath: localDatabasePath, FileLogging: enableFileLogging, LogFile: logFile, + App: frontend, + Swagger: swagger, }) } diff --git a/openapi/index.html b/openapi/index.html new file mode 100644 index 0000000..9f0ee5a --- /dev/null +++ b/openapi/index.html @@ -0,0 +1,25 @@ + + + + + + + + SwaggerUI + + + + +
+ + + + + \ No newline at end of file diff --git a/openapi/openapi.json b/openapi/openapi.json new file mode 100644 index 0000000..b6b823e --- /dev/null +++ b/openapi/openapi.json @@ -0,0 +1,273 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Swagger yt-dlp-webui - OpenAPI 3.1", + "description": "yt-dlp-webui api based on the OpenAPI 3.1 specification. You can find out more about\nSwagger at [https://swagger.io](https://swagger.io). \n\nSome useful links:\n- [yt-dlp-webui repository](https://github.com/marcopeocchi/yt-dlp-web-ui)", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "email": "apiteam@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0.11" + }, + "externalDocs": { + "description": "Find out more about Swagger", + "url": "http://swagger.io" + }, + "servers": [ + { + "url": "/api/v1" + } + ], + "tags": [ + { + "name": "download", + "description": "Everything about your Pets", + "externalDocs": { + "description": "Find out more", + "url": "https://github.com/marcopeocchi/yt-dlp-web-ui" + } + } + ], + "paths": { + "/exec": { + "post": { + "tags": [ + "download" + ], + "summary": "Add a new download in the pending state ready to be processed", + "description": "Add a new download in the pending state ready to be processed", + "operationId": "addDownload", + "requestBody": { + "description": "Create a new download", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DownloadRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful operation", + "content": { + "application/json": { + "schema": { + "type": "string", + "description": "Process uuid" + } + } + } + }, + "400": { + "description": "Invalid input" + }, + "422": { + "description": "Validation exception" + } + }, + "security": [ + { + "api_key": [ + "write:download", + "read:download" + ] + } + ] + } + }, + "/running": { + "get": { + "tags": [ + "download" + ], + "summary": "Returns all running and pending process", + "description": "Returns all running and pending process", + "operationId": "running", + "responses": { + "200": { + "description": "Successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProcessResponse" + } + } + } + }, + "400": { + "description": "Invalid input" + }, + "422": { + "description": "Validation exception" + } + }, + "security": [ + { + "api_key": [ + "write:download", + "read:download" + ] + } + ] + } + } + }, + "components": { + "schemas": { + "DownloadRequest": { + "type": "object", + "properties": { + "url": { + "type": "string", + "examples": [ + "https://..." + ] + }, + "params": { + "type": "array", + "format": "string", + "examples": [ + "-N", + "4", + "-R", + "infinite" + ] + } + } + }, + "DownloadResponse": { + "type": "object", + "properties": { + "url": { + "type": "string", + "examples": [ + "https://..." + ] + }, + "params": { + "type": "array", + "format": "string", + "examples": [ + "-N", + "4", + "-R", + "infinite" + ] + } + } + }, + "DownloadProgress": { + "type": "object", + "properties": { + "process_status": { + "type": "integer", + "examples": [ + 0, + 1, + 2, + 3 + ] + }, + "percentage": { + "type": "string", + "examples": [ + "50%" + ] + }, + "speed": { + "type": "integer", + "examples": [ + 7289347 + ] + }, + "eta": { + "type": "integer", + "examples": [ + 3600 + ] + } + } + }, + "DownloadInfo": { + "type": "object", + "properties": { + "url": { + "type": "string" + }, + "title": { + "type": "string" + }, + "thumbnail": { + "type": "string" + }, + "resolution": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "vcodec": { + "type": "string" + }, + "acodec": { + "type": "string" + }, + "extension": { + "type": "string" + }, + "original_url": { + "type": "string" + }, + "created_at": { + "type": "object" + } + } + }, + "DownloadOutput": { + "type": "object", + "properties": { + "path": { + "type": "string" + }, + "filename": { + "type": "string" + }, + "saveFilePath": { + "type": "string" + } + } + }, + "ProcessResponse": { + "type": "object", + "properties": { + "progress": { + "$ref": "#/components/schemas/DownloadProgress" + }, + "info": { + "$ref": "#/components/schemas/DownloadInfo" + }, + "output": { + "$ref": "#/components/schemas/DownloadOutput" + }, + "params": { + "type": "array", + "format": "string" + } + } + } + }, + "securitySchemes": { + "api_key": { + "type": "apiKey", + "name": "api_key", + "in": "header" + } + } + } +} \ No newline at end of file diff --git a/server/server.go b/server/server.go index 23b0c20..06f8702 100644 --- a/server/server.go +++ b/server/server.go @@ -33,14 +33,16 @@ import ( type RunConfig struct { Host string Port int - App fs.FS DBPath string LogFile string FileLogging bool + App fs.FS + Swagger fs.FS } type serverConfig struct { frontend fs.FS + swagger fs.FS logger *slog.Logger host string port int @@ -96,6 +98,7 @@ func RunBlocking(cfg *RunConfig) { srv := newServer(serverConfig{ frontend: cfg.App, + swagger: cfg.Swagger, logger: logger, host: cfg.Host, port: cfg.Port, @@ -155,6 +158,7 @@ func newServer(c serverConfig) *http.Server { // r.Use(middleware.Logger) r.Mount("/", http.FileServer(http.FS(c.frontend))) + r.Mount("/openapi", http.FileServer(http.FS(c.swagger))) // Archive routes r.Route("/archive", func(r chi.Router) {