11using LinkRouter . App . Configuration ;
2+ using LinkRouter . App . Services ;
23using Microsoft . AspNetCore . Mvc ;
3- using Prometheus ;
4-
54
65namespace LinkRouter . App . Http . Controllers ;
76
87[ ApiController ]
98public class RedirectController : Controller
109{
11-
1210 private readonly Config Config ;
13-
14- private readonly Counter RouteCounter = Metrics . CreateCounter (
15- "linkrouter_requests" ,
16- "Counts the number of requests to the link router" ,
17- new CounterConfiguration
18- {
19- LabelNames = new [ ] { "route" }
20- }
21- ) ;
22-
23-
24- private readonly Counter NotFoundCounter = Metrics . CreateCounter (
25- "linkrouter_404_requests" ,
26- "Counts the number of not found requests to the link router" ,
27- new CounterConfiguration
28- {
29- LabelNames = new [ ] { "route" }
30- }
31- ) ;
11+ private readonly RedirectionService RedirectionService ;
3212
33- public RedirectController ( Config config )
13+ public RedirectController ( Config config , RedirectionService redirectionService )
3414 {
3515 Config = config ;
16+ RedirectionService = redirectionService ;
3617 }
3718
3819 [ HttpGet ( "/{*path}" ) ]
3920 public async Task < ActionResult > RedirectToExternalUrl ( string path )
4021 {
41- if ( ! path . EndsWith ( "/" ) )
42- path += "/" ;
43-
44- path = "/" + path ;
45-
46- Console . WriteLine ( path ) ;
47-
48- var redirectRoute = Config . CompiledRoutes ? . FirstOrDefault ( x => x . CompiledPattern . IsMatch ( path ) ) ;
49-
50- if ( redirectRoute == null )
51- {
52- NotFoundCounter
53- . WithLabels ( path )
54- . Inc ( ) ;
55-
56- if ( Config . NotFoundBehavior . RedirectOn404 )
57- if ( Config . ErrorCodePattern . IsMatch ( Config . NotFoundBehavior . RedirectUrl ) )
58- {
59- var errorCodeMatch = Config . ErrorCodePattern . Match ( Config . NotFoundBehavior . RedirectUrl ) ;
60- var errorCode = int . Parse ( errorCodeMatch . Groups [ 1 ] . Value ) ;
61- return StatusCode ( errorCode ) ;
62- } else
63- return Redirect ( Config . NotFoundBehavior . RedirectUrl ) ;
64-
65- return NotFound ( ) ;
66- }
67-
68- var match = redirectRoute . CompiledPattern . Match ( path ) ;
69-
70- string redirectUrl = redirectRoute . RedirectUrl ;
71-
72- if ( Config . ErrorCodePattern . IsMatch ( redirectUrl ) )
73- {
74- var errorCodeMatch = Config . ErrorCodePattern . Match ( redirectUrl ) ;
75- var errorCode = int . Parse ( errorCodeMatch . Groups [ 1 ] . Value ) ;
76- return StatusCode ( errorCode ) ;
77- }
78-
79- foreach ( var placeholder in redirectRoute . Placeholders )
80- {
81- var value = match . Groups [ placeholder . Value ] . Value ;
82- redirectUrl = redirectUrl . Replace ( "{" + placeholder . Key + "}" , value ) ;
83- }
84-
85- return Redirect ( redirectUrl ) ;
22+ return await RedirectionService . GetRedirect ( path ) ;
8623 }
8724
8825 [ HttpGet ( "/" ) ]
89- public IActionResult GetRootRoute ( )
26+ public async Task < ActionResult > GetRootRoute ( )
9027 {
91- RouteCounter
92- . WithLabels ( "/" )
93- . Inc ( ) ;
94-
95- string url = Config . RootRoute ;
96-
97- if ( Config . ErrorCodePattern . IsMatch ( url ) )
98- {
99- var errorCodeMatch = Config . ErrorCodePattern . Match ( url ) ;
100- var errorCode = int . Parse ( errorCodeMatch . Groups [ 1 ] . Value ) ;
101- return StatusCode ( errorCode ) ;
102- }
103-
104- return Redirect ( url ) ;
28+ return await RedirectionService . GetRedirect ( string . Empty ) ;
10529 }
10630}
0 commit comments