added swagger

main
alex 2023-09-10 23:05:16 +02:00
parent 7bae66c3aa
commit cb5ddf10f9
24 changed files with 413 additions and 2 deletions

14
main.go
View File

@ -1,3 +1,17 @@
// Package classification JNX Log Manager API Documentation.
//
// Schemes: https
// Host: jannex
// BasePath: /v1
// Version: 1.0.0
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// swagger:meta
package main
import (

View File

@ -1,15 +1,16 @@
package structs
// swagger:model LogBody
type LogBody struct {
Type string
Logs []string
}
// /log/grouptasks?d=2021-08-01&f=i
type GetLogParams struct {
Type string
}
// /log/grouptasks?d=2021-08-01&f=i
type GetLogQuery struct {
D string // date
F string // filter

BIN
public/swagger/favicon-16x16.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 665 B

BIN
public/swagger/favicon-32x32.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 628 B

59
public/swagger/index.css Executable file
View File

@ -0,0 +1,59 @@
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 0;
background: #fafafa;
}
/* Position the navbar container inside the image */
.container {
position: absolute;
width: auto;
font-family: Arial, Helvetica, sans-serif;
margin: 65px 5px;
}
#container-versions {
margin: 110px 5px;
}
/* The navbar */
.topnav {
overflow: hidden;
background-color: #333;
border-radius: 10px;
}
.topnav:first-child {
margin-bottom: 10px;
}
/* Navbar links */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 10px 12px;
text-decoration: none;
font-size: 14px;
}
.topnav a:hover {
opacity: 0.7;
border-bottom: 5px solid #62a03f;
}
.topnav a.active {
color: #999494;
border-bottom: 5px solid #62a03f;
}

19
public/swagger/index.html Executable file
View File

@ -0,0 +1,19 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
<link rel="stylesheet" type="text/css" href="index.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
</body>
</html>

View File

@ -0,0 +1,79 @@
<!doctype html>
<html lang="en-US">
<head>
<title>Swagger UI: OAuth2 Redirect</title>
</head>
<body>
<script>
'use strict';
function run () {
var oauth2 = window.opener.swaggerUIRedirectOauth2;
var sentState = oauth2.state;
var redirectUrl = oauth2.redirectUrl;
var isValid, qp, arr;
if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1).replace('?', '&');
} else {
qp = location.search.substring(1);
}
arr = qp.split("&");
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';});
qp = qp ? JSON.parse('{' + arr.join() + '}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value);
}
) : {};
isValid = qp.state === sentState;
if ((
oauth2.auth.schema.get("flow") === "accessCode" ||
oauth2.auth.schema.get("flow") === "authorizationCode" ||
oauth2.auth.schema.get("flow") === "authorization_code"
) && !oauth2.auth.code) {
if (!isValid) {
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "warning",
message: "Authorization may be unsafe, passed state was changed in server. The passed state wasn't returned from auth server."
});
}
if (qp.code) {
delete oauth2.state;
oauth2.auth.code = qp.code;
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
} else {
let oauthErrorMsg;
if (qp.error) {
oauthErrorMsg = "["+qp.error+"]: " +
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
(qp.error_uri ? "More info: "+qp.error_uri : "");
}
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server."
});
}
} else {
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
}
window.close();
}
if (document.readyState !== 'loading') {
run();
} else {
document.addEventListener('DOMContentLoaded', function () {
run();
});
}
</script>
</body>
</html>

View File

