Skip to content

Commit ae0fa1c

Browse files
committed
update documentation
1 parent 890ccdf commit ae0fa1c

15 files changed

+1817
-23
lines changed

documentation/docs/.vitepress/config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export default defineConfig({
88
description: "Simple Web server library in .NET Core",
99
head: [
1010
['link', { rel: 'icon', href: '/SimpleW/favicon.ico' }]
11+
['script', { async: '', src: 'https://www.googletagmanager.com/gtag/js?id=GTM-PBQ3XQSQ' } ],
12+
['script', {}, `window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'GTM-PBQ3XQSQ');`]
1113
],
1214
themeConfig: {
1315
// https://vitepress.dev/reference/default-theme-config
@@ -74,6 +76,9 @@ export default defineConfig({
7476
},
7577

7678
},
79+
sitemap: {
80+
hostname: 'https://stratdev3.github.io'
81+
},
7782
ignoreDeadLinks: [
7883
// ignore all localhost links
7984
/^https?:\/\/localhost/,

documentation/docs/advanced.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1-
# Return Data
1+
# Advanced
2+
3+
4+
#### Catch All Maintenance Page
5+
6+
You can setup a maintenance page to catch all api call by using the wildcard in a ```RouteAttribute```.
7+
8+
```csharp:line-numbers
9+
using System;
10+
using System.Net;
11+
using SimpleW;
12+
13+
namespace Sample {
14+
class Program {
15+
16+
static void Main() {
17+
18+
// listen to all IPs port 2015
19+
var server = new SimpleWServer(IPAddress.Any, 2015);
20+
21+
// need by MaintenanceController wildcard route parameter
22+
server.Router.RegExpEnabled = true;
23+
// add the dedidacted controller
24+
server.AddDynamicContent(typeof(MaintenanceController), "/api/v1");
25+
26+
server.Start();
27+
28+
Console.WriteLine("server started at http://localhost:2015/");
29+
30+
// block console for debug
31+
Console.ReadKey();
32+
33+
}
34+
}
35+
36+
// inherit from Controller to target a class
37+
public class MaintenanceController : Controller {
38+
39+
// wildcard route parameter will call all string under root api
40+
[Route("GET", "/*")]
41+
public object Maintenance() {
42+
return Response.MakeErrorResponse(503, "Maintenance");
43+
}
44+
45+
}
46+
47+
}
48+
```
249

3-
blabla

documentation/docs/api-basic-example.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Basic Exemple
1+
# Basic Example
22

33

44
The RestAPI is based on **routes**, so just add a `RouteAttribute` to target **methods** of a `Controller` base class.<br />
55
The return is serialized into json and sent as a response to the client.
66

77
Use `server.AddDynamicContent()` to handle RestAPI.
88

9-
```csharp
9+
```csharp:line-numbers
1010
using System;
1111
using System.Net;
1212
using SimpleW;

documentation/docs/api-cors.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
11
# CORS
22

3-
blabla
3+
4+
Internet Browser (Firefox, Chrome, IE...) blocks javascript requesting RestAPI from a different domain. That's why [CORS](https://developer.mozilla.org/fr/docs/Web/HTTP/CORS) was created, to define permission and sharing data.
5+
6+
To set CORS policy, use the `server.AddCORS()` method :
7+
8+
```csharp:line-numbers
9+
using System;
10+
using System.Net;
11+
using SimpleW;
12+
13+
namespace Sample {
14+
class Program {
15+
16+
static void Main() {
17+
var server = new SimpleWServer(IPAddress.Any, 2015);
18+
server.AddDynamicContent("/api");
19+
20+
// set CORS policy
21+
server.AddCORS(
22+
"*", // Access-Control-Allow-Origin
23+
"*", // Access-Control-Allow-Headers
24+
"GET,POST,OPTIONS", // Access-Control-Allow-Methods
25+
"true" // Access-Control-Allow-Credentials
26+
);
27+
28+
server.Start();
29+
Console.WriteLine("server started at http://localhost:2015/");
30+
Console.ReadKey();
31+
}
32+
}
33+
34+
public class SomeController : Controller {
35+
36+
[Route("GET", "/test")]
37+
public object SomePublicMethod() {
38+
return new {
39+
message = "Hello World !"
40+
};
41+
}
42+
43+
}
44+
45+
}
46+
```

documentation/docs/api-hook.md

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,152 @@
11
# Hook
22

3-
blabla
3+
4+
There are some places where SimpleW behavior can be overridden.
5+
6+
#### OnBeforeMethod
7+
8+
The `Controller` class contains an abstract method `OnBeforeMethod()` which is called before any method execution.
9+
10+
```csharp:line-numbers
11+
using System;
12+
using System.Net;
13+
using SimpleW;
14+
15+
namespace Sample {
16+
17+
class Program {
18+
static void Main() {
19+
var server = new SimpleWServer(IPAddress.Any, 2015);
20+
server.AddDynamicContent("/api");
21+
server.Start();
22+
Console.WriteLine("server started at http://localhost:2015/");
23+
Console.ReadKey();
24+
}
25+
}
26+
27+
[Route("/test")]
28+
public class TestController : Controller {
29+
30+
// Router will call this methods before another one
31+
public override void OnBeforeMethod() {
32+
Console.WriteLine("OnBeforeMethod()");
33+
}
34+
35+
[Route("GET", "/index")]
36+
public object Index() {
37+
return "test index page";
38+
}
39+
40+
[Route("POST", "/create")]
41+
public object Create() {
42+
return "test create success";
43+
}
44+
45+
}
46+
47+
}
48+
```
49+
50+
51+
#### Subclass
52+
53+
A better approach for adding some logic code to all your controllers is by extending the `Controller` class.
54+
55+
Example using a `BaseController` class that contains common code to all controllers.
56+
57+
```csharp:line-numbers
58+
using System;
59+
using System.Net;
60+
using SimpleW;
61+
62+
namespace Sample {
63+
64+
class Program {
65+
static void Main() {
66+
var server = new SimpleWServer(IPAddress.Any, 2015);
67+
server.AddDynamicContent("/api");
68+
server.Start();
69+
Console.WriteLine("server started at http://localhost:2015/");
70+
Console.ReadKey();
71+
}
72+
}
73+
74+
class BaseController : Controller {
75+
76+
// example of property used in subclass
77+
protected Repository _repo = new();
78+
79+
}
80+
81+
[Route("/user")]
82+
class UserController : BaseController {
83+
84+
[Route("GET", "/index")]
85+
public object Index() {
86+
var users = _repo.GetAll<User>();
87+
return users;
88+
}
89+
90+
}
91+
92+
[Route("/department")]
93+
class DepartmentController : BaseController {
94+
95+
[Route("GET", "/index")]
96+
public object Index() {
97+
var departments = _repo.GetAll<Department>();
98+
return departments;
99+
}
100+
101+
}
102+
103+
}
104+
```
105+
106+
The subclass can contains route's method too. To avoid this subclass being parsed by the router, it must be excluded. Use the `AddDynamicContent()` exclude parameter.
107+
108+
```csharp:line-numbers
109+
using System;
110+
using System.Net;
111+
using SimpleW;
112+
113+
namespace Sample {
114+
115+
class Program {
116+
static void Main() {
117+
var server = new SimpleWServer(IPAddress.Any, 2015);
118+
119+
// exclude BaseController as a regular Controller
120+
server.AddDynamicContent("/api", new Type[] { typeof(BaseController) });
121+
122+
server.Start();
123+
Console.WriteLine("server started at http://localhost:2015/");
124+
Console.ReadKey();
125+
}
126+
}
127+
128+
class BaseController : Controller {
129+
130+
[Route("GET", "/conf")]
131+
public object Conf() {
132+
return "conf";
133+
}
134+
135+
}
136+
137+
[Route("/user")]
138+
class UserController : BaseController {
139+
140+
}
141+
142+
[Route("/department")]
143+
class DepartmentController : BaseController {
144+
145+
}
146+
147+
}
148+
```
149+
150+
Note : the method `BaseController.Conf()` with its `Route` attribute is shared across all controllers. It can be access through :
151+
- http://localhost:2015/api/user/conf
152+
- http://localhost:2015/api/department/conf

0 commit comments

Comments
 (0)