NAWA 0.9
Web Application Framework for C++
aslibrary.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa 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
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24#include <iostream>
25#include <nawa/Exception.h>
28#include <nawa/logging/Log.h>
29
30using namespace nawa;
31using namespace std;
32
33int main() {
34
35 // You can do the initialization right here, no need for an init function.
36
37 // Load the config from a config.ini file, placed in the directory from where this app is executed.
38 // Of course, you could also provide an absolute path or let the user provide the location via a command line argument.
39 // The categories privileges, application, and system (except for the request handler) are irrelevant when using
40 // NAWA as a library.
41 Config config;
42 try {
43 config.read("config.ini");
44 } catch (Exception const& e) {
45 cerr << "Could not read config.ini file: " << e.getMessage() << endl;
46 }
47 // You don't have to use an ini file of course, you could also fill the config manually with all necessary options
48 // by using config.set(key, value).
49 // Also have a look at the nawa::config::Config docs for details :)
50
51 // Set up logging. Also have a look at the nawa::logging::Log docs for details, the static functions allow you
52 // to control logging globally. Output may also be redirected to a file.
53 Log::setOutputLevel(Log::Level::WARNING);
54 // Make sure to lock the stream after setting up logging in order to ensure thread safety.
55 Log::lockStream();
56
57 // Now we need a function which handles the requests. It can be any (Connection&) -> int function,
58 // here, we use a lambda for simplicity.
59 auto handlingFunction = [](Connection& connection) -> int {
60 // Our simple example app will just print "Hello World!" for every request
61 // and therefore do exactly the same thing as examples/helloworld.cpp
62 // (but in a more complicated way)
63 connection.responseStream() << "Hello World!";
64 return 0;
65 };
66
67 // set up the NAWA request handler
68 unique_ptr<RequestHandler> requestHandler;
69 try {
70 // The last argument is the concurrency, which is the number of worker threads for processing requests
71 requestHandler = RequestHandler::newRequestHandler(handlingFunction, config, 1);
72 } catch (Exception const& e) {
73 cerr << "NAWA request handler could not be created: " << e.getMessage() << endl;
74 return 1;
75 }
76
77 // At this point, you should consider doing a privilege downgrade (if your app is meant to be started as root), as
78 // running with root privileges is a security risk (as you hopefully know).
79
80 // If you want to use access filtering, create a set of filters like this (that's optional, of course):
81 AccessFilterList accessFilters;
82 // Add filters, as shown in the manual or examples/multipage.cpp
83 // ...
84 // Then pass them to the request handler:
85 requestHandler->setAccessFilters(accessFilters);
86
87 // Now we can start handling requests! Request handling will happen in separate threads, the start() function
88 // will return immediately, and you can continue to do other things here while requests are being served.
89 try {
90 requestHandler->start();
91 } catch (Exception const& e) {
92 cerr << "NAWA request handling could not be started: " << e.getMessage() << endl;
93 return 1;
94 }
95
96 // You can control request handling at any time by calling:
97 // requestHandler->stop() to stop accepting new requests and terminating the request handlers as soon as
98 // existing ones have been served
99 // requestHandler->terminate() to terminate request handling immediately
100 // None of these functions will block until termination has completed, only join() will ever block.
101 // Tip: Use signal handlers to control request handling.
102
103 // If you want to hot-swap the config, filters, or request handling function, use:
104 // requestHandler->reconfigure(handlingFunction, accessFilters, config);
105 // (see docs of nawa::RequestHandler::reconfigure() -- you can use std::nullopt for things you don't want to
106 // reconfigure)
107
108 // Now, before your main function returns, make sure to block until request handling has terminated:
109 requestHandler->join();
110
111 return 0;
112}
Response object to be passed back to NAWA and accessor to the request.
Exception class that can be used by apps to catch errors resulting from nawa function calls.
Simple class for (not (yet) thread-safe) logging to stderr or to any other output stream.
Handles and serves incoming requests via the NAWA app.
int main()
Definition: aslibrary.cpp:33
void read(std::string const &iniFile)
Definition: Config.cpp:58
virtual std::string getMessage() const noexcept
Definition: Exception.h:71
Definition: AppInit.h:31