Skip to content

Commit e5694c4

Browse files
authored
Merge pull request #151 from Team-VoW/feat/swagger
feat(swagger): added swagger-php to generate OpenAPI documentation and Swagger ui in dev runs to view this.
2 parents 9815063 + 62b202b commit e5694c4

File tree

19 files changed

+1150
-105
lines changed

19 files changed

+1150
-105
lines changed

.htaccess

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# CORS Headers - Allow requests from Swagger UI only
2+
<IfModule mod_headers.c>
3+
# Handle preflight OPTIONS requests
4+
RewriteCond %{REQUEST_METHOD} OPTIONS
5+
RewriteRule ^(.*)$ $1 [R=200,L]
6+
7+
# Set CORS headers only for allowed origins
8+
SetEnvIf Origin "^http://localhost:8090$" CORS_ALLOW_ORIGIN=$0
9+
Header always set Access-Control-Allow-Origin "%{CORS_ALLOW_ORIGIN}e" env=CORS_ALLOW_ORIGIN
10+
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS, HEAD" env=CORS_ALLOW_ORIGIN
11+
Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" env=CORS_ALLOW_ORIGIN
12+
Header always set Access-Control-Max-Age "3600" env=CORS_ALLOW_ORIGIN
13+
Header always set Access-Control-Allow-Credentials "true" env=CORS_ALLOW_ORIGIN
14+
</IfModule>
15+
116
Options -Indexes
217

318
RewriteEngine On

Controllers/Api/ApiController.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44

55
use VoicesOfWynn\Controllers\Controller;
66
use VoicesOfWynn\Controllers\Api\ApiKey;
7+
use OpenApi\Attributes as OA;
78

8-
/**
9-
* Base class for all API controllers
10-
*/
9+
#[OA\Info(
10+
title: "Voices of Wynn API",
11+
version: "1.0.0",
12+
description: "API for the Voices of Wynn website and mod."
13+
)]
14+
15+
#[OA\Schema(
16+
schema: "Error",
17+
description: "Standard error response",
18+
properties: [
19+
new OA\Property(property: "error", type: "string", description: "Error message"),
20+
new OA\Property(property: "code", type: "integer", description: "HTTP status code")
21+
]
22+
)]
1123
abstract class ApiController extends Controller
1224
{
1325

26+
1427
/**
1528
* Controller constructor enabling output buffering and setting the Content-Type header
1629
* Since specific controllers don't have a constructor, this will be invoked every time a new constructor is

Controllers/Api/BootupActions/ModBootupLogger.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22

33
namespace VoicesOfWynn\Controllers\Api\BootupActions;
44

5+
use OpenApi\Attributes as OA;
56
use VoicesOfWynn\Controllers\Api\ApiController;
67
use VoicesOfWynn\Models\Api\FunFacts\FunFactGenerator;
78
use VoicesOfWynn\Models\Api\MessageBroadcast\BroadcastLoader;
89
use VoicesOfWynn\Models\Api\UsageAnalysis\BootupLogger;
910
use VoicesOfWynn\Models\Api\VersionChecker\VersionChecker;
1011

12+
#[OA\Tag(name: "Bootup Actions", description: "Endpoints for mod bootup.")]
1113
class ModBootupLogger extends ApiController
1214
{
1315

14-
/**
15-
* @inheritDoc
16-
*/
16+
#[OA\Get(
17+
path: "/api/version/check",
18+
summary: "Check for new version",
19+
tags: ["Bootup Actions"]
20+
)]
21+
#[OA\Parameter(name: "id", in: "query", required: true, schema: new OA\Schema(type: "string"))]
22+
#[OA\Response(
23+
response: 200,
24+
description: "Success"
25+
)]
26+
#[OA\Response(response: 400, description: "Bad request")]
27+
#[OA\Response(response: 500, description: "Internal server error")]
1728
public function process(array $args): int
1829
{
1930
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {

Controllers/Api/DiscordIntegration/DiscordIntegration.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use VoicesOfWynn\Models\Api\DiscordIntegration\DiscordManager;
88
use VoicesOfWynn\Models\Website\DiscordRole;
99
use VoicesOfWynn\Models\Website\UserException;
10+
use OpenApi\Attributes as OA;
1011

12+
#[OA\Tag(name: "Discord Integration", description: "Endpoints for integrating with the Voices of Wynn Discord server.")]
1113
class DiscordIntegration extends ApiController
1214
{
1315
public function process(array $args): int
@@ -22,6 +24,21 @@ public function process(array $args): int
2224
}
2325
}
2426

27+
#[OA\Get(
28+
path: "/api/discord-integration",
29+
summary: "Get Discord integration data",
30+
tags: ["Discord Integration"],
31+
parameters: [
32+
new OA\Parameter(name: "apiKey", in: "query", required: true, schema: new OA\Schema(type: "string", default: "testing")),
33+
new OA\Parameter(name: "action", in: "query", required: true, schema: new OA\Schema(type: "string", enum: ["getAllUsers"]))
34+
],
35+
responses: [
36+
new OA\Response(response: 200, description: "Success", content: new OA\JsonContent(type: "array", items: new OA\Items(ref: "#/components/schemas/User"))),
37+
new OA\Response(response: 400, description: "Bad request - invalid action"),
38+
new OA\Response(response: 401, description: "Unauthorized - invalid API key"),
39+
new OA\Response(response: 500, description: "Internal server error", content: new OA\JsonContent(properties: [new OA\Property(property: "error", type: "string", description: "Error message")]))
40+
]
41+
)]
2542
/**
2643
* @throws UserException
2744
*/
@@ -35,13 +52,49 @@ private function get(): int
3552
switch ($_GET['action']) {
3653
case 'getAllUsers':
3754
$users = $manager->getAllUsers();
55+
$decoded = json_decode($users, true);
56+
57+
// Check if the response contains an error
58+
if (isset($decoded['error'])) {
59+
echo $users;
60+
return 500;
61+
}
62+
3863
echo $users;
3964
return 200;
4065
default:
4166
return 400;
4267
}
4368
}
4469

70+
#[OA\Post(
71+
path: "/api/discord-integration",
72+
summary: "Synchronize a user",
73+
tags: ["Discord Integration"],
74+
requestBody: new OA\RequestBody(
75+
required: true,
76+
content: new OA\MediaType(
77+
mediaType: "application/x-www-form-urlencoded",
78+
schema: new OA\Schema(
79+
required: ["apiKey", "action", "discordId", "discordName"],
80+
properties: [
81+
new OA\Property(property: "apiKey", type: "string", default: "testing"),
82+
new OA\Property(property: "action", type: "string", enum: ["syncUser"]),
83+
new OA\Property(property: "discordId", type: "integer"),
84+
new OA\Property(property: "discordName", type: "string"),
85+
new OA\Property(property: "imgurl", type: "string"),
86+
new OA\Property(property: "name", type: "string"),
87+
new OA\Property(property: "roles", type: "string")
88+
]
89+
)
90+
)
91+
),
92+
responses: [
93+
new OA\Response(response: 200, description: "User updated"),
94+
new OA\Response(response: 201, description: "User created"),
95+
new OA\Response(response: 401, description: "Unauthorized")
96+
]
97+
)]
4598
/**
4699
* @throws UserException
47100
*/

0 commit comments

Comments
 (0)