Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to make RESTful WebBroker using C++Builder
//---------------------------------------------------------------------------
#include "Main_WebModuleUnit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TComponentClass WebModuleClass = __classid(TWebModule1);
//---------------------------------------------------------------------------
__fastcall TWebModule1::TWebModule1(TComponent* Owner)
: TWebModule(Owner)
{
FDConnection1->Connected = true;
}
//---------------------------------------------------------------------------
void __fastcall TWebModule1::WebModule1DefaultHandlerAction(TObject *Sender, TWebRequest *Request,
TWebResponse *Response, bool &Handled)
{
//Main function.
//Convert Request->PathInfo to std::vector.
auto v_path = path_to_vector(Request->PathInfo);
(v_path.size() > 0)?
//If the resource name exists, call the function that has been registered.
(resource_action_.at(*(v_path.begin())))(Sender, Request, Response, Handled, v_path):
Response->Content =
"<html>"
"<head><title>Web Server Application</title></head>"
"<body>Web Server Application</body>"
"</html>";
}
//---------------------------------------------------------------------------
std::vector<UnicodeString> __fastcall TWebModule1::path_to_vector(String path_)
{
std::vector<UnicodeString> vector_path;
std::wstringstream wss{path_.w_str()};
std::wstring path_item;
while (getline(wss, path_item , L'/'))
if (!path_item.empty())
{
vector_path.push_back(path_item.c_str());
}
return vector_path;
}
void __fastcall TWebModule1::WebModule1WebActionResource1(
TObject *Sender, TWebRequest *Request,
TWebResponse *Response, bool &Handled, std::vector<UnicodeString>& path_)
{
std::unique_ptr<TJSONObject> jo_{std::make_unique<TJSONObject>()};
if (path_.size() > 1) {
auto pos_ = StrToIntDef(path_.at(1), 0);
FDQuery1->SQL->Text = Format("select * from t_MotoGP where position = %d", ARRAYOFCONST((pos_)));
FDQuery1->Active = true;
while (! FDQuery1->Eof)
{
jo_->AddPair("racer_name", FDQuery1->FieldByName("racer_name")->AsString);
FDQuery1->Next();
}
FDQuery1->Active = false;
}
Response->Content = jo_->ToJSON();
}
void __fastcall TWebModule1::WebModule1WebActionResource2(
TObject *Sender, TWebRequest *Request,
TWebResponse *Response, bool &Handled, std::vector<UnicodeString>& path_)
{
std::unique_ptr<TJSONArray> ja_{std::make_unique<TJSONArray>()};
TJSONObject* jo_;
int iCount = 0;
for (auto s: path_)
{
jo_ = new TJSONObject();
jo_->AddPair(Format("item_%d", ARRAYOFCONST((iCount))), s);
ja_->AddElement(jo_);
iCount++;
}
Response->Content = ja_->ToString();
}
//---------------------------------------------------------------------------
#ifndef Main_WebModuleUnit1H
#define Main_WebModuleUnit1H
//---------------------------------------------------------------------------
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include <Web.HTTPApp.hpp>
#include <Data.DB.hpp>
#include <FireDAC.Comp.Client.hpp>
#include <FireDAC.Comp.DataSet.hpp>
#include <FireDAC.DApt.hpp>
#include <FireDAC.DApt.Intf.hpp>
#include <FireDAC.DatS.hpp>
#include <FireDAC.Phys.hpp>
#include <FireDAC.Phys.Intf.hpp>
#include <FireDAC.Phys.MySQL.hpp>
#include <FireDAC.Phys.MySQLDef.hpp>
#include <FireDAC.Stan.Async.hpp>
#include <FireDAC.Stan.Def.hpp>
#include <FireDAC.Stan.Error.hpp>
#include <FireDAC.Stan.Intf.hpp>
#include <FireDAC.Stan.Option.hpp>
#include <FireDAC.Stan.Param.hpp>
#include <FireDAC.Stan.Pool.hpp>
#include <FireDAC.UI.Intf.hpp>
#include <FireDAC.VCLUI.Wait.hpp>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <memory>
#include <System.Json.hpp>
//---------------------------------------------------------------------------
using TWebModuleHandlerAction = void __fastcall (__closure *)(
TObject *Sender, TWebRequest *Request, TWebResponse *Response, bool &Handled, std::vector<UnicodeString>& path_);
class TWebModule1 : public TWebModule
{
__published: // IDE-managed Components
TFDConnection *FDConnection1;
TFDQuery *FDQuery1;
TFDPhysMySQLDriverLink *FDPhysMySQLDriverLink1;
void __fastcall WebModule1DefaultHandlerAction(TObject *Sender, TWebRequest *Request,
TWebResponse *Response, bool &Handled);
private:
//Convert Request->PathInfo to std::vector
std::vector<UnicodeString> __fastcall path_to_vector(String path_);
//resource1
void __fastcall WebModule1WebActionResource1(TObject *Sender, TWebRequest *Request,
TWebResponse *Response, bool &Handled, std::vector<UnicodeString>& path_);
//resource2
void __fastcall WebModule1WebActionResource2(TObject *Sender, TWebRequest *Request,
TWebResponse *Response, bool &Handled, std::vector<UnicodeString>& path_);
//This std::map is resource management.
std::map<String, TWebModuleHandlerAction> resource_action_{
std::make_pair( "resource1", WebModule1WebActionResource1),
std::make_pair( "resource2", WebModule1WebActionResource2)
};
public:
__fastcall TWebModule1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TWebModule1 *WebModule1;
//---------------------------------------------------------------------------
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment