diff --git a/Unosquare.Labs.EmbedIO.Command/App.config b/Unosquare.Labs.EmbedIO.Command/App.config index 3249e041d..8c9939b80 100644 --- a/Unosquare.Labs.EmbedIO.Command/App.config +++ b/Unosquare.Labs.EmbedIO.Command/App.config @@ -13,9 +13,6 @@ http://localhost:9696/ - - C:\unosquare\embedio\Unosquare.Labs.EmbedIO.Samples\html - index.html diff --git a/Unosquare.Labs.EmbedIO.Command/Options.cs b/Unosquare.Labs.EmbedIO.Command/Options.cs new file mode 100644 index 000000000..e91728262 --- /dev/null +++ b/Unosquare.Labs.EmbedIO.Command/Options.cs @@ -0,0 +1,28 @@ +using CommandLine; +using CommandLine.Text; +using System.Collections.Generic; + +namespace Unosquare.Labs.EmbedIO.Command +{ + internal class Options + { + // TODO: Get a JSON file? + //[ValueList(typeof (List), MaximumElements = 1)] + //public IList Items { get; set; } + + [Option('p', "path", Required = true, HelpText = "WWW-root path.")] + public string RootPath { get; set; } + + [OptionList('a', "api", Separator = ',', HelpText = "Specify assemblies to load, separated by a comma.")] + public IList ApiAssemblies { get; set; } + + // TODO: Add url + + [HelpOption] + public string GetUsage() + { + return HelpText.AutoBuild(this, + (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current)); + } + } +} \ No newline at end of file diff --git a/Unosquare.Labs.EmbedIO.Command/Program.cs b/Unosquare.Labs.EmbedIO.Command/Program.cs index 2fdcee2fd..f7c4e4428 100644 --- a/Unosquare.Labs.EmbedIO.Command/Program.cs +++ b/Unosquare.Labs.EmbedIO.Command/Program.cs @@ -1,21 +1,32 @@ namespace Unosquare.Labs.EmbedIO.Command { + using CommandLine; using System; + using System.Linq; + using System.Reflection; using Unosquare.Labs.EmbedIO.Log; + using Unosquare.Labs.EmbedIO.Modules; - class Program + internal class Program { - static void Main(string[] args) + private static readonly SimpleConsoleLog Log = new SimpleConsoleLog(); + + private static void Main(string[] args) { + var options = new Options(); + Console.WriteLine("Unosquare.Labs.EmbedIO Web Server"); + + if (!Parser.Default.ParseArguments(args, options)) return; + Console.WriteLine(" Command-Line Utility: Press any key to stop the server."); - using (var server = new WebServer(Properties.Settings.Default.ServerAddress, new SimpleConsoleLog())) + using (var server = new WebServer(Properties.Settings.Default.ServerAddress, Log)) { if (Properties.Settings.Default.UseLocalSessionModule) - server.RegisterModule(new Modules.LocalSessionModule()); + server.RegisterModule(new LocalSessionModule()); - var staticFilesModule = new Modules.StaticFilesModule(Properties.Settings.Default.HtmlRootPath) + var staticFilesModule = new StaticFilesModule(options.RootPath) { DefaultDocument = Properties.Settings.Default.HtmlDefaultDocument, DefaultExtension = Properties.Settings.Default.HtmlDefaultExtension, @@ -24,10 +35,72 @@ static void Main(string[] args) server.RegisterModule(staticFilesModule); + if (options.ApiAssemblies != null && options.ApiAssemblies.Count > 0) + { + foreach (var api in options.ApiAssemblies) + { + Log.DebugFormat("Checking API {0}", api); + LoadApi(api, server); + } + } + // start the server server.RunAsync(); Console.ReadKey(true); } } + + private static void LoadApi(string apiPath, WebServer server) + { + try + { + var assembly = Assembly.LoadFile(apiPath); + + if (assembly == null) return; + + var types = assembly.GetTypes(); + + // Load WebApiModules + var apiControllers = + types.Where(x => x.IsClass && !x.IsAbstract && x.IsSubclassOf(typeof (WebApiController))).ToArray(); + + if (apiControllers.Any()) + { + server.RegisterModule(new WebApiModule()); + + foreach (var apiController in apiControllers) + { + server.Module().RegisterController(apiController); + Log.DebugFormat("Registering {0} WebAPI", apiController.Name); + } + } + else + { + Log.DebugFormat("{0} does not have any WebAPI", apiPath); + } + + // Load WebSocketsModules + var sockerServers = types.Where(x => x.BaseType == typeof (WebSocketsServer)).ToArray(); + + if (sockerServers.Any()) + { + server.RegisterModule(new WebSocketsModule()); + + foreach (var socketServer in sockerServers) + { + server.Module().RegisterWebSocketsServer(socketServer); + Log.DebugFormat("Registering {0} WebSocket", socketServer.Name); + } + } + else + { + Log.DebugFormat("{0} does not have any WebSocket", apiPath); + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + } } -} +} \ No newline at end of file diff --git a/Unosquare.Labs.EmbedIO.Command/Properties/Settings.Designer.cs b/Unosquare.Labs.EmbedIO.Command/Properties/Settings.Designer.cs index 6ebf0160e..7115a1959 100644 --- a/Unosquare.Labs.EmbedIO.Command/Properties/Settings.Designer.cs +++ b/Unosquare.Labs.EmbedIO.Command/Properties/Settings.Designer.cs @@ -32,15 +32,6 @@ public string ServerAddress { } } - [global::System.Configuration.ApplicationScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("C:\\unosquare\\embedio\\Unosquare.Labs.EmbedIO.Samples\\html")] - public string HtmlRootPath { - get { - return ((string)(this["HtmlRootPath"])); - } - } - [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("index.html")] diff --git a/Unosquare.Labs.EmbedIO.Command/Properties/Settings.settings b/Unosquare.Labs.EmbedIO.Command/Properties/Settings.settings index ad4102be3..7808cb9fe 100644 --- a/Unosquare.Labs.EmbedIO.Command/Properties/Settings.settings +++ b/Unosquare.Labs.EmbedIO.Command/Properties/Settings.settings @@ -5,9 +5,6 @@ http://localhost:9696/ - - C:\unosquare\embedio\Unosquare.Labs.EmbedIO.Samples\html - index.html diff --git a/Unosquare.Labs.EmbedIO.Command/README.md b/Unosquare.Labs.EmbedIO.Command/README.md new file mode 100644 index 000000000..8a58343ed --- /dev/null +++ b/Unosquare.Labs.EmbedIO.Command/README.md @@ -0,0 +1,15 @@ +## EmbedIO CLI + +Start any web folder or EmbedIO-enabled DLL from command line. + +### Run web folder (static content only) + +``` +$ embediocli -p c:\wwwroot +``` + +### Run web folder with WebAPI or WebSocket Assembly + +``` +$ embediocli -p c:\wwwroot --api mywebapi.dll +``` \ No newline at end of file diff --git a/Unosquare.Labs.EmbedIO.Command/Unosquare.Labs.EmbedIO.Command.csproj b/Unosquare.Labs.EmbedIO.Command/Unosquare.Labs.EmbedIO.Command.csproj index e1bbe273b..3074bca4f 100644 --- a/Unosquare.Labs.EmbedIO.Command/Unosquare.Labs.EmbedIO.Command.csproj +++ b/Unosquare.Labs.EmbedIO.Command/Unosquare.Labs.EmbedIO.Command.csproj @@ -8,7 +8,7 @@ Exe Properties Unosquare.Labs.EmbedIO.Command - Unosquare.Labs.EmbedIO.Command + embediocli v4.5 512 true @@ -33,7 +33,14 @@ prompt 4 + + favicon.ico + + + ..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll + True + @@ -43,6 +50,7 @@ + @@ -53,10 +61,12 @@ + SettingsSingleFileGenerator Settings.Designer.cs + @@ -64,6 +74,9 @@ Unosquare.Labs.EmbedIO + + +