Skip to content

Commit 4fa8fc8

Browse files
committed
change README to the new builder system
1 parent 5946144 commit 4fa8fc8

File tree

1 file changed

+71
-101
lines changed

1 file changed

+71
-101
lines changed

README.md

+71-101
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
# MaxLib.WebServer
22

3-
[![.NET Core](https://github.com/Garados007/MaxLib.WebServer/workflows/.NET%20Core/badge.svg?branch=main)](https://github.com/Garados007/MaxLib.WebServer/actions?query=workflow%3A%22.NET+Core%22)
4-
[![NuGet Publish](https://github.com/Garados007/MaxLib.WebServer/workflows/NuGet%20Publish/badge.svg)](https://www.nuget.org/packages?q=Garados007+MaxLib.WebServer)
5-
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Garados007/MaxLib.WebServer/blob/master/LICENSE)
6-
[![Current Version](https://img.shields.io/github/tag/garados007/MaxLib.WebServer.svg?label=release)](https://github.com/Garados007/MaxLib.WebServer/releases)
3+
[![.NET
4+
Core](https://github.com/Garados007/MaxLib.WebServer/workflows/.NET%20Core/badge.svg?branch=main)](https://github.com/Garados007/MaxLib.WebServer/actions?query=workflow%3A%22.NET+Core%22)
5+
[![NuGet
6+
Publish](https://github.com/Garados007/MaxLib.WebServer/workflows/NuGet%20Publish/badge.svg)](https://www.nuget.org/packages?q=Garados007+MaxLib.WebServer)
7+
[![License:
8+
MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Garados007/MaxLib.WebServer/blob/master/LICENSE)
9+
[![Current
10+
Version](https://img.shields.io/github/tag/garados007/MaxLib.WebServer.svg?label=release)](https://github.com/Garados007/MaxLib.WebServer/releases)
711
![Top Language](https://img.shields.io/github/languages/top/garados007/MaxLib.WebServer.svg)
812

9-
`MaxLib.WebServer` is a full web server written in C#. To use this webserver you only need to add the .NuGet package to your project, instantiate the server in your code and thats it. No special configuration files or multiple processes.
13+
`MaxLib.WebServer` is a full web server written in C#. To use this webserver you only need to add
14+
the .NuGet package to your project, instantiate the server in your code and thats it. No special
15+
configuration files or multiple processes.
1016

11-
This web server is build modular. Any modules can be replaced by you and you decide what the server is cabable of. The server can fully configured (including exchanging the modules) during runtime - no need to restart everything.
17+
This web server is build modular. Any modules can be replaced by you and you decide what the server
18+
is capable of. The server can fully configured (including exchanging the modules) during runtime -
19+
no need to restart everything.
1220

1321
Some of the current features of the web server are:
1422
- HTTP web server (of course, that's the purpose of the project)
1523
- HTTPS web server with SSL certificates
16-
- HTTP and HTTPS web server on the same port. The server detects automaticly what the user intends to use.
24+
- HTTP and HTTPS web server on the same port. The server detects automatically what the user intends
25+
to use.
1726
- Asynchronous handling of requests. Every part of the pipeline works with awaitable Tasks.
1827
- REST Api builder. You can directly bind your methods to the handlers.
19-
- Chunked transport. The server understands chunked datastreams and can produce these.
20-
- Lazy handling of requests. The server allows you to produce the content while you are sending the response. No need to wait.
28+
- Chunked transport. The server understands chunked data streams and can produce these.
29+
- Lazy handling of requests. The server allows you to produce the content while you are sending the
30+
response. No need to wait.
2131
- Work with components that belongs to another AppDomain with Marshaling.
2232
- Deliver contents from your local drive (e.g. HDD)
2333
- Session keeping. You can identify the user later.
@@ -41,132 +51,88 @@ Write somewhere in your code (e.g.) in your Main method of your programm the fol
4151

4252
```csharp
4353
using MaxLib.WebServer;
44-
using MaxLib.WebServer.Services;
54+
using MaxLib.WebServer.Builder; // this using is used later in this tutorial
4555
46-
// in your code
47-
void SetupServer()
48-
{
49-
// this expects that server is a variable that you have defined in your class
50-
server = new Server(new WebServerSettings(
51-
8000, // this will run the server on port 8000
52-
5000 // set the timout to 5 seconds.
53-
));
54-
55-
// now add some services. You can use your own implementations but here we
56-
// will add a basic set of services from MaxLib.WebServer.Services.
57-
58-
// this will read the request from the network stream
59-
server.AddWebService(new HttpHeaderParser());
60-
// this will read the header information and prepare them for later usage.
61-
server.AddWebService(new HttpHeaderPostParser());
62-
// this will take care of HTTP OPTIONS or HEAD requests
63-
server.AddWebService(new HttpHeaderSpecialAction());
64-
// this will serve 404 responses if no service has created a content for the request
65-
server.AddWebService(new Http404Service());
66-
// this will prepare the response headers before everything will be send to the user
67-
server.AddWebService(new HttpResponseCreator());
68-
// this will send the response to the user
69-
server.AddWebService(new HttpSender());
70-
71-
// the server can now be startet. A basic set of services is defined so a new
72-
// request will be handled and the user gets a response. Right now its a
73-
// 404 NOT FOUND but we will add more.
74-
server.Start();
75-
76-
// if you don't need the server anymore you can close the server with
77-
server.Stop();
78-
}
56+
// ...
57+
58+
using var server = new Server(new WebServerSettings(
59+
8000, // this will run the server on port 8000
60+
5000 // set the timout to 5 seconds.
61+
));
62+
63+
// now add some services. You can use your own implementations but here we
64+
// will add a basic set of services from MaxLib.WebServer.Services.
65+
server.InitDefault();
7966

67+
// the server can now be started. A basic set of services is defined so a new
68+
// request will be handled and the user gets a response. Right now its a
69+
// 404 NOT FOUND but we will add more.
70+
server.Start();
71+
72+
// if you want to stop the server but don't release any of its resources you can do it with:
73+
server.Stop();
8074
```
8175

82-
Right now you can start the server and open the url [http://localhost:8000](http://localhost:8000) in your browser and will get a nice 404 response.
76+
Right now you can start the server and open the url [http://localhost:8000](http://localhost:8000)
77+
in your browser and will get a nice 404 response.
8378

8479
### Create own service
8580

86-
Now we will create our own service, that will responds with a beatiful "Hello World" message.
81+
Now we will create our own service, that will responds with a beautiful "Hello World" message.
8782

88-
Create a new class `HelloWorldService` and put this code in it:
83+
Create a new class `MyServices` and put this code in it:
8984

9085
```csharp
9186
using System;
9287
using System.Threading.Tasks;
9388
using MaxLib.WebServer;
94-
using MaxLib.WebServer.Services;
89+
using MaxLib.WebServer.Builder;
9590

96-
// every service needs to be derived from WebService
97-
public class HelloWorldService : WebService
91+
// every service that uses the included Builder system needs to be derived from Service
92+
public class MyServices : Service
9893
{
99-
public HelloWorldService()
100-
// This will tell the server when this service should be executed.
101-
: base(ServerStage.CreateDocument)
94+
// this method should listen on /hello and return a nice hello
95+
[Path("/hello")]
96+
public string Hello()
10297
{
103-
// This tells the priority this service will be executed in the current stage.
104-
// right now we want the default normal priority.
105-
Importance = WebProgressImportance.Normal; // optional
106-
}
107-
108-
// the server asks every service in the current stage if they can do something
109-
// with the current request. Right now we only want to act if the url is
110-
// "/hello". This needs to be checked here.
111-
public override bool CanWorkWith(WebProgressTask task)
112-
{
113-
// IsUrl checks if the path is "/hello" or "/hello/".
114-
return task.Request.Location.IsUrl(new[] { "hello" });
115-
// If you want to check for "/hello/world" you need to call:
116-
// return task.Request.Location.IsUrl(new[] { "hello", "world" });
117-
}
118-
119-
// this function will be called from the server only if CanWorkWith succeeds.
120-
// Here we create our response
121-
public override async Task ProgressTask(WebProgressTask task)
122-
{
123-
// Our response. In this case a simple html page.
124-
var text = "<html><head><title>Hello World</title></head>" +
98+
return "<html><head><title>Hello World</title></head>" +
12599
"<body><h1>Hello World!</h1></body></html>";
126-
// now we add the result to the output. We can add any kind of data
127-
// source. This library has the helper classes for strings, Streams
128-
// and files.
129-
task.Document.DataSources.Add(new HttpStringDataSource(text)
130-
{
131-
// this will specify the Mime-Type as "text/html". The static
132-
// class MimeType contains many definitions but you can use your
133-
// own here if you want. The default Mime-Type is "text/plain".
134-
MimeType = MimeType.TextHtml,
135-
// you can specify your encoding here. Default is "utf-8".
136-
TextEncoding = "utf-8",
137-
});
138-
// we are now finished
139-
await Task.CompletedTask.ConfigureAwait(false);
140100
}
141101
}
142102
```
143103

144104
Now you need to add this line to add your service to server:
145105

146106
```csharp
147-
server.AddWebService(new HelloWorldService());
107+
server.AddWebService(Service.Build<MyServices>()!);
148108
```
149109

150-
After that you can run your programm and open the page [http://localhost:8000/hello](http://localhost:8000/hello). You will see your hello world message.
110+
After that you can run your programm and open the page
111+
[http://localhost:8000/hello](http://localhost:8000/hello). You will see your hello world message.
112+
113+
> More information about the new builder system can be found [here](https://github.com/Garados007/MaxLib.WebServer/wiki/Builder-System)
151114
152115
## Example
153116

154117
- [example/MaxLib.WebServer.Example](example/MaxLib.WebServer.Example)
155-
- create a basic webserver
118+
- create a basic webserver
156119

157120
## Contributing
158121

159-
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
122+
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process
123+
for submitting pull requests to us.
160124

161125
## Versioning
162126

163-
We use [SemVer](semver.org) for versioning. For the versions available, see the [tags on this repository](https://github.com/Garados007/MaxLib.WebServer/tags).
127+
We use [SemVer](semver.org) for versioning. For the versions available, see the [tags on this
128+
repository](https://github.com/Garados007/MaxLib.WebServer/tags).
164129

165130
## Authors
166131

167132
- **Max Brauer** - *Initial work* - [Garados007](https://github.com/Garados007)
168133

169-
See also the list of [contributors](https://github.com/Garados007/srpc/contributors) who participated in this project.
134+
See also the list of [contributors](https://github.com/Garados007/srpc/contributors) who
135+
participated in this project.
170136

171137
## Lincense
172138

@@ -176,15 +142,19 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.m
176142

177143
- StackOverflow for the help
178144
- Wikipedia, SelfHTML and Mozilla for their documentation
179-
- [PurpleBooth](https://github.com/PurpleBooth) for her [README.md](https://gist.github.com/PurpleBooth/109311bb0361f32d87a2) template
145+
- [PurpleBooth](https://github.com/PurpleBooth) for her
146+
[README.md](https://gist.github.com/PurpleBooth/109311bb0361f32d87a2) template
180147

181148
## Last Words
182149

183-
This project was a free time project of mine and I have done it because why not. The source
184-
code was a long time a part of [MaxLib](https://github.com/Garados007/MaxLib) (a collection
185-
of other fun projects and code) but got its own repository for better maintenance.
150+
This project was a free time project of mine and I have done it because why not. The source code was
151+
a long time a part of [MaxLib](https://github.com/Garados007/MaxLib) (a collection of other fun
152+
projects and code) but got its own repository for better maintenance.
153+
154+
Some of the documentation inside the code is still in German and other things needs to be optimized.
186155

187-
Some of the documentation inside the code is still in German and other things needs to be
188-
optimized.
156+
I have used this for some projects with my friends. It can handle some TB of traffic over a long
157+
period without any problems or crashes. I am a little proud of this.
189158

190-
I have used this for some projects with my friends. It can handle some TB of traffic over a long period without any problems or crashes. I am a little proud of this.
159+
I will further maintain and extend this library if I have new cool features in mind (this was the
160+
case for the full WebSocket support and easy Builder System).

0 commit comments

Comments
 (0)