Skip to content

Commit

Permalink
feat: support_echo_and_auth
Browse files Browse the repository at this point in the history
Signed-off-by: tangruilin <[email protected]>
  • Loading branch information
Tangruilin committed Jun 20, 2024
1 parent 9d0cbb0 commit 0456edd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace pikiwidb {

// command definition
// base cmd
const std::string kCmdNameEcho = " echo ";
const std::string kCmdNamePing = "ping";

// key cmd
Expand Down
36 changes: 36 additions & 0 deletions src/cmd_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

#include "cmd_admin.h"
#include <optional>
#include "client.h"
#include "config.h"
#include "db.h"

#include "braft/raft.h"
Expand Down Expand Up @@ -135,6 +138,39 @@ bool PingCmd::DoInitial(PClient* client) { return true; }

void PingCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kPong, "PONG"); }

EchoCmd::EchoCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsFast, kAclCategoryFast) {}

bool EchoCmd::DoInitial(PClient* client) { return true; }

void EchoCmd::DoCmd(PClient* client) {
if (client->argv_.size() != 2) {
return client->SetRes(CmdRes::kWrongNum, client->CmdName());
}

client->AppendString(client->argv_[1]);
}

AuthCmd::AuthCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsAdmin, kAclCategoryFast) {}

bool AuthCmd::DoInitial(PClient* client) { return true; }

void AuthCmd::DoCmd(PClient* client) {
if (client->argv_.size() != 2) {
return client->SetRes(CmdRes::kWrongNum, client->CmdName());
}

if (g_config.auth == std::nullopt || !g_config.auth.has_value()) {
return client->SetRes(CmdRes::kOK, "Authentication is empty");
}

auto& auth = g_config.auth;
if (client->argv_[1] == auth.value()) {
client->SetRes(CmdRes::kOK);
} else {
client->SetRes(CmdRes::kErrOther, "Authentication failed");
}
}

InfoCmd::InfoCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsAdmin | kCmdFlagsReadonly, kAclCategoryAdmin) {}

Expand Down
22 changes: 22 additions & 0 deletions src/cmd_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ class PingCmd : public BaseCmd {
void DoCmd(PClient* client) override;
};

class EchoCmd : public BaseCmd {
public:
EchoCmd(const std::string& name, int16_t arity);

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;
};

class AuthCmd : public BaseCmd {
public:
AuthCmd(const std::string& name, int16_t arity);

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;
};

class InfoCmd : public BaseCmd {
public:
InfoCmd(const std::string& name, int16_t arity);
Expand Down
4 changes: 3 additions & 1 deletion src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND_GROUP(Config, -2);
ADD_SUBCOMMAND(Config, Get, -3);
ADD_SUBCOMMAND(Config, Set, -4);
ADD_COMMAND(Ping, 0);
ADD_COMMAND(Ping, 1);
ADD_COMMAND_GROUP(Debug, -2);
ADD_SUBCOMMAND(Debug, Help, 2);
ADD_SUBCOMMAND(Debug, OOM, 2);
ADD_SUBCOMMAND(Debug, Segfault, 2);
ADD_COMMAND(Echo, 2);
ADD_COMMAND(Auth, 2);

// server
ADD_COMMAND(Flushdb, 1);
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <shared_mutex>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -173,6 +174,7 @@ class PConfig {
std::atomic_int rocksdb_level0_stop_writes_trigger = 36;
std::atomic_uint64_t rocksdb_ttl_second = 604800; // default 86400 * 7
std::atomic_uint64_t rocksdb_periodic_second = 259200; // default 86400 * 3
std::optional<std::string> auth = std::nullopt;

rocksdb::Options GetRocksDBOptions();

Expand Down

0 comments on commit 0456edd

Please sign in to comment.