mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -04:00
made JsonApiServer an singleton through static method instance()
This commit is contained in:
parent
3b45fc5199
commit
d19d1685de
9 changed files with 129 additions and 95 deletions
|
@ -44,6 +44,9 @@
|
|||
|
||||
/*extern*/ JsonApiServer* jsonApiServer = nullptr;
|
||||
|
||||
const std::string JsonApiServer::DEFAULT_LISTENING_ADDRESS = "127.0.0.1";
|
||||
p3ConfigMgr *JsonApiServer::_config_mgr = nullptr;
|
||||
|
||||
/*static*/ const std::multimap<std::string, std::string>
|
||||
JsonApiServer::corsHeaders =
|
||||
{
|
||||
|
@ -115,11 +118,33 @@ JsonApiServer::corsOptionsHeaders =
|
|||
return false;
|
||||
}
|
||||
|
||||
JsonApiServer::JsonApiServer(uint16_t port, const std::string& bindAddress,
|
||||
const std::function<bool(const std::string&)> newAccessRequestCallback ) :
|
||||
mPort(port), mBindAddress(bindAddress),
|
||||
mNewAccessRequestCallback(newAccessRequestCallback),
|
||||
configMutex("JsonApiServer config")
|
||||
JsonApiServer& JsonApiServer::instance()
|
||||
{
|
||||
static JsonApiServer *_instance = nullptr;
|
||||
|
||||
if(_instance == NULL)
|
||||
{
|
||||
_instance = new JsonApiServer();
|
||||
|
||||
if(_config_mgr == nullptr)
|
||||
RsErr() << "JsonApiServer::instance() called before JsonApiServer::setConfigManager(). This is a bug!" << std::endl;
|
||||
|
||||
_config_mgr->addConfiguration("jsonapi.cfg",_instance);
|
||||
}
|
||||
|
||||
return *_instance;
|
||||
}
|
||||
|
||||
void JsonApiServer::start(uint16_t port, const std::string& bindAddress, const std::function<bool(const std::string&)> callback)
|
||||
{
|
||||
mPort = port;
|
||||
mBindAddress = bindAddress;
|
||||
mNewAccessRequestCallback = callback;
|
||||
|
||||
RsThread::start("JsonApiServer");
|
||||
}
|
||||
|
||||
JsonApiServer::JsonApiServer(): configMutex("JsonApiServer config")
|
||||
{
|
||||
registerHandler("/rsLoginHelper/createLocation",
|
||||
[this](const std::shared_ptr<rb::Session> session)
|
||||
|
@ -342,6 +367,10 @@ JsonApiServer::JsonApiServer(uint16_t port, const std::string& bindAddress,
|
|||
} );
|
||||
}, true);
|
||||
|
||||
RsFileHash dummyHash;
|
||||
setFilename("jsonapi.cfg"); // hack
|
||||
loadConfiguration(dummyHash);
|
||||
|
||||
// Generated at compile time
|
||||
#include "jsonapi-wrappers.inl"
|
||||
}
|
||||
|
|
|
@ -35,11 +35,11 @@ namespace rb = restbed;
|
|||
|
||||
struct JsonApiServer;
|
||||
|
||||
/**
|
||||
* Pointer to global instance of JsonApiServer
|
||||
* @jsonapi{development}
|
||||
*/
|
||||
extern JsonApiServer* jsonApiServer;
|
||||
// /**
|
||||
// * Pointer to global instance of JsonApiServer
|
||||
// * @jsonapi{development}
|
||||
// */
|
||||
// extern JsonApiServer* jsonApiServer;
|
||||
|
||||
/**
|
||||
* Simple usage
|
||||
|
@ -53,20 +53,9 @@ extern JsonApiServer* jsonApiServer;
|
|||
struct JsonApiServer : RsSingleJobThread, p3Config
|
||||
{
|
||||
static const uint16_t DEFAULT_PORT = 9092 ;
|
||||
static const std::string DEFAULT_LISTENING_ADDRESS ;
|
||||
|
||||
/**
|
||||
* @brief construct a JsonApiServer instance with given parameters
|
||||
* @param[in] port listening port fpt the JSON API socket
|
||||
* @param[in] bindAddress binding address for the JSON API socket
|
||||
* @param newAccessRequestCallback called when a new auth token is asked to
|
||||
* be authorized via JSON API, the auth token is passed as parameter, and
|
||||
* the callback should return true if the new token get access granted and
|
||||
* false otherwise, this usually requires user interacion to confirm access
|
||||
*/
|
||||
JsonApiServer(
|
||||
uint16_t port = DEFAULT_PORT,
|
||||
const std::string& bindAddress = "127.0.0.1",
|
||||
const std::function<bool(const std::string&)> newAccessRequestCallback = [](const std::string&){return false;} );
|
||||
static JsonApiServer& instance() ;
|
||||
|
||||
/**
|
||||
* @param[in] path Path itno which publish the API call
|
||||
|
@ -96,6 +85,17 @@ struct JsonApiServer : RsSingleJobThread, p3Config
|
|||
*/
|
||||
void shutdown();
|
||||
|
||||
/**
|
||||
* @brief start
|
||||
* Starts the json Api server.
|
||||
*
|
||||
* @param port port to listen to
|
||||
* @param listen_address bind address to listen to
|
||||
*/
|
||||
void start( uint16_t port = DEFAULT_PORT,
|
||||
const std::string& bindAddress = "127.0.0.1",
|
||||
const std::function<bool(const std::string&)> newAccessRequestCallback = [](const std::string&){return false;});
|
||||
|
||||
/**
|
||||
* @brief This function should be used by JSON API clients that aren't
|
||||
* authenticated yet, to ask their token to be authorized, the success or
|
||||
|
@ -175,10 +175,25 @@ struct JsonApiServer : RsSingleJobThread, p3Config
|
|||
static void version( uint32_t& major, uint32_t& minor, uint32_t& mini,
|
||||
std::string& extra, std::string&human );
|
||||
|
||||
|
||||
static void setConfigMgr(p3ConfigMgr *cfg) { _config_mgr = cfg; }
|
||||
protected:
|
||||
/// @see RsSingleJobThread
|
||||
virtual void run();
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief construct a JsonApiServer instance with given parameters
|
||||
* @param[in] port listening port fpt the JSON API socket
|
||||
* @param[in] bindAddress binding address for the JSON API socket
|
||||
* @param newAccessRequestCallback called when a new auth token is asked to
|
||||
* be authorized via JSON API, the auth token is passed as parameter, and
|
||||
* the callback should return true if the new token get access granted and
|
||||
* false otherwise, this usually requires user interacion to confirm access
|
||||
*/
|
||||
JsonApiServer( );
|
||||
|
||||
|
||||
/// @see p3Config::setupSerialiser
|
||||
virtual RsSerialiser* setupSerialiser();
|
||||
|
||||
|
@ -191,8 +206,8 @@ private:
|
|||
/// @see p3Config::saveDone
|
||||
virtual void saveDone();
|
||||
|
||||
const uint16_t mPort;
|
||||
const std::string mBindAddress;
|
||||
uint16_t mPort;
|
||||
std::string mBindAddress;
|
||||
rb::Service mService;
|
||||
|
||||
/// Called when new JSON API auth token is requested to be authorized
|
||||
|
@ -220,5 +235,7 @@ private:
|
|||
return checkRsServicePtrReady(
|
||||
serviceInstance.get(), serviceName, ctx, session );
|
||||
}
|
||||
|
||||
static p3ConfigMgr *_config_mgr;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue