Skip to content

Commit f784a3d

Browse files
committed
Embedio CLI
1 parent 1e62b89 commit f784a3d

File tree

14 files changed

+226
-42
lines changed

14 files changed

+226
-42
lines changed

Unosquare.Labs.EmbedIO.Command/App.config

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
<setting name="ServerAddress" serializeAs="String">
1414
<value>http://localhost:9696/</value>
1515
</setting>
16-
<setting name="HtmlRootPath" serializeAs="String">
17-
<value>C:\unosquare\embedio\Unosquare.Labs.EmbedIO.Samples\html</value>
18-
</setting>
1916
<setting name="HtmlDefaultDocument" serializeAs="String">
2017
<value>index.html</value>
2118
</setting>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using CommandLine;
2+
using CommandLine.Text;
3+
using System.Collections.Generic;
4+
5+
namespace Unosquare.Labs.EmbedIO.Command
6+
{
7+
internal class Options
8+
{
9+
// TODO: Get a JSON file?
10+
//[ValueList(typeof (List<string>), MaximumElements = 1)]
11+
//public IList<string> Items { get; set; }
12+
13+
[Option('p', "path", Required = true, HelpText = "WWW-root path.")]
14+
public string RootPath { get; set; }
15+
16+
[OptionList('a', "api", Separator = ',', HelpText = "Specify assemblies to load, separated by a comma.")]
17+
public IList<string> ApiAssemblies { get; set; }
18+
19+
// TODO: Add url
20+
21+
[HelpOption]
22+
public string GetUsage()
23+
{
24+
return HelpText.AutoBuild(this,
25+
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
26+
}
27+
}
28+
}
Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
namespace Unosquare.Labs.EmbedIO.Command
22
{
3+
using CommandLine;
34
using System;
5+
using System.Linq;
6+
using System.Reflection;
47
using Unosquare.Labs.EmbedIO.Log;
8+
using Unosquare.Labs.EmbedIO.Modules;
59

6-
class Program
10+
internal class Program
711
{
8-
static void Main(string[] args)
12+
private static readonly SimpleConsoleLog Log = new SimpleConsoleLog();
13+
14+
private static void Main(string[] args)
915
{
16+
var options = new Options();
17+
1018
Console.WriteLine("Unosquare.Labs.EmbedIO Web Server");
19+
20+
if (!Parser.Default.ParseArguments(args, options)) return;
21+
1122
Console.WriteLine(" Command-Line Utility: Press any key to stop the server.");
1223

13-
using (var server = new WebServer(Properties.Settings.Default.ServerAddress, new SimpleConsoleLog()))
24+
using (var server = new WebServer(Properties.Settings.Default.ServerAddress, Log))
1425
{
1526
if (Properties.Settings.Default.UseLocalSessionModule)
16-
server.RegisterModule(new Modules.LocalSessionModule());
27+
server.RegisterModule(new LocalSessionModule());
1728

18-
var staticFilesModule = new Modules.StaticFilesModule(Properties.Settings.Default.HtmlRootPath)
29+
var staticFilesModule = new StaticFilesModule(options.RootPath)
1930
{
2031
DefaultDocument = Properties.Settings.Default.HtmlDefaultDocument,
2132
DefaultExtension = Properties.Settings.Default.HtmlDefaultExtension,
@@ -24,10 +35,72 @@ static void Main(string[] args)
2435

2536
server.RegisterModule(staticFilesModule);
2637

38+
if (options.ApiAssemblies != null && options.ApiAssemblies.Count > 0)
39+
{
40+
foreach (var api in options.ApiAssemblies)
41+
{
42+
Log.DebugFormat("Checking API {0}", api);
43+
LoadApi(api, server);
44+
}
45+
}
46+
2747
// start the server
2848
server.RunAsync();
2949
Console.ReadKey(true);
3050
}
3151
}
52+
53+
private static void LoadApi(string apiPath, WebServer server)
54+
{
55+
try
56+
{
57+
var assembly = Assembly.LoadFile(apiPath);
58+
59+
if (assembly == null) return;
60+
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+
}
99+
}
100+
catch (Exception ex)
101+
{
102+
Log.Error(ex.Message);
103+
}
104+
}
32105
}
33-
}
106+
}

Unosquare.Labs.EmbedIO.Command/Properties/Settings.Designer.cs

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Unosquare.Labs.EmbedIO.Command/Properties/Settings.settings

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
<Setting Name="ServerAddress" Type="System.String" Scope="Application">
66
<Value Profile="(Default)">http://localhost:9696/</Value>
77
</Setting>
8-
<Setting Name="HtmlRootPath" Type="System.String" Scope="Application">
9-
<Value Profile="(Default)">C:\unosquare\embedio\Unosquare.Labs.EmbedIO.Samples\html</Value>
10-
</Setting>
118
<Setting Name="HtmlDefaultDocument" Type="System.String" Scope="Application">
129
<Value Profile="(Default)">index.html</Value>
1310
</Setting>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## EmbedIO CLI
2+
3+
Start any web folder or EmbedIO-enabled DLL from command line.
4+
5+
### Run web folder (static content only)
6+
7+
```
8+
$ embediocli -p c:\wwwroot
9+
```
10+
11+
### Run web folder with WebAPI or WebSocket Assembly
12+
13+
```
14+
$ embediocli -p c:\wwwroot --api mywebapi.dll
15+
```

