1 /*
2 Copyright (c) 2023-2024 Andrea Fontana
3 
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 /++ Serverino is a small and ready-to-go http server, in D. It is multiplatform
27 + and compiles with DMD, LDC and GDC.
28 +
29 + The following example shows how to create a server that responds to all requests
30 + with the request dump, in a few lines of code.
31 + ---
32 + import serverino;
33 + mixin ServerinoMain;
34 + void hello(const Request req, Output output) { output ~= req.dump(); }
35 + ---
36 + `serverino.interfaces.Request` is the request object received, and `serverino.interfaces.Output` is the output you can write to.
37 +
38 + A more complete example is the following:
39 +
40 + ---
41 + import serverino;
42 + mixin ServerinoMain;
43 +
44 + @onServerInit ServerinoConfig setup()
45 + {
46 +    ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
47 +    sc.addListener("127.0.0.1", 8080);
48 +    sc.setWorkers(2);
49 +    // etc...
50 +
51 +    return sc;
52 + }
53 +
54 + @endpoint
55 + void dump(const Request req, Output output) { output ~= req.dump(); }
56 +
57 + @endpoint @priority(1)
58 + @route!"/hello"
59 + void hello(const Request req, Output output) { output ~= "Hello, world!"; }
60 + ---
61 +
62 + The function decorated with `serverino.config.onServerInit` is called when the server is initialized, and it is
63 + used to configure the server. It must return a `serverino.config.ServerinoConfig` object.
64 + In this example, the server is configured to listen on localhost:8080, with 2 workers.
65 +
66 + Every function decorated with `serverino.config.endpoint` is an endpoint. They are called in order of priority assigned with
67 + `serverino.config.priority` (default is 0). The first endpoint that write something to the output is the one that will respond to the request.
68 +
69 + The `serverino.config.route` attribute can be used to filter the requests that are passed to the endpoint, using a uri or a `bool delegate(Request r)` argument.
70 + In this example, only requests to `/hello` are passed to the `hello` endpoint. The `serverino.config.route` attribute can be used multiple times to specify multiple routes also using a delegate.
71 +/
72 module serverino;
73 
74 public import serverino.main;
75 public import serverino.config;
76 public import serverino.interfaces;