Unix Sockets
Unix Domain Socket (UDS) can also be used as an entrypoint for the server. They are supported on : Linux, MacOS, Android... and even Windows !
With just a small change, the basic api example can also be served over a Unix socket.
csharp
using System;
using System.Net;
using System.Net.Sockets;
using SimpleW;
namespace Sample {
class Program {
static void Main() {
// unix socket
string unixSocketPath = @"C:\www\server.sock";
var server = new SimpleWServer(new UnixDomainSocketEndPoint(unixSocketPath));
// find all Controllers classes and serve on the "/api" endpoint
server.AddDynamicContent("/api");
// start non blocking background server
server.Start();
Console.WriteLine(@"server available on : unix:C:\www\server.sock");
// block console for debug
Console.ReadKey();
}
}
// inherit from Controller
public class SomeController : Controller {
// use the Route attribute to target a public method
[Route("GET", "/test")]
public object SomePublicMethod() {
// the Request property contains all data (Url, Headers...) from the client Request
var url = Request.Url;
// the return will be serialized to json and sent as response to client
return new {
message = Message()
};
}
private string Message() {
return "Hello World !";
}
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
You can use curl to test :
$ curl --unix-socket C:\www\test.sock http://localhost/api/test
> { "message" : "Hello World !" }There only one change :
- L13 : use the
SimpleWServer()constructor withUnixDomainSocketEndPointargument.
