Author: Andrea L59044
I used FireDAC because I wanted to make a DB connection to MySQL DB.
Resource and suffix can be obtained with Request->PathInfo.
So decompose PathInfo into std::vector<UnicodeString> and put it in.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//// 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; } |
We made each resource as a member function.
1 2 3 4 5 6 7 |
//// //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_); |
And I set it on the map.
1 2 3 4 5 6 |
//// //This std::map is resource management. std::map<String, TWebModuleHandlerAction>>resource_action_{ std::make_pair( "resource1", WebModule1WebActionResource1), std::make_pair( "resource2", WebModule1WebActionResource2) }; |
Call from the main action function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//// 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>" "<building>Web Server Application</building>" "</html>"; } |
Each resource implementation part.
It is the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//// 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>()}; UnicodeString out="none"; 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>()}; UnicodeString out; 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_->ToJSON(); } |
http://localhost:8080/resource1/1
Accessing the above URL goes through the function of WebModule1WebActionResource1.
This function connects to DB using FireDAC.
http://localhost:8080/resource2/japan/America/2017
The above URL goes through the function of WebModule1WebActionResource2.
This is just a function to echo the item.
Two resource functions are returned by JSON.
[gist]
https://gist.github.com/mojeld/63592be8f6c371116f2145b37c2a54aa
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition