Skip to content
/ YAWS Public

a Web Server & Web framework which written in c++ without any other libs

Notifications You must be signed in to change notification settings

nerososft/YAWS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YAWS

Yet Another Web Server

C/C++ CI

Not just Web Server.

Test

http://localhost:8899/dashboard

WebApp Demo

Define app

let define a HelloWorldApp:

namespace App {
    class HelloWorldApp : public WebApplication {
    public:
        void Init();
    public:
        Http::HandlerResponse sayHelloWorld(Http::HttpRequest request);
    };
}

Init

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();
}

Router

add route in App's Init method:

void App::HelloWorldApp::Init() {
    this->AddRoute({"/helloworld", Http::GET}, std::bind(&HelloWorldApp::sayHelloWorld, this, std::placeholders::_1));
}

Controller

implement sayHelloWorld function:

Http::HandlerResponse App::HelloWorldApp::sayHelloWorld(Http::HttpRequest request) {
    return {Http::OK,"hello, world!"};
}

Template Engine

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.

About

a Web Server & Web framework which written in c++ without any other libs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published