First draft for MQTTnetServer project
* Basic support for settings
* Nasty code for setting up kestrel from appsettings.json
This commit is contained in:
Kiarash Ghiaseddin
2019-03-04 21:13:22 +01:00
parent 3b02a5f6aa
commit b68247f56d
13 changed files with 512 additions and 4 deletions
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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();
}
}
}
@@ -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;
}
}
@@ -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
}
}
@@ -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
View File
@@ -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);
}
}
}
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
+32
View File
@@ -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": "*"
}