1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
- using Microsoft . Extensions . Configuration ;
5
- using Microsoft . Extensions . Logging ;
6
4
using Microsoft . DevProxy . Abstractions ;
7
5
using System . CommandLine ;
8
6
using System . CommandLine . Invocation ;
9
7
10
- namespace Microsoft . DevProxy ;
8
+ namespace Microsoft . DevProxy . CommandHandlers ;
11
9
12
10
public class ProxyCommandHandler : ICommandHandler
13
11
{
14
- private readonly PluginEvents _pluginEvents ;
12
+ private readonly IPluginEvents _pluginEvents ;
15
13
private readonly Option [ ] _options ;
16
14
private readonly ISet < UrlToWatch > _urlsToWatch ;
17
15
private readonly ILogger _logger ;
18
16
19
- public ProxyCommandHandler ( PluginEvents pluginEvents ,
17
+ public static ProxyConfiguration Configuration { get => ConfigurationFactory . Value ; }
18
+
19
+ public ProxyCommandHandler ( IPluginEvents pluginEvents ,
20
20
Option [ ] options ,
21
21
ISet < UrlToWatch > urlsToWatch ,
22
22
ILogger logger )
@@ -33,6 +33,73 @@ public int Invoke(InvocationContext context)
33
33
}
34
34
35
35
public async Task < int > InvokeAsync ( InvocationContext context )
36
+ {
37
+ ParseOptions ( context ) ;
38
+ _pluginEvents . RaiseOptionsLoaded ( new OptionsLoadedArgs ( context , _options ) ) ;
39
+ await CheckForNewVersion ( ) ;
40
+
41
+ try
42
+ {
43
+ var builder = WebApplication . CreateBuilder ( ) ;
44
+ builder . Logging . AddFilter ( "Microsoft.Hosting.*" , LogLevel . Error ) ;
45
+ builder . Logging . AddFilter ( "Microsoft.AspNetCore.*" , LogLevel . Error ) ;
46
+
47
+ builder . Services . AddSingleton < IProxyState , ProxyState > ( ) ;
48
+ builder . Services . AddSingleton < IProxyConfiguration , ProxyConfiguration > ( sp => ConfigurationFactory . Value ) ;
49
+ builder . Services . AddSingleton ( _pluginEvents ) ;
50
+ builder . Services . AddSingleton ( _logger ) ;
51
+ builder . Services . AddSingleton ( _urlsToWatch ) ;
52
+ builder . Services . AddHostedService < ProxyEngine > ( ) ;
53
+
54
+ builder . Services . AddControllers ( ) ;
55
+ builder . Services . AddEndpointsApiExplorer ( ) ;
56
+ builder . Services . AddSwaggerGen ( ) ;
57
+
58
+ builder . Services . Configure < RouteOptions > ( options =>
59
+ {
60
+ options . LowercaseUrls = true ;
61
+ } ) ;
62
+
63
+ builder . WebHost . ConfigureKestrel ( options =>
64
+ {
65
+ options . ListenLocalhost ( ConfigurationFactory . Value . ApiPort ) ;
66
+ _logger . LogInformation ( "Dev Proxy API listening on http://localhost:{Port}..." , ConfigurationFactory . Value . ApiPort ) ;
67
+ } ) ;
68
+
69
+ var app = builder . Build ( ) ;
70
+
71
+ if ( app . Environment . IsDevelopment ( ) )
72
+ {
73
+ app . UseSwagger ( ) ;
74
+ app . UseSwaggerUI ( ) ;
75
+ }
76
+
77
+ app . MapControllers ( ) ;
78
+ app . Run ( ) ;
79
+
80
+
81
+ return 0 ;
82
+ }
83
+ catch ( Exception ex )
84
+ {
85
+ _logger . LogError ( ex , "An error occurred while running Dev Proxy" ) ;
86
+ var inner = ex . InnerException ;
87
+
88
+ while ( inner is not null )
89
+ {
90
+ _logger . LogError ( inner , "============ Inner exception ============" ) ;
91
+ inner = inner . InnerException ;
92
+ }
93
+ #if DEBUG
94
+ throw ; // so debug tools go straight to the source of the exception when attached
95
+ #else
96
+ return 1 ;
97
+ #endif
98
+ }
99
+
100
+ }
101
+
102
+ private void ParseOptions ( InvocationContext context )
36
103
{
37
104
var port = context . ParseResult . GetValueForOption < int ? > ( ProxyHost . PortOptionName , _options ) ;
38
105
if ( port is not null )
@@ -79,11 +146,10 @@ public async Task<int> InvokeAsync(InvocationContext context)
79
146
{
80
147
Configuration . InstallCert = installCert . Value ;
81
148
}
149
+ }
82
150
83
- CancellationToken ? cancellationToken = ( CancellationToken ? ) context . BindingContext . GetService ( typeof ( CancellationToken ? ) ) ;
84
-
85
- _pluginEvents . RaiseOptionsLoaded ( new OptionsLoadedArgs ( context , _options ) ) ;
86
-
151
+ private async Task CheckForNewVersion ( )
152
+ {
87
153
var newReleaseInfo = await UpdateNotification . CheckForNewVersion ( Configuration . NewVersionNotification ) ;
88
154
if ( newReleaseInfo != null )
89
155
{
@@ -93,39 +159,14 @@ public async Task<int> InvokeAsync(InvocationContext context)
93
159
Environment . NewLine
94
160
) ;
95
161
}
96
-
97
- try
98
- {
99
- await new ProxyEngine ( Configuration , _urlsToWatch , _pluginEvents , _logger ) . Run ( cancellationToken ) ;
100
- return 0 ;
101
- }
102
- catch ( Exception ex )
103
- {
104
- _logger . LogError ( ex , "An error occurred while running Dev Proxy" ) ;
105
- var inner = ex . InnerException ;
106
-
107
- while ( inner is not null )
108
- {
109
- _logger . LogError ( inner , "============ Inner exception ============" ) ;
110
- inner = inner . InnerException ;
111
- }
112
- #if DEBUG
113
- throw ; // so debug tools go straight to the source of the exception when attached
114
- #else
115
- return 1 ;
116
- #endif
117
- }
118
-
119
162
}
120
163
121
- public static ProxyConfiguration Configuration { get => ConfigurationFactory . Value ; }
122
-
123
164
private static readonly Lazy < ProxyConfiguration > ConfigurationFactory = new ( ( ) =>
124
165
{
125
166
var builder = new ConfigurationBuilder ( ) ;
126
167
var configuration = builder
127
- . AddJsonFile ( ProxyHost . ConfigFile , optional : true , reloadOnChange : true )
128
- . Build ( ) ;
168
+ . AddJsonFile ( ProxyHost . ConfigFile , optional : true , reloadOnChange : true )
169
+ . Build ( ) ;
129
170
var configObject = new ProxyConfiguration ( ) ;
130
171
configuration . Bind ( configObject ) ;
131
172
0 commit comments