@@ -7,32 +7,32 @@ import (
7
7
"github.com/getkin/kin-openapi/openapi3"
8
8
)
9
9
10
- // isReadOnlyEndpoint checks if an endpoint is marked as read-only in the OpenAPI spec
10
+ // isReadOnlyEndpoint checks if an endpoint is marked as read-only in the OpenAPI spec.
11
11
func isReadOnlyEndpoint (operation * openapi3.Operation ) bool {
12
- // Try to get the value directly from the map first
12
+ // Try to get the value directly from the map first.
13
13
if val , exists := operation .Extensions ["x-read-only" ]; exists {
14
- // Handle json.RawMessage case
14
+ // Handle json.RawMessage case.
15
15
if rawMsg , ok := val .(json.RawMessage ); ok {
16
16
return string (rawMsg ) == "true"
17
17
}
18
18
19
- // Handle direct boolean case
19
+ // Handle direct boolean case.
20
20
if boolVal , ok := val .(bool ); ok {
21
21
return boolVal
22
22
}
23
23
}
24
24
return false
25
25
}
26
26
27
- // shouldEnableEndpoint determines if an endpoint should be accessible based on its type and configuration
27
+ // shouldEnableEndpoint determines if an endpoint should be accessible based on its type and configuration.
28
28
func shouldEnableEndpoint (operation * openapi3.Operation , enableWriteOperations bool ) bool {
29
29
if isReadOnlyEndpoint (operation ) {
30
30
return true
31
31
}
32
32
return enableWriteOperations
33
33
}
34
34
35
- // findOperation looks up the OpenAPI operation for the given path and method
35
+ // findOperation looks up the OpenAPI operation for the given path and method.
36
36
func findOperation (spec * openapi3.T , path string , method string ) * openapi3.Operation {
37
37
pathItem := spec .Paths .Find (path )
38
38
if pathItem == nil {
@@ -53,31 +53,31 @@ func findOperation(spec *openapi3.T, path string, method string) *openapi3.Opera
53
53
}
54
54
}
55
55
56
- // ConfigMiddleware creates a middleware that controls endpoint access based on configuration
56
+ // ConfigMiddleware creates a middleware that controls endpoint access based on configuration.
57
57
func ConfigMiddleware (enableWriteOperations bool ) MiddlewareFunc {
58
58
return ConfigMiddlewareWithSpec (enableWriteOperations , GetSwagger )
59
59
}
60
60
61
- // ConfigMiddlewareWithSpec creates a middleware that controls endpoint access based on configuration
62
- // This accepts a function to get the spec, making it more testable
61
+ // ConfigMiddlewareWithSpec creates a middleware that controls endpoint access based on configuration.
62
+ // This accepts a function to get the spec, making it more testable.
63
63
func ConfigMiddlewareWithSpec (enableWriteOperations bool , getSpec func () (* openapi3.T , error )) MiddlewareFunc {
64
64
return func (next http.Handler ) http.Handler {
65
65
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
66
- // Load the OpenAPI specification
66
+ // Load the OpenAPI specification.
67
67
spec , err := getSpec ()
68
68
if err != nil {
69
69
http .Error (w , "Internal server error" , http .StatusInternalServerError )
70
70
return
71
71
}
72
72
73
- // Find the operation for this request
73
+ // Find the operation for this request.
74
74
operation := findOperation (spec , r .URL .Path , r .Method )
75
75
if operation == nil {
76
76
http .Error (w , "Endpoint not found" , http .StatusNotFound )
77
77
return
78
78
}
79
79
80
- // Check if the endpoint should be accessible
80
+ // Check if the endpoint should be accessible.
81
81
if ! shouldEnableEndpoint (operation , enableWriteOperations ) {
82
82
http .Error (w , "Endpoint not enabled" , http .StatusForbidden )
83
83
return
0 commit comments