Переглянути джерело

WIP

First draft for MQTTnetServer project
* Basic support for settings
* Nasty code for setting up kestrel from appsettings.json
release/3.x.x
Kiarash Ghiaseddin 5 роки тому
джерело
коміт
b68247f56d
13 змінених файлів з 512 додано та 4 видалено
  1. +1
    -0
      .gitignore
  2. +19
    -0
      MQTTnet.sln
  3. +45
    -0
      Source/MQTTnetServer/Controllers/ValuesController.cs
  4. +41
    -0
      Source/MQTTnetServer/MQTTnetServer.csproj
  5. +116
    -0
      Source/MQTTnetServer/MQTTnetServer.xml
  6. +118
    -0
      Source/MQTTnetServer/Program.cs
  7. +30
    -0
      Source/MQTTnetServer/Settings/ListenModel.cs
  8. +23
    -0
      Source/MQTTnetServer/Settings/ListenProtocolTypes.cs
  9. +15
    -0
      Source/MQTTnetServer/Settings/SettingsModel.cs
  10. +61
    -0
      Source/MQTTnetServer/Startup.cs
  11. +9
    -0
      Source/MQTTnetServer/appsettings.Development.json
  12. +32
    -0
      Source/MQTTnetServer/appsettings.json
  13. +2
    -4
      Tests/MQTTnet.TestApp.AspNetCore2/package.json

+ 1
- 0
.gitignore Переглянути файл

@@ -235,6 +235,7 @@ FakesAssemblies/
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
/*/**/package-lock.json

# Typescript v1 declaration files
typings/


+ 19
- 0
MQTTnet.sln Переглянути файл

