Skip to content

Commit d3f37f8

Browse files
committed
Add API key check endpoint and update Swagger documentation
1 parent 1cb6566 commit d3f37f8

5 files changed

Lines changed: 107 additions & 4 deletions

File tree

controllers/apikey.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/algohive/beeapi/services"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
// CheckApiKey godoc
11+
// @Summary Check API key
12+
// @Description Returns the current API key
13+
// @Tags API Key
14+
// @Produce json
15+
// @Success 200 {object} map[string]string
16+
// CheckApiKey handles the API key check
17+
// @Router /apikey [get]
18+
// @Security Bearer
19+
func CheckApiKey(c * gin.Context) {
20+
apiKeyManager, err := services.NewAPIKeyManager(".")
21+
if err != nil {
22+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to initialize API key manager"})
23+
return
24+
}
25+
26+
c.JSON(http.StatusOK, gin.H{"api_key": apiKeyManager.GetAPIKey()})
27+
}

docs/docs.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@ const docTemplate = `{
2222
"host": "{{.Host}}",
2323
"basePath": "{{.BasePath}}",
2424
"paths": {
25+
"/apikey": {
26+
"get": {
27+
"security": [
28+
{
29+
"Bearer": []
30+
}
31+
],
32+
"description": "Returns the current API key",
33+
"produces": [
34+
"application/json"
35+
],
36+
"tags": [
37+
"API Key"
38+
],
39+
"summary": "Check API key",
40+
"responses": {
41+
"200": {
42+
"description": "OK",
43+
"schema": {
44+
"type": "object",
45+
"additionalProperties": {
46+
"type": "string"
47+
}
48+
}
49+
}
50+
}
51+
}
52+
},
2553
"/name": {
2654
"get": {
2755
"description": "Returns the name of the server",
@@ -647,7 +675,7 @@ const docTemplate = `{
647675
"Bearer": {
648676
"type": "apiKey",
649677
"name": "Authorization",
650-
"in": "header"
678+
"in": "Bearer"
651679
}
652680
}
653681
}`

docs/swagger.json

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@
1616
"host": "localhost:5000",
1717
"basePath": "/",
1818
"paths": {
19+
"/apikey": {
20+
"get": {
21+
"security": [
22+
{
23+
"Bearer": []
24+
}
25+
],
26+
"description": "Returns the current API key",
27+
"produces": [
28+
"application/json"
29+
],
30+
"tags": [
31+
"API Key"
32+
],
33+
"summary": "Check API key",
34+
"responses": {
35+
"200": {
36+
"description": "OK",
37+
"schema": {
38+
"type": "object",
39+
"additionalProperties": {
40+
"type": "string"
41+
}
42+
}
43+
}
44+
}
45+
}
46+
},
1947
"/name": {
2048
"get": {
2149
"description": "Returns the name of the server",
@@ -641,7 +669,7 @@
641669
"Bearer": {
642670
"type": "apiKey",
643671
"name": "Authorization",
644-
"in": "header"
672+
"in": "Bearer"
645673
}
646674
}
647675
}

docs/swagger.yaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ info:
5050
title: BeeAPI Go
5151
version: "1.0"
5252
paths:
53+
/apikey:
54+
get:
55+
description: Returns the current API key
56+
produces:
57+
- application/json
58+
responses:
59+
"200":
60+
description: OK
61+
schema:
62+
additionalProperties:
63+
type: string
64+
type: object
65+
security:
66+
- Bearer: []
67+
summary: Check API key
68+
tags:
69+
- API Key
5370
/name:
5471
get:
5572
description: Returns the name of the server
@@ -417,7 +434,7 @@ paths:
417434
- Themes
418435
securityDefinitions:
419436
Bearer:
420-
in: header
437+
in: Bearer
421438
name: Authorization
422439
type: apiKey
423440
swagger: "2.0"

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
// @host localhost:5000
3232
// @BasePath /
3333
// @securityDefinitions.apikey Bearer
34-
// @in header
34+
// @in Bearer
3535
// @name Authorization
3636
func main() {
3737
// Load environment variables from .env file if it exists
@@ -87,6 +87,9 @@ func main() {
8787
protected := router.Group("")
8888
protected.Use(middlewares.RequireAPIKey(apiKeyManager))
8989
{
90+
// Check API key
91+
protected.GET("/apikey", controllers.CheckApiKey)
92+
9093
// Theme management
9194
protected.POST("/theme", themeController.CreateTheme)
9295
protected.DELETE("/theme", themeController.DeleteTheme)

0 commit comments

Comments
 (0)