Skip to content

Commit 3065dea

Browse files
committed
Added more examples
1 parent 27a430f commit 3065dea

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

examples/Makefile.am

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
LDADD = $(top_builddir)/src/libhttpserver.la
2020
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
2121
METASOURCES = AUTO
22-
noinst_PROGRAMS = hello_world service
22+
noinst_PROGRAMS = hello_world service minimal_hello_world custom_error
2323

2424
hello_world_SOURCES = hello_world.cpp
2525
service_SOURCES = service.cpp
26-
26+
minimal_hello_world_SOURCES = minimal_hello_world.cpp
27+
custom_error_SOURCES = custom_error.cpp

examples/custom_error.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include <httpserver.hpp>
22+
23+
using namespace httpserver;
24+
25+
const std::shared_ptr<http_response> not_found_custom(const http_request& req)
26+
{
27+
return std::shared_ptr<string_response>(new string_response("Not found custom", 404, "text/plain"));
28+
}
29+
30+
const std::shared_ptr<http_response> not_allowed_custom(const http_request& req)
31+
{
32+
return std::shared_ptr<string_response>(new string_response("Not allowed custom", 405, "text/plain"));
33+
}
34+
35+
class hello_world_resource : public http_resource {
36+
public:
37+
const std::shared_ptr<http_response> render(const http_request&) {
38+
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
39+
}
40+
};
41+
42+
int main(int argc, char** argv) {
43+
webserver ws = create_webserver(8080)
44+
.not_found_resource(not_found_custom)
45+
.method_not_allowed_resource(not_allowed_custom);
46+
47+
hello_world_resource hwr;
48+
hwr.disallow_all();
49+
hwr.set_allowing("GET", true);
50+
ws.register_resource("/hello", &hwr);
51+
ws.start(true);
52+
53+
return 0;
54+
}

examples/minimal_hello_world.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include <httpserver.hpp>
22+
23+
using namespace httpserver;
24+
25+
class hello_world_resource : public http_resource {
26+
public:
27+
const std::shared_ptr<http_response> render(const http_request&) {
28+
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
29+
}
30+
};
31+
32+
int main(int argc, char** argv) {
33+
webserver ws = create_webserver(8080);
34+
35+
hello_world_resource hwr;
36+
ws.register_resource("/hello", &hwr);
37+
ws.start(true);
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)