@@ -47,6 +47,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Extensions.ManagedC
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.AspNetCore.Tests", "Tests\MQTTnet.AspNetCore.Tests\MQTTnet.AspNetCore.Tests.csproj", "{61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnetServer", "Source\MQTTnetServer\MQTTnetServer.csproj", "{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -226,6 +228,22 @@ Global
{61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}.Release|x64.Build.0 = Release|Any CPU
{61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}.Release|x86.ActiveCfg = Release|Any CPU
{61B62223-F5D0-48E4-BBD6-2CBA9353CB5E}.Release|x86.Build.0 = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|ARM.ActiveCfg = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|ARM.Build.0 = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x64.ActiveCfg = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x64.Build.0 = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x86.ActiveCfg = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Debug|x86.Build.0 = Debug|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|Any CPU.Build.0 = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|ARM.ActiveCfg = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|ARM.Build.0 = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x64.ActiveCfg = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x64.Build.0 = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x86.ActiveCfg = Release|Any CPU
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -241,6 +259,7 @@ Global
{998D04DD-7CB0-45F5-A393-E2495C16399E} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4}
{C400533A-8EBA-4F0B-BF4D-295C3708604B} = {12816BCC-AF9E-44A9-9AE5-C246AF2A0587}
{61B62223-F5D0-48E4-BBD6-2CBA9353CB5E} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4}
{5699FB8C-838C-4AB0-80A5-9CA809F9B65B} = {32A630A7-2598-41D7-B625-204CD906F5FB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {07536672-5CBC-4BE3-ACE0-708A431A7894}


+ 45
- 0
Source/MQTTnetServer/Controllers/ValuesController.cs Переглянути файл

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace MQTTnetServer.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

+ 41
- 0
Source/MQTTnetServer/MQTTnetServer.csproj Переглянути файл

@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyName>MQTTnetServer</AssemblyName>
<RootNamespace>MQTTnetServer</RootNamespace>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Company />
<Product />
<Description />
<Authors />
<PackageId />
<SignAssembly>false</SignAssembly>
<DelaySign>false</DelaySign>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>MQTTnetServer.xml</DocumentationFile>
<WarningsAsErrors>NU1605</WarningsAsErrors>
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>

<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj" />
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj" />
</ItemGroup>

</Project>

+ 116
- 0
Source/MQTTnetServer/MQTTnetServer.xml Переглянути файл

@@ -0,0 +1,116 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>MQTTnetServer</name>
</assembly>
<members>
<member name="T:MQTTnetServer.Program">
<summary>
Main Entry point
</summary>
</member>
<member name="M:MQTTnetServer.Program.Main(System.String[])">
<summary>
Main
</summary>
<param name="args"></param>
</member>
<member name="M:MQTTnetServer.Program.CreateWebHostBuilder(System.String[])">
<summary>
Configure and Start Kestrel
</summary>
<param name="args"></param>
<returns></returns>
</member>
<member name="M:MQTTnetServer.Program.ReadListenSettings">
<summary>
Read Application Settings
</summary>
<returns></returns>
</member>
<member name="T:MQTTnetServer.Settings.ListenModel">
<summary>
Listen Entry Settings Model
</summary>
</member>
<member name="M:MQTTnetServer.Settings.ListenModel.#ctor">
<summary>
Constructor
</summary>
</member>
<member name="P:MQTTnetServer.Settings.ListenModel.Address">
<summary>
Listen Address
</summary>
</member>
<member name="P:MQTTnetServer.Settings.ListenModel.Port">
<summary>
Listen Port
</summary>
</member>
<member name="P:MQTTnetServer.Settings.ListenModel.Protocol">
<summary>
Protocol Type
</summary>
</member>
<member name="T:MQTTnetServer.Settings.ListenProtocolTypes">
<summary>
Listen Protocol Types
</summary>
</member>
<member name="F:MQTTnetServer.Settings.ListenProtocolTypes.HTTP">
<summary>
HTTP
</summary>
</member>
<member name="F:MQTTnetServer.Settings.ListenProtocolTypes.HTTPS">
<summary>
HTTPS
</summary>
</member>
<member name="F:MQTTnetServer.Settings.ListenProtocolTypes.MQTT">
<summary>
MQTT
</summary>
</member>
<member name="T:MQTTnetServer.Settings.SettingsModel">
<summary>
Main Settings Model
</summary>
</member>
<member name="P:MQTTnetServer.Settings.SettingsModel.Listen">
<summary>
Listen Settings
</summary>
</member>
<member name="T:MQTTnetServer.Startup">
<summary>
Web App Startup
</summary>
</member>
<member name="M:MQTTnetServer.Startup.#ctor(Microsoft.Extensions.Configuration.IConfiguration)">
<summary>
Constructor
</summary>
<param name="configuration"></param>
</member>
<member name="P:MQTTnetServer.Startup.Configuration">
<summary>
Application Settings
</summary>
</member>
<member name="M:MQTTnetServer.Startup.Configure(Microsoft.AspNetCore.Builder.IApplicationBuilder,Microsoft.AspNetCore.Hosting.IHostingEnvironment)">
<summary>
This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
</summary>
<param name="app"></param>
<param name="env"></param>
</member>
<member name="M:MQTTnetServer.Startup.ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<summary>
This method gets called by the runtime. Use this method to add services to the container.
</summary>
<param name="services"></param>
</member>
</members>
</doc>

+ 118
- 0
Source/MQTTnetServer/Program.cs Переглянути файл

@@ -0,0 +1,118 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using MQTTnet.AspNetCore;
using MQTTnetServer.Settings;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace MQTTnetServer
{
/// <summary>
/// Main Entry point
/// </summary>
public class Program
{
/// <summary>
/// Main
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
try
{
CreateWebHostBuilder(args).Build().Run();
}
catch (FileNotFoundException e)
{
Console.WriteLine("Could not find application settings file in: " + e.FileName);
return;
}
}

/// <summary>
/// Configure and Start Kestrel
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var webHost = WebHost.CreateDefaultBuilder(args);
var listen = ReadListenSettings();
webHost
.UseKestrel(o =>
{
if (listen?.Length > 0)
{
foreach (var item in listen)
{
if (item.Address?.Trim() == "*")
{
if (item.Protocol == ListenProtocolTypes.MQTT)
{
o.ListenAnyIP(item.Port, c => c.UseMqtt());
}
else
{
o.ListenAnyIP(item.Port);
}
}
else if (item.Address?.Trim() == "localhost")
{
if (item.Protocol == ListenProtocolTypes.MQTT)
{
o.ListenLocalhost(item.Port, c => c.UseMqtt());
}
else
{
o.ListenLocalhost(item.Port);
}
}
else
{
if (IPAddress.TryParse(item.Address, out var ip))
{
if (item.Protocol == ListenProtocolTypes.MQTT)
{
o.Listen(ip, item.Port, c => c.UseMqtt());
}
else
{
o.Listen(ip, item.Port);
}
}
}
}
}
else
{
o.ListenAnyIP(1883, l => l.UseMqtt());
o.ListenAnyIP(5000);
}
});

webHost.UseStartup<Startup>();

return webHost;
}

/// <summary>
/// Read Application Settings
/// </summary>
/// <returns></returns>
public static ListenModel[] ReadListenSettings()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();

var listen = new List<ListenModel>();
builder.Bind("MQTTnetServer:Listen", listen);

return listen.ToArray();
}
}
}

