RESTful Color Palette Server▲
Sélectionnez
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "apibehavior.h"
#include "types.h"
#include "utils.h"
#include <QtCore/QCoreApplication>
#include <QtHttpServer/QHttpServer>
#define SCHEME "http"
#define HOST "127.0.0.1"
#define PORT 49425
template
&
lt;typename
T&
gt;
void
addCrudRoutes(QHttpServer &
amp;httpServer, const
QString &
amp;apiPath, CrudApi&
lt;T&
gt; &
amp;api,
const
SessionApi &
amp;sessionApi)
{
httpServer.route(
QString(&
quot;%
1
&
quot;).arg(apiPath), QHttpServerRequest::Method::
Get,
[&
amp;api](const
QHttpServerRequest &
amp;request) {
return
api.getPaginatedList(request); }
);
httpServer.route(QString(&
quot;%
1
/&
lt;arg&
gt;&
quot;).arg(apiPath), QHttpServerRequest::Method::
Get,
[&
amp;api](qint64 itemId) {
return
api.getItem(itemId); }
);
httpServer.route(QString(&
quot;%
1
&
quot;).arg(apiPath), QHttpServerRequest::Method::
Post,
[&
amp;api, &
amp;sessionApi](const
QHttpServerRequest &
amp;request) {
if
(!
sessionApi.authorize(request)) {
return
QHttpServerResponse(
QHttpServerResponder::StatusCode::
Unauthorized);
}
return
api.postItem(request);
}
);
httpServer.route(QString(&
quot;%
1
/&
lt;arg&
gt;&
quot;).arg(apiPath), QHttpServerRequest::Method::
Put,
[&
amp;api, &
amp;sessionApi](qint64 itemId, const
QHttpServerRequest &
amp;request) {
if
(!
sessionApi.authorize(request)) {
return
QHttpServerResponse(
QHttpServerResponder::StatusCode::
Unauthorized);
}
return
api.updateItem(itemId, request);
}
);
httpServer.route(QString(&
quot;%
1
/&
lt;arg&
gt;&
quot;).arg(apiPath), QHttpServerRequest::Method::
Patch,
[&
amp;api, &
amp;sessionApi](qint64 itemId, const
QHttpServerRequest &
amp;request) {
if
(!
sessionApi.authorize(request)) {
return
QHttpServerResponse(
QHttpServerResponder::StatusCode::
Unauthorized);
}
return
api.updateItemFields(itemId, request);
}
);
httpServer.route(QString(&
quot;%
1
/&
lt;arg&
gt;&
quot;).arg(apiPath), QHttpServerRequest::Method::
Delete,
[&
amp;api, &
amp;sessionApi](qint64 itemId, const
QHttpServerRequest &
amp;request) {
if
(!
sessionApi.authorize(request)) {
return
QHttpServerResponse(
QHttpServerResponder::StatusCode::
Unauthorized);
}
return
api.deleteItem(itemId);
}
);
}
int
main(int
argc, char
*
argv[])
{
QCoreApplication app(argc, argv);
QCommandLineParser parser;
parser.addOptions({
{
&
quot;port&
quot;, QCoreApplication::
translate(&
quot;main&
quot;, &
quot;The port the server listens on.&
quot;),
&
quot;port&
quot; }
,
}
);
parser.addHelpOption();
parser.process(app);
quint16 portArg =
PORT;
if
(!
parser.value(&
quot;port&
quot;).isEmpty())
portArg =
parser.value(&
quot;port&
quot;).toUShort();
auto
colorFactory =
std::
make_unique&
lt;ColorFactory&
gt;();
auto
colors =
tryLoadFromFile&
lt;Color&
gt;(*
colorFactory, &
quot;:/
assets/
colors.json&
quot;);
CrudApi&
lt;Color&
gt; colorsApi(std::
move(colors), std::
move(colorFactory));
auto
userFactory =
std::
make_unique&
lt;UserFactory&
gt;(SCHEME, HOST, portArg);
auto
users =
tryLoadFromFile&
lt;User&
gt;(*
userFactory, &
quot;:/
assets/
users.json&
quot;);
CrudApi&
lt;User&
gt; usersApi(std::
move(users), std::
move(userFactory));
auto
sessionEntryFactory =
std::
make_unique&
lt;SessionEntryFactory&
gt;();
auto
sessions =
tryLoadFromFile&
lt;SessionEntry&
gt;(*
sessionEntryFactory, &
quot;:/
assets/
sessions.json&
quot;);
SessionApi sessionApi(std::
move(sessions), std::
move(sessionEntryFactory));
// Setup QHttpServer
QHttpServer httpServer;
httpServer.route(&
quot;/&
quot;, []() {
return
&
quot;Qt Colorpalette example server. Please see documentation for
API description&
quot;;
}
);
addCrudRoutes(httpServer, &
quot;/
api/
unknown&
quot;, colorsApi, sessionApi);
addCrudRoutes(httpServer, &
quot;/
api/
users&
quot;, usersApi, sessionApi);
// Login resource
httpServer.route(
&
quot;/
api/
login&
quot;, QHttpServerRequest::Method::
Post,
[&
amp;sessionApi](const
QHttpServerRequest &
amp;request) {
return
sessionApi.login(request); }
);
httpServer.route(&
quot;/
api/
register
&
quot;, QHttpServerRequest::Method::
Post,
[&
amp;sessionApi](const
QHttpServerRequest &
amp;request) {
return
sessionApi.registerSession(request);
}
);
httpServer.route(&
quot;/
api/
logout&
quot;, QHttpServerRequest::Method::
Post,
[&
amp;sessionApi](const
QHttpServerRequest &
amp;request) {
return
sessionApi.logout(request);
}
);
// Images resource
httpServer.route(&
quot;/
img/
faces/&
lt;arg&
gt;-
image.jpg&
quot;, QHttpServerRequest::Method::
Get,
[](qint64 imageId, const
QHttpServerRequest &
amp;) {
return
QHttpServerResponse::
fromFile(
QString(&
quot;:/
assets/
img/%
1
-
image.jpg&
quot;).arg(imageId));
}
);
const
auto
port =
httpServer.listen(QHostAddress::
Any, portArg);
if
(!
port) {
qDebug() &
lt;&
lt; QCoreApplication::
translate(&
quot;QHttpServerExample&
quot;,
&
quot;Server failed to listen on a port.&
quot;);
return
0
;
}
qDebug() &
lt;&
lt; QCoreApplication::
translate(
&
quot;QHttpServerExample&
quot;,
&
quot;Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)")
.arg(port);
return
app.exec();
}