1 module app;
2 
3 import serverino;
4 import parserino;
5 import std;
6 
7 mixin ServerinoMain;
8 
9 
10 // Create a server with serverino on port 8080
11 // more info about serverino: https://github.com/trikko/serverino
12 @onServerInit ServerinoConfig configure()
13 {
14 	return ServerinoConfig
15 		.create()
16 		.addListener("0.0.0.0", 8282)
17 		.setWorkers(4);
18 }
19 
20 // Handle incoming requests
21 @endpoint @route!"/"
22 void dump(Request request, Output output)
23 {
24 	// Load the html file, template html created by https://www.shapingrain.com
25 	Document doc = Document("html/index.html".readText());
26 
27 	// Replace the title
28 	doc.byTagName("h1").frontOrThrow.innerText = "Parserino";
29 
30 	// Replace the description
31 	doc.byTagName("h2").frontOrThrow.innerText = "Hello from parserino! This is a simple example of how to use parserino to parse html files and replace their content.";
32 
33 	// Change button link to point to parserino github page
34 	doc.bySelector("a.button").frontOrThrow.setAttribute("href", "https://github.com/trikko/parserino");
35 
36 	// Remove the pricing section of the template
37 	doc.bySelector("section#pricing").frontOrThrow.remove();
38 
39 	// Remove all the social link except twitter
40 	// You should avoid removing elements while iterating over them with a range
41 	// but in this case it's safe because we are converting it to an array
42 	foreach(e; doc.bySelector("ul.social-icons > li").array)
43 	{
44 		if(e.firstChild.getAttribute("title") != "Twitter") e.remove();
45 		else e.firstChild.setAttribute("href", "https://twitter.com/twittatore");
46 	}
47 
48 	// Output the modified document
49 	output ~= doc;
50 
51 	// Try to navigate to http://localhost:8282
52 }
53 
54 // Serve static files
55 @endpoint
56 @route!(x => x.uri.startsWith("/css/"))	 	// Serve files in the css folder and ...
57 @route!(x => x.uri.startsWith("/js/"))		 	// ... in the js folder and ...
58 @route!(x => x.uri.startsWith("/fonts/"))  	// ... in the fonts folder and ...
59 @route!(x => x.uri.startsWith("/images/")) 	// ... in the images folder
60 void static_serve(Request request, Output output)
61 {
62 	output.serveFile("html" ~ request.uri);
63 }