1+ use queensac:: domain:: NewSubscriber ;
12use queensac:: { cancel_repository_checker, check_repository_links} ;
23
34use axum:: {
@@ -10,9 +11,6 @@ use std::time::Duration;
1011use tracing:: { Level , error, info} ;
1112use tracing_subscriber:: FmtSubscriber ;
1213
13- const GITHUB_BASE_URL : & str = "https://github.com/" ;
14- const GITHUB_URL_FORMAT : & str = "https://github.com/{owner}/{repo_name}" ;
15-
1614async fn spawn_repository_checker (
1715 repo_url : & str ,
1816 branch : Option < String > ,
@@ -36,20 +34,27 @@ async fn health_check() -> &'static str {
3634
3735#[ derive( Deserialize ) ]
3836struct CheckRequest {
39- repo : RepositoryURL ,
40- branch : Option < String > ,
37+ subscriber : NewSubscriber ,
4138 interval_secs : Option < u64 > ,
4239}
4340
4441async fn check_handler ( Json ( payload) : Json < CheckRequest > ) -> Result < & ' static str , StatusCode > {
4542 info ! (
46- "Received check request for repository: {}, branch: {:?}" ,
47- payload. repo. url, payload. branch
43+ "Received check request for repository: {}, branch: {:?}, email: {}" ,
44+ payload. subscriber. repository_url( ) . url( ) ,
45+ payload. subscriber. branch( ) ,
46+ payload. subscriber. email( ) . as_str( )
4847 ) ;
4948 // FIXME 일단 interval_secs 는 유저가 수정할 수 없게 할 거긴 한데, 일단 테스트할 때 편하게 요청을 받아보자.
5049 let interval = payload. interval_secs . unwrap_or ( 120 ) ;
5150 let interval = Duration :: from_secs ( interval) ;
52- if let Err ( e) = spawn_repository_checker ( & payload. repo . url , payload. branch , interval) . await {
51+ if let Err ( e) = spawn_repository_checker (
52+ payload. subscriber . repository_url ( ) . url ( ) ,
53+ payload. subscriber . branch ( ) . cloned ( ) ,
54+ interval,
55+ )
56+ . await
57+ {
5358 error ! ( "Failed to spawn repository checker: {}" , e) ;
5459 return Err ( StatusCode :: BAD_REQUEST ) ;
5560 }
@@ -58,56 +63,16 @@ async fn check_handler(Json(payload): Json<CheckRequest>) -> Result<&'static str
5863
5964#[ derive( Deserialize ) ]
6065struct CancelRequest {
61- repo : RepositoryURL ,
62- branch : Option < String > ,
63- }
64-
65- /// Represents a GitHub repository URL.
66- ///
67- /// This struct ensures that the URL is valid and follows the format
68- /// `https://github.com/{owner}/{repo_name}`. It includes validation logic
69- /// to enforce this format.
70- #[ derive( Debug , Clone ) ]
71- struct RepositoryURL {
72- /// The URL of the repository.
73- url : String ,
74- }
75-
76- impl < ' de > Deserialize < ' de > for RepositoryURL {
77- /// Custom deserialization logic for `RepositoryURL`.
78- ///
79- /// This implementation ensures that the URL is validated during
80- /// deserialization. If the URL is invalid, an error is returned.
81- fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
82- where
83- D : serde:: Deserializer < ' de > ,
84- {
85- let url = String :: deserialize ( deserializer) ?;
86- let repo = RepositoryURL { url } ;
87- repo. validate ( ) . map_err ( serde:: de:: Error :: custom) ?;
88- Ok ( repo)
89- }
90- }
91-
92- impl RepositoryURL {
93- fn validate ( & self ) -> Result < ( ) , String > {
94- if !self . url . starts_with ( GITHUB_BASE_URL ) {
95- return Err ( format ! ( "URL must start with {}" , GITHUB_BASE_URL ) ) ;
96- }
97- let parts: Vec < & str > = self
98- . url
99- . trim_start_matches ( GITHUB_BASE_URL )
100- . split ( '/' )
101- . collect ( ) ;
102- if parts. len ( ) != 2 || parts[ 0 ] . is_empty ( ) || parts[ 1 ] . is_empty ( ) {
103- return Err ( format ! ( "URL must be in format {}" , GITHUB_URL_FORMAT ) ) ;
104- }
105- Ok ( ( ) )
106- }
66+ subscriber : NewSubscriber ,
10767}
10868
10969async fn cancel_handler ( Json ( payload) : Json < CancelRequest > ) -> Result < & ' static str , StatusCode > {
110- if let Err ( e) = cancel_repository_checker ( & payload. repo . url , payload. branch ) . await {
70+ if let Err ( e) = cancel_repository_checker (
71+ payload. subscriber . repository_url ( ) . url ( ) ,
72+ payload. subscriber . branch ( ) . cloned ( ) ,
73+ )
74+ . await
75+ {
11176 error ! ( "Repository checker failed: {}" , e) ;
11277 return Err ( StatusCode :: BAD_REQUEST ) ;
11378 }
0 commit comments