Hints about HTTP REST service vs database
-
I hope I'm not off-topic here.
I'm writing an application that has a background Qt Console application and a front-end PHP web page.
If that matter, it runs on a custom Buildroot environment for Raspberry Pi 3, but my question can be related to any environment.The Qt application creates and updates some
QList
of custom struct, for example:typedef struct Song { Q_GADGET Q_PROPERTY(QString title MEMBER title) Q_PROPERTY(QString author MEMBER author) Q_PROPERTY(QString lyrics MEMBER lyrics) Q_PROPERTY(QString message MEMBER message) Q_PROPERTY(int rating MEMBER rating) Q_PROPERTY(QUrl cover MEMBER coverUrl) public: int id; QString file; QString title; QString author; QString lyrics; QString message; QString uuid; int rating; QImage cover; QUrl coverUrl; bool operator != (const Song &val) { if (val.title != this->title) return false; if (val.author != this->author) return false; if (val.lyrics != this->lyrics) return false; if (val.message != this->message) return false; if (val.rating != this->rating) return false; if (val.file != this->file) return false; return true; } } Song; Q_DECLARE_METATYPE(Song)
This
QList
is read/edited by the Qt/QML application but it should be read also by the PHP page.
I have two options, I've tried both but I'm not sure which one is better.a) Use a HTTP REST service in the Qt application
I have to maintain only one storage (theQList
) but the PHP page has to use a different port toGET
the list at run-time and all the stuff to process the data should be done in JavaScript.b) Use a database
The web page can fetch the data before create the page, but I have to keep in sync both theQList
and the db table.Any thoughts about these approaches? What would you suggest?
-
@Mark81 said in Hints about HTTP REST service vs database:
but I have to keep in sync both the QList
Why would you need a QList if you store the data in a database?
-
Hi,
Why would you need two ports to access a REST API from two different applications ?
In any case, can you explain your architecture ? The Qt console application with a PHP front end and QML in the mix sounds at bit surprising.
-
@SGaist I try to explain it better, sorry if it was not so clear.
I have:
- apache2 to serve PHP/HTML pages
- Qt application (I've just noticed I use the term "Console" improperly. I meant I don't use X server, but my application shows a QML page on the screen, though)
The Qt application handles communications with the external world (i.e. using
QSerialPort
, orQFile
) and maintain updated a "table" (currently theQList
).The web application fetch the PHP pages on port 80 from the webserver (apache2). In order to provide a REST API from the Qt application I had to create another HTTP server (in Qt) that listen to a different port (i.e. 8080). In this way, the page itself is retrieve with a
GET
on port 80, while the run-time data can be fetch with another GET on port 8080.I hope now it's more clear!
-
@SGaist exactly, but the same data is also shown on the QML pages. Here my confusion about which architecture is better.
QList
is easier for QML but database is easier for PHP.If I use both I have to keep them in sync, otherwise I have to "convert" them on the fly: i.e. executing a query every time I need anything on the Qt side.
-
You can use a QSqlTableModel subclass to feed your Qt application with database data.
Since the main active component will be your Qt application, it will also be simpler. You will update the database from Qt and the web application will be responsible to update itself when new data arrive in the database.
-
For your setup involving a Qt application and a PHP web page, using a database seems like the smoother path. Your web page can retrieve data as needed, while you maintain data consistency by syncing the database with your QList in the Qt app. This way, both your Qt app and web page stay on the same page with the data.