Skip to content

Commit ec174e8

Browse files
committed
work
1 parent eef0914 commit ec174e8

File tree

8 files changed

+105
-14
lines changed

8 files changed

+105
-14
lines changed

configs/static_config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ components_manager:
3434
http-client:
3535
fs-task-processor: fs-task-processor
3636
resources-cache:
37-
dir: /var/www
38-
update-period: 30s
37+
dir: /home/segoon/projects/upastebin/www-data
38+
update-period: 1s
3939
fs-task-processor: fs-task-processor
4040

4141
tests-control:
@@ -68,7 +68,7 @@ components_manager:
6868
method: GET
6969
task_processor: main-task-processor
7070
handler-text:
71-
path: /*
71+
path: /{id}
7272
method: GET
7373
task_processor: main-task-processor
7474
handler-resources:

src/redirect.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RedirectHandler final : public userver::server::handlers::HttpHandlerBase
2222
userver::server::request::RequestContext&) const override {
2323
auto& response = request.GetHttpResponse();
2424
response.SetStatus(userver::server::http::HttpStatus::kTemporaryRedirect);
25-
response.SetHeader(userver::http::headers::kLocation, "/auth");
25+
response.SetHeader(userver::http::headers::kLocation, "/index.html");
2626
return {};
2727
}
2828
};

src/text.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
#include "text.hpp"
22

33
#include <userver/components/component_context.hpp>
4-
// #include <userver/formats/json/inline.hpp>
5-
// #include <userver/http/common_headers.hpp>
6-
// #include <userver/server/handlers/exceptions.hpp>
7-
// #include <userver/server/handlers/http_handler_base.hpp>
4+
#include <userver/components/fs_cache.hpp>
85

96
namespace upastebin {
107

118
TextHandler::TextHandler(
129
const userver::components::ComponentConfig& config,
1310
const userver::components::ComponentContext& context)
1411
: HttpHandlerBase(config, context),
15-
fs_(context.GetTaskProcessor("fs-task-processor")) {}
12+
fs_client_(
13+
context.FindComponent<userver::components::FsCache>("resources-cache")
14+
.GetClient()) {}
1615

1716
std::string TextHandler::HandleRequestThrow(
1817
const userver::server::http::HttpRequest& request,
1918
userver::server::request::RequestContext&) const {
19+
auto id = request.GetPathArg("id");
2020

21-
return {};
21+
auto file_ptr = fs_client_.TryGetFile("/text.html");
22+
UINVARIANT(file_ptr, "text.html is not found");
23+
24+
auto& response = request.GetHttpResponse();
25+
response.SetContentType("text/html; charset=UTF-8");
26+
return file_ptr->data;
2227
}
2328

2429
} // namespace upastebin

src/text.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#pragma once
22

3-
#include <userver/engine/task/task_processor_fwd.hpp>
4-
#include <userver/http/common_headers.hpp>
5-
#include <userver/server/handlers/exceptions.hpp>
63
#include <userver/server/handlers/http_handler_base.hpp>
4+
#include <userver/fs/fs_cache_client.hpp>
75

86
namespace upastebin {
97

@@ -20,7 +18,7 @@ class TextHandler final
2018
userver::server::request::RequestContext&) const override;
2119

2220
private:
23-
userver::engine::TaskProcessor& fs_;
21+
const userver::fs::FsCacheClient& fs_client_;
2422
};
2523

2624
} // namespace upastebin

www-data/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF8">
5+
<title>Pastebin (userver)</title>
6+
</head>
7+
<body>
8+
<form name="form" action="#">
9+
<p><label for="author">Автор: </label><input type="text" name="author" required></p>
10+
<p><label for="text">Текст: </label><textarea rows="30" cols="100" name="text"></textarea></p>
11+
<input type="submit" onclick="onSend()">
12+
</form>
13+
<span id="latest">unchanged</span>
14+
<script type="text/javascript" src="../r/index.js"></script>
15+
</body>
16+
</html>

www-data/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
fetch("../api/v1/latest")
2+
.then(function (response) {
3+
return response.json();
4+
})
5+
.then(function (json) {
6+
latest_html = "";
7+
for (const item in json.items) {
8+
latest_html += "<p>author: " + item["author"] + "</p>";
9+
}
10+
document.getElementById("latest").innerHTML = latest_html;
11+
})
12+
.catch(function (error) {
13+
alert(error);
14+
});
15+
16+
function onSend() {
17+
const author = document.forms["form"].author.value;
18+
const text = document.forms["form"].text.value;
19+
20+
fetch("../api/v1/posts?author=" + author, {
21+
method: "POST",
22+
body: text,
23+
})
24+
.then(function(response) {
25+
return response.json();
26+
})
27+
.then(function(json) {
28+
alert(json);
29+
})
30+
.catch(function (error) {
31+
alert(error);
32+
});
33+
}

www-data/text.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF8">
5+
<title>Pastebin (userver)</title>
6+
</head>
7+
<body>
8+
<span id="text"></span>
9+
<script type="text/javascript" src="r/text.js"></script>
10+
<span id="latest">unchanged</span>
11+
<script type="text/javascript" src="r/index.js"></script>
12+
</body>
13+
</html>

www-data/text.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function get_text_id() {
2+
const url = window.location.href;
3+
const pos = url.lastIndexOf("/");
4+
const id = url.substring(pos+1);
5+
return id;
6+
}
7+
8+
function fetch_text_by_id(id) {
9+
return fetch("../api/v1/posts/" + id);
10+
}
11+
12+
function setup_text() {
13+
const id = get_text_id();
14+
fetch_text_by_id(id)
15+
.then(function (response) {
16+
return response.text();
17+
})
18+
.then(function (text) {
19+
document.getElementById("text").innerHTML = text; // TODO: escape
20+
})
21+
.catch(function (error) {
22+
alert(error);
23+
});
24+
}
25+
26+
setup_text();

0 commit comments

Comments
 (0)