Skip to content

Commit 931e548

Browse files
implement scalar
1 parent 31ff308 commit 931e548

File tree

7 files changed

+127
-53
lines changed

7 files changed

+127
-53
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ SWAGGER_API_DOCS_PATH=<API_DOCS_PATH> # Path to serve OpenAPI JSON (de
7575
SWAGGER_REDOC_ENABLED=<TRUE/FALSE> # Enable or disable ReDoc UI
7676
SWAGGER_REDOC_PATH=<REDOC_PATH> # Path to access ReDoc UI (e.g., /redoc)
7777

78+
# Settings for your Scalar UI
79+
SCALAR_ENABLED=<TRUE/FALSE> # Enable or disable ReDoc UI
80+
SCALAR_PATH=<SCALAR_PATH> # Path to access ReDoc UI (e.g., /redoc)
81+
7882
# Settings for your Swagger UI
7983
SWAGGER_UI_ENABLED=<TRUE/FALSE> # Enable or disable Swagger UI
8084
SWAGGER_UI_PATH=<SWAGGER_UI_PATH> # Swagger UI access path (e.g., /)

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@
151151
<artifactId>spring-boot-starter-thymeleaf</artifactId>
152152
</dependency>
153153

154-
<dependency>
155-
<groupId>org.springdoc</groupId>
156-
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
157-
<version>2.8.13</version>
158-
</dependency>
159-
160154
<dependency>
161155
<groupId>io.github.cdimascio</groupId>
162156
<artifactId>dotenv-java</artifactId>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package smartpot.com.api.Documentation;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.server.ResponseStatusException;
9+
10+
@Controller
11+
public class DocumentController {
12+
@Value("${application.title}")
13+
private String title;
14+
15+
@Value("${application.description}")
16+
private String description;
17+
18+
@Value("${application.author}")
19+
private String author;
20+
21+
@Value("${application.version}")
22+
private String version;
23+
24+
@Value("${springdoc.api-docs.path}")
25+
private String specUrl;
26+
27+
@Value("${springdoc.redoc.enabled}")
28+
private boolean redocEnabled;
29+
30+
@Value("${springdoc.scalar.enabled}")
31+
private boolean scalarEnabled;
32+
33+
@GetMapping("${springdoc.redoc.path}")
34+
public String redoc(Model model) {
35+
if (!redocEnabled) {
36+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "ReDoc está deshabilitado");
37+
}
38+
39+
model.addAttribute("specUrl", specUrl);
40+
model.addAttribute("title", title);
41+
model.addAttribute("description", description);
42+
model.addAttribute("version", version);
43+
return "redoc";
44+
}
45+
46+
@GetMapping("${springdoc.scalar.path}")
47+
public String scalar(Model model) {
48+
if (!scalarEnabled) {
49+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "ReDoc está deshabilitado");
50+
}
51+
52+
model.addAttribute("specUrl", specUrl);
53+
model.addAttribute("title", title);
54+
model.addAttribute("description", description);
55+
model.addAttribute("version", version);
56+
return "scalar";
57+
}
58+
}
59+
60+

src/main/java/smartpot/com/api/Documentation/RedocController.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/resources/application.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ springdoc:
132132
# Ruta para acceder a la documentación en formato ReDoc
133133
path: ${SWAGGER_REDOC_PATH:/redoc}
134134

135+
scalar:
136+
enabled: ${SCALAR_ENABLED:true}
137+
path: ${SCALAR_PATH:/scalar}
138+
135139
swagger-ui:
136140
# Profundidad de expansión de los modelos
137141
default-model-expand-depth: ${SWAGGER_UI_DEFAULT_MODEL_EXPAND_DEPTH:1}

src/main/resources/templates/redoc.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@
44
<meta charset="UTF-8">
55
<title th:text="${title}">SmartPot API Docs</title>
66
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script>
7-
<style>
8-
body {
9-
margin: 0;
10-
padding: 0;
11-
background-color: #121212;
12-
}
13-
#redoc-container {
14-
height: 100vh;
15-
}
16-
</style>
177
</head>
188
<body>
199
<div id="redoc-container"></div>
2010

2111
<script th:inline="javascript">
22-
const specUrl = /*[[${specUrl}]]*/ '/v3/api-docs';
12+
const specUrl = [[${specUrl}]];
2313
Redoc.init(specUrl, {
2414
scrollYOffset: 20,
2515
hideDownloadButton: false,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE html>
2+
<html lang="es" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
6+
<title th:text="${title} ?: 'API Documentation (Scalar)'">API Docs</title>
7+
8+
<!-- Scalar API Reference -->
9+
<script id="scalar-script"
10+
src="https://cdn.jsdelivr.net/npm/@scalar/api-reference@latest/dist/browser/standalone.min.js"></script>
11+
12+
<style>
13+
body {
14+
margin: 0;
15+
padding: 0;
16+
font-family: system-ui, sans-serif;
17+
background-color: #f7f9fb;
18+
}
19+
20+
.scalar-header {
21+
padding: 1rem 2rem;
22+
background: linear-gradient(90deg, #005c97, #363795);
23+
color: white;
24+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);
25+
}
26+
27+
.scalar-header h1 {
28+
margin: 0;
29+
font-size: 1.6rem;
30+
}
31+
32+
.scalar-header p {
33+
margin: 0.2rem 0 0;
34+
opacity: 0.9;
35+
}
36+
37+
#api-reference {
38+
height: calc(100vh - 90px);
39+
}
40+
</style>
41+
</head>
42+
<body>
43+
<div id="api-reference"></div>
44+
<script th:inline="javascript">
45+
const specUrl = [[${specUrl}]];
46+
document.addEventListener("DOMContentLoaded", function () {
47+
ScalarApiReference({
48+
element: "#api-reference",
49+
spec: {url: specUrl || "/v3/api-docs"},
50+
theme: window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light',
51+
layout: "modern",
52+
hideDownloadButton: false,
53+
hideModels: false
54+
});
55+
});
56+
</script>
57+
</body>
58+
</html>

0 commit comments

Comments
 (0)