Skip to content

Commit ca3b947

Browse files
Swagger Json Automation (#64)
* chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * chore(swagger): update github swagger workflow * fix(swagger): healt-retries decrese to 5 * chore(swagger): add 104 properties, swagger profile and update swagger workflow * chore(swagger): add verson for swagger doc and fix jwt issue * chore(swagger): add mysqldialect on swagger properties * fix(swagger): removed application-swagger.properties
1 parent dde6f4d commit ca3b947

15 files changed

Lines changed: 172 additions & 9 deletions

File tree

.github/workflows/swagger-json.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Sync Swagger to AMRIT-Docs
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
jobs:
9+
swagger-sync:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout API repo
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Java 17
17+
uses: actions/setup-java@v4
18+
with:
19+
distribution: temurin
20+
java-version: 17
21+
cache: maven
22+
23+
- name: Build API (skip tests)
24+
run: mvn clean package -DENV_VAR=swagger -DskipTests -Dcheckstyle.skip=true
25+
26+
- name: Install jq
27+
run: sudo apt-get update && sudo apt-get install -y jq
28+
29+
- name: Run API in swagger profile
30+
run: |
31+
mvn spring-boot:run \
32+
-DENV_VAR=swagger \
33+
-Dspring-boot.run.profiles=swagger \
34+
-Dcheckstyle.skip=true \
35+
-Dmaven.test.skip=true \
36+
-Dspring-boot.run.arguments="--server.port=9090" \
37+
> app.log 2>&1 &
38+
echo $! > api_pid.txt
39+
40+
- name: Wait for API & fetch Swagger
41+
run: |
42+
for i in {1..30}; do
43+
CODE=$(curl --connect-timeout 2 --max-time 5 -s -o swagger_raw.json -w "%{http_code}" http://localhost:9090/v3/api-docs || true)
44+
if [ "$CODE" = "200" ]; then
45+
if jq . swagger_raw.json > helpline104-api.json; then
46+
echo "Swagger generated successfully"
47+
exit 0
48+
else
49+
echo "Failed to parse swagger_raw.json with jq"
50+
exit 1
51+
fi
52+
fi
53+
echo "Waiting for API... ($i)"
54+
sleep 5
55+
done
56+
57+
echo "Swagger not generated"
58+
cat app.log || true
59+
exit 1
60+
61+
- name: Stop API
62+
if: always()
63+
run: |
64+
# Graceful shutdown of the process group
65+
sleep 5
66+
# Force kill the process group if still running
67+
if [ -f api_pid.txt ]; then
68+
PID=$(cat api_pid.txt)
69+
kill -TERM -- -"$PID" 2>/dev/null || true
70+
sleep 2
71+
kill -9 -- -"$PID" 2>/dev/null || true
72+
fi
73+
# Fallback: kill any remaining java process on port 9090
74+
fuser -k 9090/tcp 2>/dev/null || true
75+
76+
- name: Checkout AMRIT-Docs
77+
uses: actions/checkout@v4
78+
with:
79+
repository: PSMRI/AMRIT-Docs
80+
token: ${{ secrets.DOCS_REPO_TOKEN }}
81+
path: amrit-docs
82+
83+
- name: Copy Swagger JSON
84+
run: |
85+
mkdir -p amrit-docs/docs/swagger
86+
cp helpline104-api.json amrit-docs/docs/swagger/helpline104-api.json
87+
88+
- name: Create Pull Request
89+
uses: peter-evans/create-pull-request@v8
90+
with:
91+
token: ${{ secrets.DOCS_REPO_TOKEN }}
92+
path: amrit-docs
93+
branch: auto/swagger-update-helpline104-api
94+
base: main
95+
commit-message: Auto-update Helpline104-API swagger
96+
title: Auto-update Helpline104-API swagger
97+
delete-branch: true
98+
body: |
99+
This PR automatically updates the Helpline104-API Swagger JSON
100+
from the latest main branch build.

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@
249249
<version>0.12.6</version>
250250
<scope>runtime</scope>
251251
</dependency>
252+
<dependency>
253+
<groupId>com.h2database</groupId>
254+
<artifactId>h2</artifactId>
255+
<scope>runtime</scope>
256+
</dependency>
252257

253258

254259
</dependencies>

src/main/java/com/iemr/helpline104/App.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.builder.SpringApplicationBuilder;
2727
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
2828
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Profile;
2930
import org.springframework.data.redis.connection.RedisConnectionFactory;
3031
import org.springframework.data.redis.core.RedisTemplate;
3132
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@@ -60,6 +61,7 @@ public static void main(String[] args) {
6061
}
6162

6263
@Bean
64+
@Profile("!swagger")
6365
public IEMRApplBeans instantiateBeans() {
6466
return new IEMRApplBeans();
6567
}
@@ -83,6 +85,7 @@ public String hello(@PathVariable String name) {
8385
}
8486

8587
@Bean
88+
@Profile("!swagger")
8689
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
8790
RedisTemplate<String, Object> template = new RedisTemplate<>();
8891
template.setConnectionFactory(factory);

src/main/java/com/iemr/helpline104/config/InterceptorConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929

3030
import com.iemr.helpline104.utils.http.HTTPRequestInterceptor;
3131

32+
import org.springframework.context.annotation.Profile;
33+
3234
@Configuration
35+
@Profile("!swagger")
3336
public class InterceptorConfig implements WebMvcConfigurer {
3437

3538
@Autowired

src/main/java/com/iemr/helpline104/config/RedisConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import org.springframework.data.redis.core.RedisTemplate;
88
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
99
import org.springframework.data.redis.serializer.StringRedisSerializer;
10+
import org.springframework.context.annotation.Profile;
1011
import org.springframework.session.data.redis.config.ConfigureRedisAction;
1112

1213
import com.iemr.helpline104.data.users.M_User;
1314

1415
@Configuration
1516
@EnableCaching
17+
@Profile("!swagger")
1618
public class RedisConfig {
1719

1820
@Bean

src/main/java/com/iemr/helpline104/config/SwaggerConfig.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.env.Environment;
56

67
import io.swagger.v3.oas.models.Components;
78
import io.swagger.v3.oas.models.OpenAPI;
@@ -11,14 +12,23 @@
1112

1213
@Configuration
1314
public class SwaggerConfig {
14-
15-
@Bean
16-
public OpenAPI customOpenAPI() {
17-
return new OpenAPI().info(new
18-
Info().title("Helpline 104 API").version("version").description("A microservice for the creation and management of beneficiaries."))
19-
.addSecurityItem(new SecurityRequirement().addList("my security"))
20-
.components(new Components().addSecuritySchemes("my security",
21-
new SecurityScheme().name("my security").type(SecurityScheme.Type.HTTP).scheme("bearer")));
15+
private static final String DEFAULT_SERVER_URL = "http://localhost:8091";
16+
17+
@Bean
18+
public OpenAPI customOpenAPI(Environment env) {
19+
String devUrl = env.getProperty("api.dev.url", DEFAULT_SERVER_URL);
20+
String uatUrl = env.getProperty("api.uat.url", DEFAULT_SERVER_URL);
21+
String demoUrl = env.getProperty("api.demo.url", DEFAULT_SERVER_URL);
22+
return new OpenAPI()
23+
.info(new Info().title("Helpline 104 API").version("1.0.0").description("Helpline 104 API for health advisory, disease surveillance, and medical information services."))
24+
.addSecurityItem(new SecurityRequirement().addList("my security"))
25+
.components(new Components().addSecuritySchemes("my security",
26+
new SecurityScheme().name("my security").type(SecurityScheme.Type.HTTP).scheme("bearer")))
27+
.servers(java.util.Arrays.asList(
28+
new io.swagger.v3.oas.models.servers.Server().url(devUrl).description("Dev"),
29+
new io.swagger.v3.oas.models.servers.Server().url(uatUrl).description("UAT"),
30+
new io.swagger.v3.oas.models.servers.Server().url(demoUrl).description("Demo")
31+
));
2232
}
2333

2434
}

src/main/java/com/iemr/helpline104/utils/FilterConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import org.springframework.boot.web.servlet.FilterRegistrationBean;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Profile;
67
import org.springframework.core.Ordered;
78
import org.springframework.beans.factory.annotation.Value;
89

910
@Configuration
11+
@Profile("!swagger")
1012
public class FilterConfig {
1113

1214
@Value("${cors.allowed-origins}")

src/main/java/com/iemr/helpline104/utils/JwtAuthenticationUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.Profile;
910
import org.springframework.data.redis.core.RedisTemplate;
1011
import org.springframework.http.HttpStatus;
1112
import org.springframework.http.ResponseEntity;
@@ -19,6 +20,7 @@
1920
import jakarta.servlet.http.HttpServletRequest;
2021

2122
@Component
23+
@Profile("!swagger")
2224
public class JwtAuthenticationUtil {
2325

2426
@Autowired

src/main/java/com/iemr/helpline104/utils/JwtUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.context.annotation.Profile;
89
import org.springframework.stereotype.Component;
910

1011
import io.jsonwebtoken.Claims;
1112
import io.jsonwebtoken.Jwts;
1213
import io.jsonwebtoken.security.Keys;
1314

1415
@Component
16+
@Profile("!swagger")
1517
public class JwtUtil {
1618

1719
@Value("${jwt.secret}")

src/main/java/com/iemr/helpline104/utils/TokenDenylist.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
import org.slf4j.LoggerFactory;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.data.redis.core.RedisTemplate;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Profile;
79
import org.springframework.stereotype.Component;
810

911
import java.util.concurrent.TimeUnit;
1012

1113
@Component
14+
@Profile("!swagger")
1215
public class TokenDenylist {
1316
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
1417

1518
private static final String PREFIX = "denied_";
1619

1720
@Autowired
21+
@Qualifier("genericRedisTemplate")
1822
private RedisTemplate<String, Object> redisTemplate;
1923

2024
private String getKey(String jti) {

0 commit comments

Comments
 (0)