Yet Another Web Server
Not just Web Server.
http://localhost:8899/dashboard
let define a HelloWorldApp:
namespace App {
class HelloWorldApp : public WebApplication {
public:
void Init();
public:
Http::HandlerResponse sayHelloWorld(Http::HttpRequest request);
};
}
add app and attach app to server in AppConfig::AttachApps:
void App::AppConfig::AttachApps(std::shared_ptr<Http::HttpServer> httpServer) {
App::HelloWorldApp helloWorldApp;
helloWorldApp.SetServer(httpServer);
helloWorldApp.Init();
}
add route in App's Init method:
void App::HelloWorldApp::Init() {
this->AddRoute({"/helloworld", Http::GET}, std::bind(&HelloWorldApp::sayHelloWorld, this, std::placeholders::_1));
}
implement sayHelloWorld function:
Http::HandlerResponse App::HelloWorldApp::sayHelloWorld(Http::HttpRequest request) {
return {Http::OK,"hello, world!"};
}
can render html template in controller:
template define: add a html file in /www/html directory,like:
helloworld.html
<html lang="html">
<head>
<title>HelloWorldApp</title>
</head>
<body>
<p>{{ helloworld }}</p>
</body>
</html>
and use it in controller:
Http::HandlerResponse App::HelloWorldApp::sayHelloWorld(Http::HttpRequest request) {
Loader::FileLoader fileLoader;
Template::TemplateEngine templateEngine(fileLoader);
try {
templateEngine.Load("www/html/helloworld.html");
templateEngine.Set("helloworld", "hello, world!");
return {Http::OK, templateEngine.RenderToText()};
} catch (std::logic_error error) {
return {Http::OK, error.what()};
}
}
more complex template plsease see the code.