@ -0,0 +1,11 @@
window.onload = function() {
window.ui = SwaggerUIBundle({
url: 'swagger.json',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout",
});
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

3
public/swagger/swagger-ui.css Executable file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
public/swagger/swagger-ui.js Executable file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

138
public/swagger/swagger.json Normal file
View File

@ -0,0 +1,138 @@
{
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"schemes": [
"https"
],
"swagger": "2.0",
"info": {
"title": "JNX Log Manager API Documentation.",
"version": "1.0.0"
},
"host": "jannex",
"basePath": "/v1",
"paths": {
"/v1/log": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"log"
],
"summary": "Add a log to the log file. You can specify multiple logs at once",
"operationId": "addLog",
"parameters": [
{
"description": "The log to add",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/LogBody"
}
}
],
"responses": {
"200": {
"description": "Successfully added log"
},
"400": {
"description": "Invalid request body"
}
}
}
},
"/v1/log/types": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"log"
],
"summary": "Get the available log types",
"operationId": "getAvailableLogTypes",
"responses": {
"200": {
"description": "Successfully got log types. Returns an array of strings"
}
}
}
},
"/v1/logs/{type}": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"logs"
],
"summary": "Get the log file for the specified type",
"operationId": "getLog",
"parameters": [
{
"type": "string",
"description": "The type of log to get",
"name": "type",
"in": "path",
"required": true
},
{
"type": "string",
"description": "The date of the log to get",
"name": "d",
"in": "query"
},
{
"type": "string",
"description": "The filter to apply to the log",
"name": "f",
"in": "query"
}
],
"responses": {
"200": {
"description": "Successfully got log. Returns an array of strings"
},
"400": {
"description": "Invalid request body"
},
"422": {
"description": "No log file found for the specified type or date"
}
}
}
}
},
"definitions": {
"LogBody": {
"type": "object",
"properties": {
"Logs": {
"type": "array",
"items": {
"type": "string"
}
},
"Type": {
"type": "string"
}
},
"x-go-package": "jannex/log-manager/modules/structs"
}
}
}

View File

@ -10,6 +10,26 @@ import (
)
func AddLog(c *fiber.Ctx) error {
// swagger:operation POST /v1/log log addLog
// ---
// summary: Add a log to the log file. You can specify multiple logs at once
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: body
// in: body
// description: The log to add
// required: true
// schema:
// "$ref": "#/definitions/LogBody"
// responses:
// '200':
// description: Successfully added log
// '400':
// description: Invalid request body
var body structs.LogBody
if err := utils.BodyParserHelper(c, &body); err != nil {
@ -22,6 +42,37 @@ func AddLog(c *fiber.Ctx) error {
}
func GetLog(c *fiber.Ctx) error {
// swagger:operation GET /v1/logs/{type} logs getLog
// ---
// summary: Get the log file for the specified type
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: type
// in: path
// description: The type of log to get
// required: true
// type: string
// - name: d
// in: query
// description: The date of the log to get
// required: false
// type: string
// - name: f
// in: query
// description: The filter to apply to the log
// required: false
// type: string
// responses:
// '200':
// description: Successfully got log. Returns an array of strings
// '400':
// description: Invalid request body
// '422':
// description: No log file found for the specified type or date
var params structs.GetLogParams
if err := utils.ParamsParserHelper(c, &params); err != nil {
@ -59,10 +110,20 @@ func GetLog(c *fiber.Ctx) error {
}
return c.JSON(logs)
}
func GetAvailableLogTypes(c *fiber.Ctx) error {
// swagger:operation GET /v1/log/types log getAvailableLogTypes
// ---
// summary: Get the available log types
// consumes:
// - application/json
// produces:
// - application/json
// responses:
// '200':
// description: Successfully got log types. Returns an array of strings
logTypes := loghandler.GetAvailableLogTypes()
return c.JSON(logTypes)

View File

@ -15,4 +15,6 @@ func SetupRoutes(app *fiber.App) {
ls := v1.Group("/logs")
ls.Get("/:type", log.GetLog)
app.Static("/", "./public/")
}

4
swagger-gen.sh Executable file
View File

@ -0,0 +1,4 @@
# installing swagger - version v0.30.5 - installed at 26.08.2023
# go install github.com/go-swagger/go-swagger/cmd/swagger@latest
~/go/bin/swagger generate spec -o ./public/swagger/swagger.json --scan-models