File: main.cpp

package info (click to toggle)
qt6-httpserver 6.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,032 kB
  • sloc: cpp: 8,416; makefile: 23
file content (41 lines) | stat: -rw-r--r-- 1,116 bytes parent folder | download | duplicates (4)
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
39
40
41
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QHttpServer>
#include <QHttpServerResponse>

#include <QCoreApplication>
#include <QString>
#include <QTcpServer>
#include <QTimer>
#include <QMetaType>

//! [Define class]
struct CustomArg {
    int data = 10;

    CustomArg() {} ;
    CustomArg(const QString &urlArg) : data(urlArg.toInt()) {}
};
//! [Define class]

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QHttpServer server;
    auto tcpServer = std::make_unique<QTcpServer>();
    if (!tcpServer->listen(QHostAddress::Any, 1999) || !server.bind(tcpServer.get())) {
        qWarning() << "Server failed to listen on port 1999.";
        return -1;
    }
    tcpServer.release();

    //! [Add converter and route]
    server.router()->addConverter<CustomArg>(u"[+-]?\\d+");
    server.route("/customTest/<arg>", [] (const CustomArg custom) { return QString::number(custom.data); });
    //! [Add converter and route]

    qInfo().noquote() << "Server listening on port 1999.";

    return app.exec();
}