Skip to content

Commit 394a1f1

Browse files
authored
Merge pull request #49 from CommitField/dev
chore : actuator ๊ถŒํ•œ ์ถ”๊ฐ€
2 parents 396a206 + 82b8d5e commit 394a1f1

File tree

8 files changed

+170
-1
lines changed

8 files changed

+170
-1
lines changed

โ€Žbuild.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ dependencies {
6262
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.6")
6363
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.6")
6464

65+
// WebClient
66+
implementation ("org.springframework.boot:spring-boot-starter-webflux")
6567
}
6668

6769
tasks.withType<Test> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmf.commitField.domain.totalCommit.config;
2+
3+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
4+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
5+
6+
// CORS ์˜ค๋ฅ˜ ์„ค์ • ์šฉ
7+
public class WebConfig implements WebMvcConfigurer {
8+
@Override
9+
public void addCorsMappings(CorsRegistry registry) {
10+
registry.addMapping("/**")
11+
.allowedOrigins("http://localhost:8090") // ํ”„๋ก ํŠธ์—”๋“œ ๋„๋ฉ”์ธ
12+
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
13+
.allowedHeaders("*")
14+
.allowCredentials(true);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmf.commitField.domain.totalCommit.controller;
2+
3+
import cmf.commitField.domain.totalCommit.dto.TotalCommitResponseDto;
4+
import cmf.commitField.domain.totalCommit.service.TotalCommitService;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequiredArgsConstructor
12+
public class TotalCommitController {
13+
private final TotalCommitService totalCommitService;
14+
15+
@GetMapping("/api/commits/{username}")
16+
public TotalCommitResponseDto getTotalCommits(@PathVariable String username) {
17+
return totalCommitService.getTotalCommitCount(username);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cmf.commitField.domain.totalCommit.dto;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
6+
/*
7+
์‘๋‹ต๊ตฌ์กฐ ํ† ๋Œ€๋กœ ๊ตฌํ˜„
8+
{
9+
"data": {
10+
"user": {
11+
"contributionsCollection": {
12+
"totalCommitContributions": 116,
13+
"restrictedContributionsCount": 0
14+
}
15+
}
16+
}
17+
}
18+
*/
19+
@Getter
20+
@NoArgsConstructor
21+
public class TotalCommitGraphQLResponse {
22+
private Data data;
23+
24+
@Getter
25+
@NoArgsConstructor
26+
public static class Data {
27+
CommitUser user;
28+
}
29+
30+
31+
@Getter
32+
@NoArgsConstructor
33+
public static class CommitUser {
34+
private ContributionsCollection contributionsCollection;
35+
}
36+
37+
@Getter
38+
@NoArgsConstructor
39+
public static class ContributionsCollection {
40+
private long totalCommitContributions;
41+
private long restrictedContributionsCount;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmf.commitField.domain.totalCommit.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
7+
@Getter
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class TotalCommitResponseDto {
11+
private long totalCommitContributions;
12+
private long restrictedContributionsCount;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmf.commitField.domain.totalCommit.service;
2+
3+
import cmf.commitField.domain.totalCommit.dto.TotalCommitGraphQLResponse;
4+
import cmf.commitField.domain.totalCommit.dto.TotalCommitResponseDto;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.web.reactive.function.client.WebClient;
11+
12+
import java.util.Map;
13+
14+
15+
@Service
16+
@RequiredArgsConstructor
17+
public class TotalCommitService {
18+
private static final String BASE_URL = "https://api.github.com/graphql";
19+
20+
@Value("${github.token}")
21+
private String PAT;
22+
23+
24+
private final WebClient webClient = WebClient.builder()
25+
.baseUrl(BASE_URL)
26+
.defaultHeader(HttpHeaders.CONTENT_TYPE , MediaType.APPLICATION_JSON_VALUE)
27+
.build();
28+
29+
public TotalCommitResponseDto getTotalCommitCount(String username) {
30+
// String query = String.format("""
31+
// {"query": "query { user(login: \\\"%s\\\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }"}
32+
// """, username);
33+
// GraphQL ์ฟผ๋ฆฌ๋ฅผ Map์œผ๋กœ ๊ตฌ์„ฑ
34+
Map<String, String> requestBody = Map.of(
35+
"query", String.format(
36+
"query { user(login: \"%s\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }",
37+
username
38+
)
39+
);
40+
41+
TotalCommitGraphQLResponse response = webClient.post()
42+
.header("Authorization", "bearer " + PAT)
43+
.bodyValue(requestBody)
44+
.retrieve()
45+
.bodyToMono(TotalCommitGraphQLResponse.class)
46+
.block();
47+
48+
TotalCommitGraphQLResponse.ContributionsCollection contributions = response.getData().getUser().getContributionsCollection();
49+
50+
return new TotalCommitResponseDto(
51+
contributions.getTotalCommitContributions(),
52+
contributions.getRestrictedContributionsCount()
53+
);
54+
}
55+
}

โ€Žsrc/main/java/cmf/commitField/global/security/SecurityConfig.java

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
2323

2424
@Bean
2525
protected SecurityFilterChain config(HttpSecurity http) throws Exception {
26+
// ๊ถŒํ•œ ์„ค์ •
27+
http
28+
.authorizeHttpRequests(auth -> auth
29+
.requestMatchers("/actuator/**").permitAll() // actuator ์—”๋“œํฌ์ธํŠธ ํ—ˆ์šฉ
30+
.anyRequest().authenticated() // ๊ทธ ์™ธ ๋ชจ๋“  ์š”์ฒญ์€ ์ธ์ฆ ํ•„์š”
31+
);
32+
2633
//๋กœ๊ทธ์ธ ๊ด€๋ จ ์„ค์ •
2734
http
2835
.oauth2Login(oauth2 -> oauth2

โ€Žsrc/main/resources/application.yml

+15-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ spring:
4040
name: "commit-field"
4141
cookieDomain: "${custom.dev.cookieDomain}"
4242
frontUrl: "${custom.dev.frontUrl}"
43-
backUrl: "${custom.dev.backUrl}"
43+
backUrl: "${custom.dev.backUrl}"
44+
45+
# swagger ์„ค์ •
46+
springdoc:
47+
swagger-ui:
48+
path: /api-test # swagger-ui ์ ‘๊ทผ ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ๋ณ„์นญ, ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์ ‘์†ํ•ด๋„ http://localhost:8080/swagger-ui/index.html๋กœ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ๋จ.
49+
50+
groups-order: DESC # path, query, body, response ์ˆœ์œผ๋กœ ์ถœ๋ ฅ
51+
52+
tags-sorter: alpha # ํƒœ๊ทธ๋ฅผ ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ์ •๋ ฌ
53+
54+
operations-sorter: method # delete - get - patch - post - put ์ˆœ์œผ๋กœ ์ •๋ ฌ, alpha๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ์ •๋ ฌ ๊ฐ€๋Šฅ
55+
56+
# paths-to-match:
57+
# - /api/** # swagger-ui์— ํ‘œ์‹œํ•  api์˜ ์—”๋“œํฌ์ธํŠธ ํŒจํ„ด

0 commit comments

Comments
ย (0)