Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: CI
name: urealmedium CI

'on':
schedule:
- cron: '30 5 * * 1' # Every Monday at 5:30
pull_request:
push:
branches:
Expand Down Expand Up @@ -42,16 +44,15 @@ jobs:

- name: Install packages
run: |
(cd third_party && git clone -b develop --single-branch --depth 1 https://github.com/userver-framework/userver.git)
sudo apt update
sudo apt install --allow-downgrades -y postgresql $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
sudo apt install --allow-downgrades -y pep8 $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
sudo apt install --allow-downgrades -y pycodestyle postgresql $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')

- name: Reinstall postgres 14
run: |
sudo apt purge libpq5 libpq-dev postgresql-*
sudo apt install -y postgresql-14 postgresql-client-14 postgresql-server-dev-14


- name: Setup ccache
run: |
ccache -M 2.0GB
Expand Down Expand Up @@ -81,11 +82,11 @@ jobs:

- name: Test run after install
if: matrix.make == 'test-release'
run: >-
./local_installation/bin/realmedium_sample
--config=./local_installation/etc/realmedium_sample/static_config.yaml
--config_vars=./local_installation/etc/realmedium_sample/config_vars.yaml
&
run: |
./local_installation/bin/realmedium_sample \
--config=./local_installation/etc/realmedium_sample/config.yaml \
--config_vars=./local_installation/etc/realmedium_sample/config_vars.yaml \
&

- name: Check work run service
if: matrix.make == 'test-release'
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Docker build
name: urealmedium Docker build

'on':
schedule:
- cron: '30 5 * * 1' # Every Monday at 5:30
pull_request:
push:
branches:
Expand All @@ -15,12 +17,12 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: true

- name: Reuse ccache directory
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: .ccache
key: 'ccache-dir-${{github.ref}}_run-${{github.run_number}}'
Expand Down
22 changes: 0 additions & 22 deletions .github/workflows/update-submodules.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "third_party/userver"]
path = third_party/userver
url = https://github.com/userver-framework/userver.git
[submodule "third_party/cpp-jwt"]
path = third_party/cpp-jwt
url = https://github.com/arun11299/cpp-jwt
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ add_library(${PROJECT_NAME}_objs OBJECT
src/cache/articles_cache.cpp
src/cache/comments_cache.cpp
src/cache/comments_cache.hpp
src/handlers/common.cpp
src/handlers/common.hpp
src/handlers/profiles/profiles.cpp
src/handlers/profiles/profiles.hpp
src/handlers/users/users.cpp
Expand Down Expand Up @@ -80,15 +82,13 @@ add_library(${PROJECT_NAME}_objs OBJECT
src/dto/comment.hpp
src/dto/comment.cpp
src/db/sql.hpp
src/db/types.hpp
src/models/user.hpp
src/models/user.cpp
src/models/comment.hpp
src/models/comment.cpp
src/models/article.hpp
src/models/article.cpp
src/models/profile.hpp
src/models/profile.cpp
src/validators/user_validators.hpp
src/validators/user_validators.cpp
src/validators/length_validator.hpp
Expand Down Expand Up @@ -118,10 +118,8 @@ target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver::core userver::postgre

file(GLOB_RECURSE SCHEMAS ${CMAKE_CURRENT_SOURCE_DIR}/docs/*.yaml)
userver_target_generate_chaotic(${PROJECT_NAME}-chgen
ARGS
-n "/components/schemas/([^/]*)/=real_medium::handlers::{0}"
-f "(.*)={0}"
--generate-serializers
LAYOUT "/components/schemas/([^/]*)/=real_medium::handlers::{0}"
GENERATE_SERIALIZERS
OUTPUT_DIR
${CMAKE_CURRENT_BINARY_DIR}/src
SCHEMAS
Expand Down Expand Up @@ -153,7 +151,9 @@ target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs user
add_google_tests(${PROJECT_NAME}_unittest)

# Functional Tests
add_subdirectory(tests)
userver_testsuite_add_simple(
REQUIREMENTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/requirements.txt
)

# Install
include(GNUInstallDirs)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CMAKE_COMMON_FLAGS ?= -DUSERVER_OPEN_SOURCE_BUILD=1 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE='addr ub'
CMAKE_RELEASE_FLAGS ?=
CMAKE_OS_FLAGS ?= -DUSERVER_FEATURE_CRYPTOPP_BLAKE2=0 -DUSERVER_FEATURE_REDIS_HI_MALLOC=1
Expand Down
1 change: 0 additions & 1 deletion Makefile.local

This file was deleted.

15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# RealMedium

## THIS SAMPLE IS NOT READY! COME BACK LATER
[![urealmedium CI](https://github.com/userver-framework/realmedium_sample/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/userver-framework/realmedium_sample/actions/workflows/ci.yml)
[![urealmedium Docker build](https://github.com/userver-framework/realmedium_sample/actions/workflows/docker.yaml/badge.svg?branch=develop)](https://github.com/userver-framework/realmedium_sample/actions/workflows/docker.yaml)

This is project created in summer school Yandex.
This codebase was created to demonstrate a fully fledged backend application built with **[userver framework](https://userver.tech/)**

### Prerequisites

[Install the userver](https://userver.tech/de/dab/md_en_2userver_2build_2build.html#ways_to_get_userver) into the system
or
* check it out `git clone --depth 1 https://github.com/userver-framework/userver.git /path/to/userver`
* install all of its dependencies
* `ln -s /path/to/userver /path/to/realmedioum_sample/third_party/userver`

### Docker-compose
```
# Clone submodules
Expand All @@ -13,16 +22,20 @@ git submodule update --init
# Run service
make docker-start-service-release
```

### Local
```
make service-start-release
```

## Tests
Run unit and functional tests in docker or local.

### Docker-compose
```
make docker-test-release
```

### Local
```
make test-release
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ services:
- CMAKE_OPTIONS
volumes:
- .:/realmedium:rw
- ./third_party/userver/tools/docker:/tools:ro
- ${TC_CORES_DIR:-./.cores}:/cores:rw
ports:
- 8080:8080
Expand Down
12 changes: 10 additions & 2 deletions src/cache/articles_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "articles_cache.hpp"

#include <userver/utils/numeric_cast.hpp>

namespace real_medium::cache::articles_cache {

userver::storages::postgres::Query ArticlesCachePolicy::kQuery =
Expand Down Expand Up @@ -41,7 +43,10 @@ ArticlesCacheContainer::getRecent(
std::vector<ArticlePtr> articles;
int offset = 0;
for (const auto& it : recentArticles_) {
if (filter.limit && articles.size() >= filter.limit) break;
if (filter.limit &&
articles.size() >=
userver::utils::numeric_cast<std::size_t>(filter.limit))
break;

const auto& tags = it.second->tags;
if (filter.tag && it.second->tags.find(filter.tag.value()) == tags.end())
Expand Down Expand Up @@ -77,7 +82,10 @@ std::vector<ArticlesCacheContainer::ArticlePtr> ArticlesCacheContainer::getFeed(
;
int offset = 0;
for (const auto& it : followedArticlesOrdered) {
if (filter.limit && articles.size() >= filter.limit) break;
if (filter.limit &&
articles.size() >=
userver::utils::numeric_cast<std::size_t>(filter.limit))
break;
if (filter.offset && offset < filter.offset) {
++offset;
continue;
Expand Down
5 changes: 3 additions & 2 deletions src/cache/articles_cache.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include <docs/api/api.hpp>
#include <memory>
#include <unordered_map>
#include <userver/cache/base_postgres_cache.hpp>
#include <userver/storages/postgres/io/chrono.hpp>
#include <docs/api/api.hpp>
#include "../db/sql.hpp"
#include "../dto/filter.hpp"
#include "../models/article.hpp"
Expand All @@ -26,7 +26,8 @@ class ArticlesCacheContainer {
std::vector<ArticlePtr> getRecent(
real_medium::handlers::ArticleFilterDTO& filter_) const;
std::vector<ArticlePtr> getFeed(
real_medium::handlers::FeedArticleFilterDTO& filter_, UserId authId_) const;
real_medium::handlers::FeedArticleFilterDTO& filter_,
UserId authId_) const;

private:
struct TimepointedArticle {
Expand Down
15 changes: 0 additions & 15 deletions src/db/types.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/dto/article.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <docs/api/api.hpp>
#include <string>
#include <userver/formats/json/value.hpp>
#include <userver/formats/parse/common_containers.hpp>
#include <userver/server/handlers/http_handler_base.hpp>
#include <docs/api/api.hpp>
#include <userver/storages/postgres/io/chrono.hpp>
#include "models/article.hpp"

Expand Down
6 changes: 4 additions & 2 deletions src/dto/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace real_medium::dto {

template <>
handlers::FeedArticleFilterDTO Parse(const userver::server::http::HttpRequest& request) {
handlers::FeedArticleFilterDTO Parse(
const userver::server::http::HttpRequest& request) {
handlers::FeedArticleFilterDTO filter;
if (request.HasArg("limit")) {
filter.limit = boost::lexical_cast<std::int32_t>(request.GetArg("limit"));
Expand All @@ -19,7 +20,8 @@ handlers::FeedArticleFilterDTO Parse(const userver::server::http::HttpRequest& r
}

template <>
handlers::ArticleFilterDTO Parse(const userver::server::http::HttpRequest& request) {
handlers::ArticleFilterDTO Parse(
const userver::server::http::HttpRequest& request) {
handlers::ArticleFilterDTO filter;
if (request.HasArg("tag")) {
filter.tag = request.GetArg("tag");
Expand Down
19 changes: 7 additions & 12 deletions src/handlers/articles/articles_favorite.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#include "articles_favorite.hpp"

#include <userver/formats/json/serialize_container.hpp>

#include "db/sql.hpp"
#include "dto/article.hpp"

namespace real_medium::handlers::articles_favorite::post {

Handler::Handler(const userver::components::ComponentConfig& config,
const userver::components::ComponentContext& component_context)
: HttpHandlerJsonBase(config, component_context),
pg_cluster_(component_context
.FindComponent<userver::components::Postgres>(
"realmedium-database")
.GetCluster()) {}

userver::formats::json::Value Handler::HandleRequestJsonThrow(
const userver::server::http::HttpRequest& request,
const userver::formats::json::Value&,
Expand All @@ -20,9 +15,9 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
auto& slug = request.GetPathArg("slug");

auto transaction =
pg_cluster_->Begin("favorite_article_transaction",
userver::storages::postgres::ClusterHostType::kMaster,
userver::storages::postgres::Transaction::RW);
GetPg().Begin("favorite_article_transaction",
userver::storages::postgres::ClusterHostType::kMaster,
userver::storages::postgres::Transaction::RW);

auto res =
transaction.Execute(sql::kInsertFavoritePair.data(), user_id, slug);
Expand All @@ -34,7 +29,7 @@ userver::formats::json::Value Handler::HandleRequestJsonThrow(
transaction.Commit();
}

const auto get_article_res = pg_cluster_->Execute(
const auto get_article_res = GetPg().Execute(
userver::storages::postgres::ClusterHostType::kSlave,
real_medium::sql::kGetArticleWithAuthorProfileBySlug.data(), slug,
user_id);
Expand Down
18 changes: 6 additions & 12 deletions src/handlers/articles/articles_favorite.hpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
#pragma once

#include "userver/components/component.hpp"
#include "userver/components/component_list.hpp"
#include "userver/formats/json/serialize_container.hpp"
#include "userver/server/handlers/http_handler_json_base.hpp"
#include "userver/storages/postgres/cluster.hpp"
#include "userver/storages/postgres/component.hpp"
#include <userver/formats/json/serialize_container.hpp>
#include <userver/storages/postgres/cluster.hpp>

#include "handlers/common.hpp"

namespace real_medium::handlers::articles_favorite::post {

class Handler final : public userver::server::handlers::HttpHandlerJsonBase {
class Handler final : public Common {
public:
static constexpr std::string_view kName = "handler-articles-favorite-post";

Handler(const userver::components::ComponentConfig& config,
const userver::components::ComponentContext& component_context);
using Common::Common;

userver::formats::json::Value HandleRequestJsonThrow(
const userver::server::http::HttpRequest& request,
const userver::formats::json::Value&,
userver::server::request::RequestContext& context) const override;

private:
userver::storages::postgres::ClusterPtr pg_cluster_;
};

} // namespace real_medium::handlers::articles_favorite::post
Loading