File tree Expand file tree Collapse file tree 8 files changed +105
-14
lines changed
Expand file tree Collapse file tree 8 files changed +105
-14
lines changed Original file line number Diff line number Diff 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 :
Original file line number Diff line number Diff 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};
Original file line number Diff line number Diff line change 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
96namespace upastebin {
107
118TextHandler::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
1716std::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
Original file line number Diff line number Diff line change 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
86namespace 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
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 ( ) ;
You can’t perform that action at this time.
0 commit comments