mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-12-27 21:35:41 +00:00
Submitting a paste and saving to redis works!
This commit is contained in:
parent
e32cecedba
commit
dbd1a3a90a
7 changed files with 37 additions and 15 deletions
|
@ -1 +1 @@
|
|||
Subproject commit ee0767e52c73402f7832bf84b2f00b36aae62c2d
|
||||
Subproject commit 89da7b33fe578fc545deebfb26dafbc80362a164
|
|
@ -9,8 +9,8 @@ namespace tawashi {
|
|||
void IndexResponse::on_send (std::ostream& parStream) {
|
||||
parStream <<
|
||||
R"(
|
||||
<form action="http://127.0.0.1:8080" method="POST" accept-charset="UTF-8">
|
||||
<textarea name="tawashi" cols="80" rows="24"></textarea>
|
||||
<form action="http://127.0.0.1:8080/paste.cgi" method="POST" accept-charset="UTF-8">
|
||||
<textarea name="pastie" cols="80" rows="24"></textarea>
|
||||
<br>
|
||||
<button type="submit">tawashi</button>
|
||||
</br>
|
||||
|
|
|
@ -14,6 +14,7 @@ int main() {
|
|||
//std::cout << "Content-type:text/plain\n\n";
|
||||
|
||||
redis::IncRedis incredis("127.0.0.1", 6379);
|
||||
incredis.connect();
|
||||
|
||||
tawashi::CGIEnv cgi_env;
|
||||
if (cgi_env.path_info() == "/index.cgi") {
|
||||
|
@ -21,10 +22,13 @@ int main() {
|
|||
resp.send();
|
||||
}
|
||||
else if (cgi_env.path_info() == "/paste.cgi") {
|
||||
incredis.connect();
|
||||
tawashi::SubmitFormResponse resp(incredis);
|
||||
resp.send();
|
||||
}
|
||||
else {
|
||||
std::cout << "Content-type:text/plain\n\n";
|
||||
std::cout << "you shouldn't be here\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -32,13 +32,13 @@ namespace tawashi {
|
|||
//}
|
||||
} //unnamed namespace
|
||||
|
||||
std::string make_token (uint64_t parNum) {
|
||||
assert(0 != parNum);
|
||||
std::string num_to_token (int64_t parNum) {
|
||||
assert(0 < parNum);
|
||||
std::string retval;
|
||||
|
||||
do {
|
||||
const auto remainder = parNum % ('z' - 'a' + 1);
|
||||
retval.push_back(static_cast<char>(remainder));
|
||||
retval.push_back(static_cast<char>('a' + remainder));
|
||||
parNum /= ('z' - 'a' + 1);
|
||||
} while (parNum);
|
||||
|
||||
|
|
|
@ -5,5 +5,5 @@
|
|||
#include <cstdint>
|
||||
|
||||
namespace tawashi {
|
||||
std::string make_token (uint64_t parNum) a_pure;
|
||||
std::string num_to_token (int64_t parNum) a_pure;
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
#include "submit_form_response.hpp"
|
||||
#include "incredis/incredis.hpp"
|
||||
#include "cgi_post.hpp"
|
||||
#include "num_to_token.hpp"
|
||||
#include <ciso646>
|
||||
|
||||
namespace tawashi {
|
||||
namespace {
|
||||
const char g_post_key[] = "tawashi";
|
||||
|
||||
bool submit_to_redis (const std::string& parText) {
|
||||
return true;
|
||||
}
|
||||
const char g_post_key[] = "pastie";
|
||||
} //unnamed namespace
|
||||
|
||||
SubmitFormResponse::SubmitFormResponse (redis::IncRedis& parRedis) :
|
||||
Response("text/html"),
|
||||
Response("text/plain"),
|
||||
m_redis(parRedis)
|
||||
{
|
||||
}
|
||||
|
@ -20,11 +18,28 @@ namespace tawashi {
|
|||
void SubmitFormResponse::on_send (std::ostream& parStream) {
|
||||
auto post = cgi::read_post(cgi_env());
|
||||
auto post_data_it = post.find(g_post_key);
|
||||
if (post.end() != post_data_it) {
|
||||
if (post.end() == post_data_it) {
|
||||
parStream << "can't find POST data\n";
|
||||
}
|
||||
else if (submit_to_redis(post_data_it->second)) {
|
||||
parStream << "post submitted correctly\n";
|
||||
}
|
||||
else {
|
||||
parStream << "something happened? :/\n";
|
||||
}
|
||||
}
|
||||
|
||||
bool SubmitFormResponse::submit_to_redis (const std::string& parText) const {
|
||||
if (not m_redis.is_connected()) {
|
||||
m_redis.connect();
|
||||
m_redis.wait_for_connect();
|
||||
if (not m_redis.is_connected())
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto next_id = m_redis.incr("paste_counter");
|
||||
const std::string token = num_to_token(next_id);
|
||||
assert(not token.empty());
|
||||
return m_redis.set(token, parText);
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "response.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace redis {
|
||||
class IncRedis;
|
||||
|
@ -13,6 +14,8 @@ namespace tawashi {
|
|||
|
||||
private:
|
||||
virtual void on_send (std::ostream& parStream) override;
|
||||
bool submit_to_redis (const std::string& parText) const;
|
||||
|
||||
redis::IncRedis& m_redis;
|
||||
};
|
||||
} //namespace tawashi
|
||||
|
|
Loading…
Reference in a new issue