Unosquare.Labs.EmbedIO.Command/Unosquare.Labs.EmbedIO.Command.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<OutputType>Exe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>Unosquare.Labs.EmbedIO.Command</RootNamespace>
11-
<AssemblyName>Unosquare.Labs.EmbedIO.Command</AssemblyName>
11+
<AssemblyName>embediocli</AssemblyName>
1212
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@@ -33,7 +33,14 @@
3333
<ErrorReport>prompt</ErrorReport>
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
36+
<PropertyGroup>
37+
<ApplicationIcon>favicon.ico</ApplicationIcon>
38+
</PropertyGroup>
3639
<ItemGroup>
40+
<Reference Include="CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
41+
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
42+
<Private>True</Private>
43+
</Reference>
3744
<Reference Include="System" />
3845
<Reference Include="System.Core" />
3946
<Reference Include="System.Xml.Linq" />
@@ -43,6 +50,7 @@
4350
<Reference Include="System.Xml" />
4451
</ItemGroup>
4552
<ItemGroup>
53+
<Compile Include="Options.cs" />
4654
<Compile Include="Program.cs" />
4755
<Compile Include="Properties\AssemblyInfo.cs" />
4856
<Compile Include="Properties\Settings.Designer.cs">
@@ -53,17 +61,22 @@
5361
</ItemGroup>
5462
<ItemGroup>
5563
<None Include="App.config" />
64+
<None Include="packages.config" />
5665
<None Include="Properties\Settings.settings">
5766
<Generator>SettingsSingleFileGenerator</Generator>
5867
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
5968
</None>
69+
<None Include="README.md" />
6070
</ItemGroup>
6171
<ItemGroup>
6272
<ProjectReference Include="..\Unosquare.Labs.EmbedIO\Unosquare.Labs.EmbedIO.csproj">
6373
<Project>{7d7c29b4-9493-4ebd-8f20-6fac1e7161ee}</Project>
6474
<Name>Unosquare.Labs.EmbedIO</Name>
6575
</ProjectReference>
6676
</ItemGroup>
77+
<ItemGroup>
78+
<Content Include="favicon.ico" />
79+
</ItemGroup>
6780
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6881
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6982
Other similar extension points exist, see Microsoft.Common.targets.
1.12 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="CommandLineParser" version="1.9.71" targetFramework="net45" />
4+
</packages>

Unosquare.Labs.EmbedIO.Samples/RestApiSample.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
public static class RestApiSample
1414
{
1515
private const string RelativePath = "/api/";
16-
public static List<Person> People { get; private set; }
16+
17+
public static List<Person> People = new List<Person>
18+
{
19+
new Person() {Key = 1, Name = "Mario Di Vece", Age = 31, EmailAddress = "[email protected]"},
20+
new Person() {Key = 2, Name = "Geovanni Perez", Age = 32, EmailAddress = "[email protected]"},
21+
new Person() {Key = 3, Name = "Luis Gonzalez", Age = 29, EmailAddress = "[email protected]"},
22+
};
1723

1824
/// <summary>
1925
/// Here we add the WebApiModule to our Web Server and register our controller classes.
@@ -23,13 +29,6 @@ public static class RestApiSample
2329
/// <param name="server">The server.</param>
2430
public static void Setup(WebServer server)
2531
{
26-
People = new List<Person>()
27-
{
28-
new Person() {Key = 1, Name = "Mario Di Vece", Age = 31, EmailAddress = "[email protected]"},
29-
new Person() {Key = 2, Name = "Geovanni Perez", Age = 32, EmailAddress = "[email protected]"},
30-
new Person() {Key = 3, Name = "Luis Gonzalez", Age = 29, EmailAddress = "[email protected]"},
31-
};
32-
3332
foreach (var person in People)
3433
{
3534
person.PhotoUrl = GetGravatarUrl(person.EmailAddress);
@@ -41,22 +40,23 @@ public static void Setup(WebServer server)
4140

4241
private static string GetGravatarUrl(string emailAddress)
4342
{
44-
return string.Format("http://www.gravatar.com/avatar/{0}.png?s=100", HashMD5(emailAddress));
43+
return string.Format("http://www.gravatar.com/avatar/{0}.png?s=100", HashMd5(emailAddress));
4544
}
4645

47-
private static string HashMD5(string input)
46+
private static string HashMd5(string input)
4847
{
4948
// step 1, calculate MD5 hash from input
50-
MD5 md5 = System.Security.Cryptography.MD5.Create();
51-
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
49+
MD5 md5 = MD5.Create();
50+
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
5251
byte[] hash = md5.ComputeHash(inputBytes);
5352

5453
// step 2, convert byte array to hex string
55-
StringBuilder sb = new StringBuilder();
54+
var sb = new StringBuilder();
5655
for (int i = 0; i < hash.Length; i++)
5756
{
5857
sb.Append(hash[i].ToString("x2"));
5958
}
59+
6060
return sb.ToString();
6161
}
6262

0 commit comments

Comments
 (0)