Skip to content

Commit 60e7cc1

Browse files
authored
Adjust WebServer samples to support new FileSystem nuget (#330)
1 parent 6a53ee0 commit 60e7cc1

23 files changed

+418
-96
lines changed

samples/Webserver/README.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Shows how to use .NET **nanoFramework** Webserver library in common usage scenar
55
## Samples
66

77
- [🌶️ - REST and GPIO](./WebServer.GpioRest/)
8-
- [🌶️🌶️ - HTTP server](./WebServer.Sample/)
8+
- [🌶️🌶️ - HTTP server](./WebServer.Sample/WebServer.Sample/)
9+
- [🌶️🌶️ - HTTP server with file system](./WebServer.Sample/WebServer.Sample.FileSystem/)
910
- [🌶️🌶️ - Dependency Injection](./WebServer.DI/)
1011

1112
## Usage
@@ -68,9 +69,10 @@ The `RouteAnyTest`is called whenever the url is `test/any` whatever the method i
6869

6970
There is a more advance example with simple REST API to get a list of Person and add a Person. Check it in the [sample](./WebServer.Sample/ControllerPerson.cs).
7071

71-
**Important**
72-
* By default the routes are not case sensitive and the attribute **must** be lowercase
73-
* If you want to use case sensitive routes like in the previous example, use the attribute `CaseSensitive`. As in the previous example, you **must** write the route as you want it to be responded to.
72+
> ![Important]
73+
>
74+
> - By default the routes are not case sensitive and the attribute **must** be lowercase
75+
> - If you want to use case sensitive routes like in the previous example, use the attribute `CaseSensitive`. As in the previous example, you **must** write the route as you want it to be responded to.
7476
7577
## A simple GPIO controller REST API
7678

@@ -84,6 +86,7 @@ You will find in simple [GPIO controller sample](./WebServer.GpioRest) REST API.
8486
- To read the pin 4: http://yoururl/read/4, you will get as a raw text `high`or `low`depending on the state
8587

8688
## DI integration with controllers
89+
8790
Check [WebServer.DI sample](./WebServer.DI) if you want to use controllers with automatically injecting services from [nanoFramework.DependencyInjection](https://github.com/nanoframework/nanoFramework.DependencyInjection).
8891

8992
- Type you credentials in Program.cs files
@@ -95,13 +98,13 @@ Check [WebServer.DI sample](./WebServer.DI) if you want to use controllers with
9598
Controllers support authentication. 3 types of authentications are currently implemented on controllers only:
9699

97100
- Basic: the classic user and password following the HTTP standard. Usage:
98-
- `[Authentication("Basic")]` will use the default credential of the webserver
99-
- `[Authentication("Basic:myuser mypassword")]` will use myuser as a user and my password as a password. Note: the user cannot contains spaces.
101+
- `[Authentication("Basic")]` will use the default credential of the webserver
102+
- `[Authentication("Basic:myuser mypassword")]` will use myuser as a user and my password as a password. Note: the user cannot contains spaces.
100103
- APiKey in header: add ApiKey in headers with the API key. Usage:
101-
- `[Authentication("ApiKey")]` will use the default credential of the webserver
102-
- `[Authentication("ApiKeyc:akey")]` will use akey as ApiKey.
104+
- `[Authentication("ApiKey")]` will use the default credential of the webserver
105+
- `[Authentication("ApiKeyc:akey")]` will use akey as ApiKey.
103106
- None: no authentication required. Usage:
104-
- `[Authentication("None")]` will use the default credential of the webserver
107+
- `[Authentication("None")]` will use the default credential of the webserver
105108

106109
The Authentication attribute applies to both public Classes an public Methods.
107110

@@ -166,16 +169,15 @@ using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { type
166169
With the previous example the following happens:
167170

168171
- All the controller by default, even when nothing is specified will use the controller credentials. In our case, the Basic authentication with the default user (topuser) and password (topPassword) will be used.
169-
- When calling http://yoururl/authbasic from a browser, you will be prompted for the user and password, use the default one topuser and topPassword to get access
170-
- When calling http://yoururl/authnone, you won't be prompted because the authentication has been overridden for no authentication
171-
- When calling http://yoururl/authbasicspecial, the user and password are different from the defautl ones, user2 and password is the right couple here
172+
- When calling http://yoururl/authbasic from a browser, you will be prompted for the user and password, use the default one topuser and topPassword to get access
173+
- When calling http://yoururl/authnone, you won't be prompted because the authentication has been overridden for no authentication
174+
- When calling http://yoururl/authbasicspecial, the user and password are different from the defautl ones, user2 and password is the right couple here
172175
- If you would have define in the controller a specific user and password like `[Authentication("Basic:myuser mypassword")]`, then the default one for all the controller would have been myuser and mypassword
173176
- When calling http://yoururl/authapi, you must pass the header `ApiKey` (case sensitive) with the value `superKey1234` to get authorized, this is overridden the default Basic authentication
174177
- When calling http://yoururl/authdefaultapi, the default key `ATopSecretAPIKey1234` will be used so you have to pass it in the headers of the request
175178

176179
All up, this is an example to show how to use authentication, it's been defined to allow flexibility.
177180

178-
179181
## Managing incoming queries thru events
180182

181183
Very basic usage is the following:
@@ -225,15 +227,27 @@ if (url.ToLower().IndexOf("/param.htm") == 0)
225227
}
226228
```
227229

228-
And server static files:
230+
To serve static files, you have to use the `nanoFramework.WebServer.FileSystem` nuget. This is only supported on devices having the `System.IO.FileSystem` capability.
229231

230232
```csharp
231-
var files = storage.GetFiles();
232-
foreach (var file in files)
233+
// Gets the list of all files in a specific directory
234+
// See the MountExample for more details if you need to mount an SD card and adjust here
235+
// https://github.com/nanoframework/Samples/blob/main/samples/System.IO.FileSystem/MountExample/Program.cs
236+
_listFiles = Directory.GetFiles(DirectoryPath);
237+
// Remove the root directory
238+
for (int i = 0; i < _listFiles.Length; i++)
239+
{
240+
_listFiles[i] = _listFiles[i].Substring(DirectoryPath.Length);
241+
}
242+
243+
var fileName = url.Substring(1);
244+
// Note that the file name is case sensitive
245+
// Very simple example serving a static file on an SD card
246+
foreach (var file in _listFiles)
233247
{
234-
if (file.Name == url)
248+
if (file == fileName)
235249
{
236-
WebServer.SendFileOverHTTP(e.Context.Response, file);
250+
WebServer.SendFileOverHTTP(e.Context.Response, DirectoryPath + file);
237251
return;
238252
}
239253
}
@@ -345,7 +359,7 @@ using (WebServer server = new WebServer(443, HttpProtocol.Https)
345359
}
346360
```
347361

348-
> IMPORTANT: because the certificate above is not issued from a Certificate Authority it won't be recognized as a valid certificate. If you want to access the nanoFramework device with your browser, for example, you'll have to add the (CRT file)[WebServer.Sample\webserver-cert.crt] as a trusted one. On Windows, you just have to double click on the CRT file and then click "Install Certificate...".
362+
> IMPORTANT: because the certificate above is not issued from a Certificate Authority it won't be recognized as a valid certificate. If you want to access the nanoFramework device with your browser, for example, you'll have to add the [CRT file](WebServer.Sample\WebServer.Sample\webserver-cert.crt) as a trusted one. On Windows, you just have to double click on the CRT file and then click "Install Certificate...".
349363

350364
You can of course use the routes as defined earlier. Both will work, event or route with the notion of controller.
351365

samples/Webserver/WebServer.DI/WebServer.Sample.nfproj

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@
5151
<HintPath>packages\nanoFramework.System.Text.1.2.37\lib\nanoFramework.System.Text.dll</HintPath>
5252
<Private>True</Private>
5353
</Reference>
54-
<Reference Include="nanoFramework.WebServer, Version=1.2.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
55-
<HintPath>packages\nanoFramework.WebServer.1.2.1\lib\nanoFramework.WebServer.dll</HintPath>
56-
<Private>True</Private>
54+
<Reference Include="nanoFramework.WebServer">
55+
<HintPath>packages\nanoFramework.WebServer.1.2.3\lib\nanoFramework.WebServer.dll</HintPath>
5756
</Reference>
5857
<Reference Include="System.Device.Wifi, Version=1.5.65.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
5958
<HintPath>packages\nanoFramework.System.Device.Wifi.1.5.65\lib\System.Device.Wifi.dll</HintPath>
@@ -75,14 +74,6 @@
7574
<HintPath>packages\nanoFramework.System.Threading.1.1.19\lib\System.Threading.dll</HintPath>
7675
<Private>True</Private>
7776
</Reference>
78-
<Reference Include="Windows.Storage, Version=1.5.33.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
79-
<HintPath>packages\nanoFramework.Windows.Storage.1.5.33\lib\Windows.Storage.dll</HintPath>
80-
<Private>True</Private>
81-
</Reference>
82-
<Reference Include="Windows.Storage.Streams, Version=1.14.24.46541, Culture=neutral, PublicKeyToken=c07d481e9758c731">
83-
<HintPath>packages\nanoFramework.Windows.Storage.Streams.1.14.24\lib\Windows.Storage.Streams.dll</HintPath>
84-
<Private>True</Private>
85-
</Reference>
8677
</ItemGroup>
8778
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
8879
<ProjectExtensions>

samples/Webserver/WebServer.DI/packages.config

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@
1010
<package id="nanoFramework.System.Net.Http.Server" version="1.5.104" targetFramework="netnano1.0" />
1111
<package id="nanoFramework.System.Text" version="1.2.37" targetFramework="netnano1.0" />
1212
<package id="nanoFramework.System.Threading" version="1.1.19" targetFramework="netnano1.0" />
13-
<package id="nanoFramework.WebServer" version="1.2.1" targetFramework="netnano1.0" />
14-
<package id="nanoFramework.Windows.Storage" version="1.5.33" targetFramework="netnano1.0" />
15-
<package id="nanoFramework.Windows.Storage.Streams" version="1.14.24" targetFramework="netnano1.0" />
13+
<package id="nanoFramework.WebServer" version="1.2.3" targetFramework="netnano1.0" />
1614
</packages>

samples/Webserver/WebServer.GpioRest/WebServer.GpioRest.nfproj

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
<HintPath>packages\nanoFramework.System.Text.1.2.37\lib\nanoFramework.System.Text.dll</HintPath>
4444
<Private>True</Private>
4545
</Reference>
46-
<Reference Include="nanoFramework.WebServer, Version=1.2.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
47-
<HintPath>packages\nanoFramework.WebServer.1.2.1\lib\nanoFramework.WebServer.dll</HintPath>
48-
<Private>True</Private>
46+
<Reference Include="nanoFramework.WebServer">
47+
<HintPath>packages\nanoFramework.WebServer.1.2.3\lib\nanoFramework.WebServer.dll</HintPath>
4948
</Reference>
5049
<Reference Include="System.Device.Gpio, Version=1.1.28.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
5150
<HintPath>packages\nanoFramework.System.Device.Gpio.1.1.28\lib\System.Device.Gpio.dll</HintPath>
@@ -75,14 +74,6 @@
7574
<HintPath>packages\nanoFramework.System.Threading.1.1.19\lib\System.Threading.dll</HintPath>
7675
<Private>True</Private>
7776
</Reference>
78-
<Reference Include="Windows.Storage, Version=1.5.33.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
79-
<HintPath>packages\nanoFramework.Windows.Storage.1.5.33\lib\Windows.Storage.dll</HintPath>
80-
<Private>True</Private>
81-
</Reference>
82-
<Reference Include="Windows.Storage.Streams, Version=1.14.24.46541, Culture=neutral, PublicKeyToken=c07d481e9758c731">
83-
<HintPath>packages\nanoFramework.Windows.Storage.Streams.1.14.24\lib\Windows.Storage.Streams.dll</HintPath>
84-
<Private>True</Private>
85-
</Reference>
8677
</ItemGroup>
8778
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
8879
<ProjectExtensions>

samples/Webserver/WebServer.GpioRest/packages.config

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@
1111
<package id="nanoFramework.System.Net.Http.Server" version="1.5.104" targetFramework="netnano1.0" />
1212
<package id="nanoFramework.System.Text" version="1.2.37" targetFramework="netnano1.0" />
1313
<package id="nanoFramework.System.Threading" version="1.1.19" targetFramework="netnanoframework10" />
14-
<package id="nanoFramework.WebServer" version="1.2.1" targetFramework="netnano1.0" />
15-
<package id="nanoFramework.Windows.Storage" version="1.5.33" targetFramework="netnano1.0" />
16-
<package id="nanoFramework.Windows.Storage.Streams" version="1.14.24" targetFramework="netnano1.0" />
14+
<package id="nanoFramework.WebServer" version="1.2.3" targetFramework="netnano1.0" />
1715
</packages>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>{129CD942-C541-4E30-A605-9AB688E92800}</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>WebServer.Sample</RootNamespace>
16+
<AssemblyName>WebServer.Sample</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
<DefineConstants>$(DefineConstants);HAS_WIFI;HAS_STORAGE;</DefineConstants>
19+
</PropertyGroup>
20+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
21+
<ItemGroup>
22+
<Compile Include="..\WebServer.Sample\ControllerAuth.cs" >
23+
<Link>ControllerAuth.cs</Link>
24+
</Compile>
25+
<Compile Include="..\WebServer.Sample\ControllerPerson.cs" >
26+
<Link>ControllerPerson.cs</Link>
27+
</Compile>
28+
<Compile Include="..\WebServer.Sample\ControllerTest.cs" >
29+
<Link>ControllerTest.cs</Link>
30+
</Compile>
31+
<Compile Include="..\WebServer.Sample\Program.cs" >
32+
<Link>Program.cs</Link>
33+
</Compile>
34+
<Compile Include="Properties\AssemblyInfo.cs" />
35+
<Compile Include="Resources.Designer.cs">
36+
<DesignTime>True</DesignTime>
37+
<AutoGen>True</AutoGen>
38+
<DependentUpon>Resources.resx</DependentUpon>
39+
</Compile>
40+
</ItemGroup>
41+
<ItemGroup>
42+
<EmbeddedResource Include="Resources.resx">
43+
<Generator>nFResXFileCodeGenerator</Generator>
44+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
45+
</EmbeddedResource>
46+
</ItemGroup>
47+
<ItemGroup>
48+
<None Include="packages.config" />
49+
<None Include="Resources\favicon.ico" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<Reference Include="mscorlib, Version=1.14.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
53+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
54+
<Private>True</Private>
55+
</Reference>
56+
<Reference Include="nanoFramework.ResourceManager, Version=1.2.13.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
57+
<HintPath>..\packages\nanoFramework.ResourceManager.1.2.13\lib\nanoFramework.ResourceManager.dll</HintPath>
58+
<Private>True</Private>
59+
</Reference>
60+
<Reference Include="nanoFramework.Runtime.Events, Version=1.11.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
61+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
62+
<Private>True</Private>
63+
</Reference>
64+
<Reference Include="nanoFramework.System.Collections, Version=1.5.18.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
65+
<HintPath>..\packages\nanoFramework.System.Collections.1.5.18\lib\nanoFramework.System.Collections.dll</HintPath>
66+
<Private>True</Private>
67+
</Reference>
68+
<Reference Include="nanoFramework.System.Text, Version=1.2.37.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
69+
<HintPath>..\packages\nanoFramework.System.Text.1.2.37\lib\nanoFramework.System.Text.dll</HintPath>
70+
<Private>True</Private>
71+
</Reference>
72+
<Reference Include="nanoFramework.WebServer">
73+
<HintPath>..\packages\nanoFramework.WebServer.FileSystem.1.2.3\lib\nanoFramework.WebServer.dll</HintPath>
74+
</Reference>
75+
<Reference Include="System.Device.Gpio, Version=1.1.28.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
76+
<HintPath>..\packages\nanoFramework.System.Device.Gpio.1.1.28\lib\System.Device.Gpio.dll</HintPath>
77+
<Private>True</Private>
78+
</Reference>
79+
<Reference Include="System.Device.Wifi">
80+
<HintPath>..\packages\nanoFramework.System.Device.Wifi.1.5.65\lib\System.Device.Wifi.dll</HintPath>
81+
</Reference>
82+
<Reference Include="System.IO.FileSystem">
83+
<HintPath>..\packages\nanoFramework.System.IO.FileSystem.1.1.23\lib\System.IO.FileSystem.dll</HintPath>
84+
</Reference>
85+
<Reference Include="System.IO.Streams, Version=1.1.38.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
86+
<HintPath>..\packages\nanoFramework.System.IO.Streams.1.1.38\lib\System.IO.Streams.dll</HintPath>
87+
<Private>True</Private>
88+
</Reference>
89+
<Reference Include="System.Net, Version=1.10.62.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
90+
<HintPath>..\packages\nanoFramework.System.Net.1.10.62\lib\System.Net.dll</HintPath>
91+
<Private>True</Private>
92+
</Reference>
93+
<Reference Include="System.Net.Http, Version=1.5.104.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
94+
<HintPath>..\packages\nanoFramework.System.Net.Http.Server.1.5.104\lib\System.Net.Http.dll</HintPath>
95+
<Private>True</Private>
96+
</Reference>
97+
<Reference Include="System.Threading, Version=1.1.19.33722, Culture=neutral, PublicKeyToken=c07d481e9758c731">
98+
<HintPath>..\packages\nanoFramework.System.Threading.1.1.19\lib\System.Threading.dll</HintPath>
99+
<Private>True</Private>
100+
</Reference>
101+
</ItemGroup>
102+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
103+
<ProjectExtensions>
104+
<ProjectCapabilities>
105+
<ProjectConfigurationsDeclaredAsItems />
106+
</ProjectCapabilities>
107+
</ProjectExtensions>
108+
</Project>

0 commit comments

Comments
 (0)