Project

General

Profile

Adding a REST API to my Wt web server

Added by George McFie over 2 years ago

I would like to implement a simple API on my existing Wt-based web server.
This would seem to imply that I need to intercept POST/GET requests for a specific URI (say /api) and handle those request/responses myself.

My difficulty is that I don't know how/where to intercept those requests before the 'standard' entry point code. My Wt::WServer subclass code looks something like this ...

//------------------------------------------------------------------------------------
class WebServer : public Wt::WServer
{
public:
WebServer (int argc, char* argv[])
: Wt::WServer (argc, argv, WTHTTP_CONFIGURATION)
{
Wt::WServer::addEntryPoint (Wt::EntryPointType::Application, CreateApplication);
}
virtual ~WebServer() override = default;

private:
WebServer (const WebServer&) = delete;
WebServer& operator= (const WebServer&) = delete;
};

std::unique_ptrWt::WApplication CreateApplication (const Wt::WEnvironment& environment)
{
auto application = Wt::cpp14::make_uniqueWt::WApplication (environment);

...

application->root()->addWidget (Wt::cpp14::make_unique (application.get(), environment);

return application;
}
//------------------------------------------------------------------------------------

From this, there doesn't seem to be any obvious way to implement my API code within the Wt code/context.

As a work around 'solution' I have implemented an entirely separate HTTP/socket listener on a separate port and thread, but this is clearly not a clean solution.
Is there a better way to do this?

Thank you in advance - any suggestions would be very welcome.


Replies (2)

RE: Adding a REST API to my Wt web server - Added by Marcelo Antunes over 2 years ago

int main(int argc, char **argv){
try {
Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
server.addEntryPoint(Wt::EntryPointType::Application, createApplication);
ResourceA aAPI;
server.addResource(&aAPI,"/somepath");

    server.run();

} catch (Wt::WServer::Exception& e) {
    std::cerr << e.what() << std::endl;
} catch (std::exception &e) {
    std::cerr << "exception: " << e.what() << std::endl;
}

}

An then ResourceA is a class extended from WResource

class ResourceA : public Wt::WResource
{
public:
ResourceA();
virtual void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response);
};

void ResourceA::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {

//use response.out()<<"some answer"; to return the answer request
//use request properties to filter the kind of request (post/get) and to get parameters/variables from url or from the body request
//if you are using wt::dbo, it integrates withe wt::json, sou you can directly serialize/deserialize jason from/to Wt::dbo collections
}

RE: Adding a REST API to my Wt web server - Added by George McFie over 2 years ago

Excellent - thank you very much.

On Thu, Nov 11, 2021 at 10:17 AM Emweb Redmine wt-redmine@emweb.be wrote:

http://redmine.emweb.be/boards/1/topics/17771?r=17772#message-17772
Marcelo Antunes

int main(int argc, char **argv){
try {
Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
server.addEntryPoint(Wt::EntryPointType::Application,
createApplication);
ResourceA aAPI;
server.addResource(&aAPI,"/somepath");

    server.run();

} catch (Wt::WServer::Exception& e) {
    std::cerr << e.what() << std::endl;
} catch (std::exception &e) {
    std::cerr << "exception: " << e.what() << std::endl;
}

}

An then ResourceA is a class extended from WResource

class ResourceA : public Wt::WResource
{
public:
ResourceA();
virtual void handleRequest(const Wt::Http::Request &request,
Wt::Http::Response &response);
};

void ResourceA::handleRequest(const Wt::Http::Request &request,
Wt::Http::Response &response) {

//use response.out()<<"some answer"; to return the answer request
//use request properties to filter the kind of request (post/get) and to
get parameters/variables from url or from the body request
//if you are using wt::dbo, it integrates withe wt::json, sou you can
directly serialize/deserialize jason from/to Wt::dbo collections
}

--
You have received this notification because you have either subscribed to
it, or are involved in it.
You can disable it by changing your account settings.

    (1-2/2)