-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from xaya/rest
Add REST API and use it for bootstrap data
- Loading branch information
Showing
10 changed files
with
324 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -Im4 | ||
|
||
SUBDIRS = hexagonal proto database mapdata src gametest | ||
SUBDIRS = data hexagonal proto database mapdata src gametest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
EXTRA_DIST = letsencrypt.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw | ||
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh | ||
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4 | ||
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu | ||
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATB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vwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK | ||
NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5 | ||
ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur | ||
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC | ||
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc | ||
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq | ||
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA | ||
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d | ||
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc= | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright (C) 2020 The Xaya developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "rest.hpp" | ||
|
||
#include "gamestatejson.hpp" | ||
|
||
#include <microhttpd.h> | ||
|
||
#include <gflags/gflags.h> | ||
#include <glog/logging.h> | ||
|
||
#include <chrono> | ||
|
||
namespace pxd | ||
{ | ||
|
||
namespace | ||
{ | ||
|
||
DEFINE_int32 (rest_bootstrap_refresh_seconds, 60 * 60, | ||
"the refresh interval for bootstrap data in seconds"); | ||
|
||
} // anonymous namespace | ||
|
||
std::shared_ptr<RestApi::SuccessResult> | ||
RestApi::ComputeBootstrapData () | ||
{ | ||
const Json::Value val = logic.GetCustomStateData (game, | ||
[] (GameStateJson& gsj) | ||
{ | ||
return gsj.BootstrapData (); | ||
}); | ||
auto res = std::make_shared<SuccessResult> (SuccessResult (val).Gzip ()); | ||
|
||
if (val["state"].asString () == "up-to-date") | ||
{ | ||
LOG (INFO) << "Refreshing bootstrap-data cache"; | ||
std::lock_guard<std::mutex> lock(mutBootstrap); | ||
bootstrapData = res; | ||
} | ||
else | ||
LOG (WARNING) << "We are still catching up, not caching bootstrap data"; | ||
|
||
return res; | ||
} | ||
|
||
RestApi::SuccessResult | ||
RestApi::Process (const std::string& url) | ||
{ | ||
std::string remainder; | ||
if (MatchEndpoint (url, "/bootstrap.json.gz", remainder) && remainder == "") | ||
{ | ||
std::shared_ptr<SuccessResult> res; | ||
{ | ||
std::lock_guard<std::mutex> lock(mutBootstrap); | ||
res = bootstrapData; | ||
} | ||
if (res == nullptr) | ||
res = ComputeBootstrapData (); | ||
CHECK (res != nullptr); | ||
return *res; | ||
} | ||
|
||
throw HttpError (MHD_HTTP_NOT_FOUND, "invalid API endpoint"); | ||
} | ||
|
||
void | ||
RestApi::Start () | ||
{ | ||
xaya::RestApi::Start (); | ||
|
||
std::lock_guard<std::mutex> lock(mutStop); | ||
shouldStop = false; | ||
CHECK (bootstrapRefresher == nullptr); | ||
bootstrapRefresher = std::make_unique<std::thread> ([this] () | ||
{ | ||
const auto intv | ||
= std::chrono::seconds (FLAGS_rest_bootstrap_refresh_seconds); | ||
while (true) | ||
{ | ||
ComputeBootstrapData (); | ||
|
||
std::unique_lock<std::mutex> lock(mutStop); | ||
if (shouldStop) | ||
break; | ||
cvStop.wait_for (lock, intv); | ||
if (shouldStop) | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
void | ||
RestApi::Stop () | ||
{ | ||
{ | ||
std::lock_guard<std::mutex> lock(mutStop); | ||
shouldStop = true; | ||
cvStop.notify_all (); | ||
} | ||
|
||
if (bootstrapRefresher != nullptr) | ||
{ | ||
bootstrapRefresher->join (); | ||
bootstrapRefresher.reset (); | ||
} | ||
|
||
xaya::RestApi::Stop (); | ||
} | ||
|
||
Json::Value | ||
RestClient::GetBootstrapData () | ||
{ | ||
Request req(*this); | ||
if (!req.Send ("/bootstrap.json.gz")) | ||
throw std::runtime_error (req.GetError ()); | ||
|
||
if (req.GetType () != "application/json") | ||
throw std::runtime_error ("response is not JSON"); | ||
|
||
return req.GetJson (); | ||
} | ||
|
||
} // namespace pxd |
Oops, something went wrong.