init repo.
This commit is contained in:
12
.codecrafters/compile.sh
Executable file
12
.codecrafters/compile.sh
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# This script is used to compile your program on CodeCrafters
|
||||||
|
#
|
||||||
|
# This runs before .codecrafters/run.sh
|
||||||
|
#
|
||||||
|
# Learn more: https://codecrafters.io/program-interface
|
||||||
|
|
||||||
|
set -e # Exit on failure
|
||||||
|
|
||||||
|
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
|
||||||
|
cmake --build ./build
|
||||||
11
.codecrafters/run.sh
Executable file
11
.codecrafters/run.sh
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# This script is used to run your program on CodeCrafters
|
||||||
|
#
|
||||||
|
# This runs after .codecrafters/compile.sh
|
||||||
|
#
|
||||||
|
# Learn more: https://codecrafters.io/program-interface
|
||||||
|
|
||||||
|
set -e # Exit on failure
|
||||||
|
|
||||||
|
exec ./build/server "$@"
|
||||||
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
* text=auto
|
||||||
51
.gitignore
vendored
Normal file
51
.gitignore
vendored
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
*.smod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
server
|
||||||
|
|
||||||
|
# CMake
|
||||||
|
CMakeLists.txt.user
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Testing
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
CTestTestfile.cmake
|
||||||
|
_deps
|
||||||
|
|
||||||
|
build/
|
||||||
|
.cache/
|
||||||
|
vcpkg_installed
|
||||||
|
out/
|
||||||
14
.vscode/tasks.json
vendored
Normal file
14
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"type": "shell",
|
||||||
|
"label": "CMake Generate",
|
||||||
|
"command": "cmake -S. -Bbuild -DCAMKE_POLICY_MINIMUM_VERSION=3.5 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON $VCPKG_CMAKE_ARGS"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "shell",
|
||||||
|
"label": "CMake Build",
|
||||||
|
"comamnd": "cmake --build build"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
56
CMakeLists.txt
Normal file
56
CMakeLists.txt
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.13)
|
||||||
|
|
||||||
|
project(redis-starter-cpp LANGUAGES C CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11) # Enable the C++23 standard
|
||||||
|
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static -static-libgcc -static-libstdc++")
|
||||||
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static -static-libgcc -static-libstdc++")
|
||||||
|
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
find_package(GTest CONFIG REQUIRED)
|
||||||
|
include(cmake/CPM.cmake)
|
||||||
|
|
||||||
|
CPMAddPackage(
|
||||||
|
NAME Boost
|
||||||
|
VERSION 1.89.0
|
||||||
|
URL https://github.com/boostorg/boost/releases/download/boost-1.89.0/boost-1.89.0-cmake.tar.xz
|
||||||
|
URL_HASH SHA256=67acec02d0d118b5de9eb441f5fb707b3a1cdd884be00ca24b9a73c995511f74
|
||||||
|
# SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/boost
|
||||||
|
OPTIONS "BOOST_ENABLE_CMAKE ON"
|
||||||
|
)
|
||||||
|
|
||||||
|
CPMAddPackage(
|
||||||
|
NAME spdlog
|
||||||
|
VERSION 1.12.0
|
||||||
|
URL https://github.com/gabime/spdlog/archive/refs/tags/v1.12.0.zip
|
||||||
|
URL_HASH SHA256=6174bf8885287422a6c6a0312eb8a30e8d22bcfcee7c48a6d02d1835d7769232
|
||||||
|
OPTIONS "SPDLOG_BUILD_PIC ON"
|
||||||
|
)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE SOURCE_FILES src/*.cpp src/*.cc)
|
||||||
|
add_executable(server ${SOURCE_FILES})
|
||||||
|
target_link_libraries(server PRIVATE
|
||||||
|
Threads::Threads
|
||||||
|
Boost::array
|
||||||
|
Boost::asio
|
||||||
|
Boost::filesystem
|
||||||
|
Boost::format
|
||||||
|
Boost::log
|
||||||
|
Boost::log_setup
|
||||||
|
Boost::property_tree
|
||||||
|
Boost::serialization
|
||||||
|
Boost::filesystem
|
||||||
|
Boost::dll
|
||||||
|
Boost::crc
|
||||||
|
Boost::range
|
||||||
|
Boost::foreach
|
||||||
|
Boost::timer
|
||||||
|
Boost::contract
|
||||||
|
spdlog::spdlog
|
||||||
|
# Poco::Foundation
|
||||||
|
# Poco::Net
|
||||||
|
# Poco::Util
|
||||||
|
)
|
||||||
|
target_include_directories(server PRIVATE src)
|
||||||
33
README.md
Normal file
33
README.md
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
[](https://app.codecrafters.io/users/codecrafters-bot?r=2qF)
|
||||||
|
|
||||||
|
This is a starting point for C++ solutions to the
|
||||||
|
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).
|
||||||
|
|
||||||
|
In this challenge, you'll build a toy Redis clone that's capable of handling
|
||||||
|
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
|
||||||
|
event loops, the Redis protocol and more.
|
||||||
|
|
||||||
|
**Note**: If you're viewing this repo on GitHub, head over to
|
||||||
|
[codecrafters.io](https://codecrafters.io) to try the challenge.
|
||||||
|
|
||||||
|
# Passing the first stage
|
||||||
|
|
||||||
|
The entry point for your Redis implementation is in `src/Server.cpp`. Study and
|
||||||
|
uncomment the relevant code, and push your changes to pass the first stage:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git commit -am "pass 1st stage" # any msg
|
||||||
|
git push origin master
|
||||||
|
```
|
||||||
|
|
||||||
|
That's all!
|
||||||
|
|
||||||
|
# Stage 2 & beyond
|
||||||
|
|
||||||
|
Note: This section is for stages 2 and beyond.
|
||||||
|
|
||||||
|
1. Ensure you have `cmake` installed locally
|
||||||
|
1. Run `./your_program.sh` to run your Redis server, which is implemented in
|
||||||
|
`src/Server.cpp`.
|
||||||
|
1. Commit your changes and run `git push origin master` to submit your solution
|
||||||
|
to CodeCrafters. Test output will be streamed to your terminal.
|
||||||
1363
cmake/CPM.cmake
Normal file
1363
cmake/CPM.cmake
Normal file
File diff suppressed because it is too large
Load Diff
11
codecrafters.yml
Normal file
11
codecrafters.yml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Set this to true if you want debug logs.
|
||||||
|
#
|
||||||
|
# These can be VERY verbose, so we suggest turning them off
|
||||||
|
# unless you really need them.
|
||||||
|
debug: false
|
||||||
|
|
||||||
|
# Use this to change the C++ version used to run your code
|
||||||
|
# on Codecrafters.
|
||||||
|
#
|
||||||
|
# Available versions: cpp-23
|
||||||
|
language_pack: cpp-23
|
||||||
8
src/base/log.h
Normal file
8
src/base/log.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
|
#define LOGV(...) SPDLOG_TRACE(__VA_ARGS__)
|
||||||
|
#define LOGD(...) SPDLOG_DEBUG(__VA_ARGS__)
|
||||||
|
#define LOGI(...) SPDLOG_INFO(__VA_ARGS__)
|
||||||
|
#define LOGW(...) SPDLOG_WARN(__VA_ARGS__)
|
||||||
|
#define LOGE(...) SPDLOG_ERROR(__VA_ARGS__)
|
||||||
75
src/main.cc
Normal file
75
src/main.cc
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#include "base/log.h"
|
||||||
|
#include "boost/asio.hpp"
|
||||||
|
#include "boost/asio/executor_work_guard.hpp"
|
||||||
|
#include "boost/asio/io_context.hpp"
|
||||||
|
#include "boost/chrono/duration.hpp"
|
||||||
|
#include "boost/contract.hpp"
|
||||||
|
#include "boost/foreach.hpp"
|
||||||
|
#include "boost/range.hpp"
|
||||||
|
#include "boost/thread.hpp"
|
||||||
|
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
bool
|
||||||
|
initLog()
|
||||||
|
{
|
||||||
|
spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%f%z [%^%L%$] [thread %t] %v");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
class RedisServer {
|
||||||
|
public:
|
||||||
|
RedisServer(asio::ip::tcp::endpoint ep = asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 6379), int io_thread_num = 1)
|
||||||
|
: _io_context(),
|
||||||
|
_io_context_guard(asio::make_work_guard(_io_context)),
|
||||||
|
_io_threads(),
|
||||||
|
_acceptor(_io_context, ep)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= io_thread_num; ++i) {
|
||||||
|
_io_threads.create_thread(boost::bind(&asio::io_context::run, &_io_context));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool running() const { return true; }
|
||||||
|
|
||||||
|
bool startServer()
|
||||||
|
{
|
||||||
|
LOGI("Listen {}:{}", _acceptor.local_endpoint().address().to_string(), _acceptor.local_endpoint().port());
|
||||||
|
startAccept();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void startAccept()
|
||||||
|
{
|
||||||
|
_acceptor.async_accept([this](const system::error_code &ec, asio::ip::tcp::socket new_socket) {
|
||||||
|
if (!ec) {
|
||||||
|
auto remote_ep = new_socket.remote_endpoint();
|
||||||
|
LOGI("Connected to Server, From {}:{}", remote_ep.address().to_string(), remote_ep.port());
|
||||||
|
} else {
|
||||||
|
LOGE("Accept failed. reason={}", ec.message());
|
||||||
|
}
|
||||||
|
|
||||||
|
startAccept();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
asio::io_context _io_context;
|
||||||
|
asio::executor_work_guard<asio::io_context::executor_type> _io_context_guard;
|
||||||
|
thread_group _io_threads;
|
||||||
|
// for accept
|
||||||
|
asio::ip::tcp::acceptor _acceptor;
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
BOOST_CONTRACT_ASSERT(initLog());
|
||||||
|
|
||||||
|
RedisServer redisServer;
|
||||||
|
redisServer.startServer();
|
||||||
|
while (redisServer.running()) { boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); }
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
14
vcpkg-configuration.json
Normal file
14
vcpkg-configuration.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"default-registry": {
|
||||||
|
"kind": "git",
|
||||||
|
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
|
||||||
|
"repository": "https://github.com/microsoft/vcpkg"
|
||||||
|
},
|
||||||
|
"registries": [
|
||||||
|
{
|
||||||
|
"kind": "artifact",
|
||||||
|
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
|
||||||
|
"name": "microsoft"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
7
vcpkg.json
Normal file
7
vcpkg.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"dependencies": [
|
||||||
|
"eigen3",
|
||||||
|
"gtest",
|
||||||
|
"pthreads"
|
||||||
|
]
|
||||||
|
}
|
||||||
25
your_program.sh
Executable file
25
your_program.sh
Executable file
@@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Use this script to run your program LOCALLY.
|
||||||
|
#
|
||||||
|
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
|
||||||
|
#
|
||||||
|
# Learn more: https://codecrafters.io/program-interface
|
||||||
|
|
||||||
|
set -e # Exit early if any commands fail
|
||||||
|
|
||||||
|
# Copied from .codecrafters/compile.sh
|
||||||
|
#
|
||||||
|
# - Edit this to change how your program compiles locally
|
||||||
|
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
|
||||||
|
(
|
||||||
|
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
|
||||||
|
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
|
||||||
|
cmake --build ./build
|
||||||
|
)
|
||||||
|
|
||||||
|
# Copied from .codecrafters/run.sh
|
||||||
|
#
|
||||||
|
# - Edit this to change how your program runs locally
|
||||||
|
# - Edit .codecrafters/run.sh to change how your program runs remotely
|
||||||
|
exec ./build/server "$@"
|
||||||
Reference in New Issue
Block a user