22{
33 using CommandLine ;
44 using System ;
5- using System . Linq ;
65 using System . Reflection ;
7- using Unosquare . Labs . EmbedIO . Log ;
86 using Unosquare . Labs . EmbedIO . Modules ;
97
8+ /// <summary>
9+ /// Entry poing
10+ /// </summary>
1011 internal class Program
1112 {
12- private static readonly SimpleConsoleLog Log = new SimpleConsoleLog ( ) ;
13-
13+ /// <summary>
14+ /// Load WebServer instance
15+ /// </summary>
16+ /// <param name="args"></param>
1417 private static void Main ( string [ ] args )
1518 {
1619 var options = new Options ( ) ;
@@ -21,25 +24,22 @@ private static void Main(string[] args)
2124
2225 Console . WriteLine ( " Command-Line Utility: Press any key to stop the server." ) ;
2326
24- using ( var server = new WebServer ( Properties . Settings . Default . ServerAddress , Log ) )
27+ using ( var server = WebServer . CreateWithConsole ( "http://localhost:" + options . Port + "/" ) )
2528 {
2629 if ( Properties . Settings . Default . UseLocalSessionModule )
27- server . RegisterModule ( new LocalSessionModule ( ) ) ;
30+ server . WithLocalSession ( ) ;
2831
29- var staticFilesModule = new StaticFilesModule ( options . RootPath )
30- {
31- DefaultDocument = Properties . Settings . Default . HtmlDefaultDocument ,
32- DefaultExtension = Properties . Settings . Default . HtmlDefaultExtension ,
33- UseRamCache = Properties . Settings . Default . UseRamCache
34- } ;
32+ server . WithStaticFolderAt ( options . RootPath ,
33+ defaultDocument : Properties . Settings . Default . HtmlDefaultDocument ) ;
3534
36- server . RegisterModule ( staticFilesModule ) ;
35+ server . Module < StaticFilesModule > ( ) . DefaultExtension = Properties . Settings . Default . HtmlDefaultExtension ;
36+ server . Module < StaticFilesModule > ( ) . UseRamCache = Properties . Settings . Default . UseRamCache ;
3737
3838 if ( options . ApiAssemblies != null && options . ApiAssemblies . Count > 0 )
3939 {
4040 foreach ( var api in options . ApiAssemblies )
4141 {
42- Log . DebugFormat ( "Checking API {0}" , api ) ;
42+ server . Log . DebugFormat ( "Registering Assembly {0}" , api ) ;
4343 LoadApi ( api , server ) ;
4444 }
4545 }
@@ -50,6 +50,11 @@ private static void Main(string[] args)
5050 }
5151 }
5252
53+ /// <summary>
54+ /// Load an Assembly
55+ /// </summary>
56+ /// <param name="apiPath"></param>
57+ /// <param name="server"></param>
5358 private static void LoadApi ( string apiPath , WebServer server )
5459 {
5560 try
@@ -58,48 +63,12 @@ private static void LoadApi(string apiPath, WebServer server)
5863
5964 if ( assembly == null ) return ;
6065
61- var types = assembly . GetTypes ( ) ;
62-
63- // Load WebApiModules
64- var apiControllers =
65- types . Where ( x => x . IsClass && ! x . IsAbstract && x . IsSubclassOf ( typeof ( WebApiController ) ) ) . ToArray ( ) ;
66-
67- if ( apiControllers . Any ( ) )
68- {
69- server . RegisterModule ( new WebApiModule ( ) ) ;
70-
71- foreach ( var apiController in apiControllers )
72- {
73- server . Module < WebApiModule > ( ) . RegisterController ( apiController ) ;
74- Log . DebugFormat ( "Registering {0} WebAPI" , apiController . Name ) ;
75- }
76- }
77- else
78- {
79- Log . DebugFormat ( "{0} does not have any WebAPI" , apiPath ) ;
80- }
81-
82- // Load WebSocketsModules
83- var sockerServers = types . Where ( x => x . BaseType == typeof ( WebSocketsServer ) ) . ToArray ( ) ;
84-
85- if ( sockerServers . Any ( ) )
86- {
87- server . RegisterModule ( new WebSocketsModule ( ) ) ;
88-
89- foreach ( var socketServer in sockerServers )
90- {
91- server . Module < WebSocketsModule > ( ) . RegisterWebSocketsServer ( socketServer ) ;
92- Log . DebugFormat ( "Registering {0} WebSocket" , socketServer . Name ) ;
93- }
94- }
95- else
96- {
97- Log . DebugFormat ( "{0} does not have any WebSocket" , apiPath ) ;
98- }
66+ server . LoadApiControllers ( assembly , true ) . LoadWebSockets ( assembly , true ) ;
9967 }
10068 catch ( Exception ex )
10169 {
102- Log . Error ( ex . Message ) ;
70+ server . Log . Error ( ex . Message ) ;
71+ server . Log . Error ( ex . StackTrace ) ;
10372 }
10473 }
10574 }
0 commit comments