+ 30
- 0
Source/MQTTnetServer/Settings/ListenModel.cs Переглянути файл

@@ -0,0 +1,30 @@
namespace MQTTnetServer.Settings
{
/// <summary>
/// Listen Entry Settings Model
/// </summary>
public class ListenModel
{
/// <summary>
/// Constructor
/// </summary>
public ListenModel()
{
}

/// <summary>
/// Listen Address
/// </summary>
public string Address { get; set; }

/// <summary>
/// Listen Port
/// </summary>
public int Port { get; set; }

/// <summary>
/// Protocol Type
/// </summary>
public ListenProtocolTypes Protocol { get; set; } = ListenProtocolTypes.HTTP;
}
}

+ 23
- 0
Source/MQTTnetServer/Settings/ListenProtocolTypes.cs Переглянути файл

@@ -0,0 +1,23 @@
namespace MQTTnetServer.Settings
{
/// <summary>
/// Listen Protocol Types
/// </summary>
public enum ListenProtocolTypes
{
/// <summary>
/// HTTP
/// </summary>
HTTP = 0,

/// <summary>
/// HTTPS
/// </summary>
HTTPS = 1,

/// <summary>
/// MQTT
/// </summary>
MQTT = 20
}
}

+ 15
- 0
Source/MQTTnetServer/Settings/SettingsModel.cs Переглянути файл

@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace MQTTnetServer.Settings
{
/// <summary>
/// Main Settings Model
/// </summary>
public class SettingsModel
{
/// <summary>
/// Listen Settings
/// </summary>
public IEnumerable<ListenModel> Listen { get; set; }
}
}

+ 61
- 0
Source/MQTTnetServer/Startup.cs Переглянути файл

@@ -0,0 +1,61 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MQTTnetServer
{
/// <summary>
/// Web App Startup
/// </summary>
public class Startup
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="configuration"></param>
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}

/// <summary>
/// Application Settings
/// </summary>
public IConfigurationRoot Configuration { get; }

/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseMvc();
}

/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
}
}

+ 9
- 0
Source/MQTTnetServer/appsettings.Development.json Переглянути файл

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

+ 32
- 0
Source/MQTTnetServer/appsettings.json Переглянути файл

@@ -0,0 +1,32 @@
{
"MQTTnetServer": {
/*
Wildcard Addresses:
* - All local IP addresses
localhost - Localhost only

Supported Protocols:
0 - HTTP
1 - HTTPS
20 - MQTT
*/
"Listen": [
{
"Address": "localhost",
"Port": 5000,
"Protocol": 0
},
{
"Address": "*",
"Port": 1883,
"Protocol": 20
}
]
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

+ 2
- 4
Tests/MQTTnet.TestApp.AspNetCore2/package.json Переглянути файл

@@ -2,13 +2,11 @@
"version": "1.0.0",
"name": "mqtt.test",
"private": true,
"devDependencies": {
},

"devDependencies": {},
"dependencies": {
"mqtt": "2.15.1",
"@types/node": "8.0.46",
"systemjs": "0.20.19",
"typescript": "2.3.4"
}
}
}

Завантаження…
Відмінити
Зберегти