mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-08-03 12:50:02 +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) {
|
void IndexResponse::on_send (std::ostream& parStream) {
|
||||||
parStream <<
|
parStream <<
|
||||||
R"(
|
R"(
|
||||||
<form action="http://127.0.0.1:8080" method="POST" accept-charset="UTF-8">
|
<form action="http://127.0.0.1:8080/paste.cgi" method="POST" accept-charset="UTF-8">
|
||||||
<textarea name="tawashi" cols="80" rows="24"></textarea>
|
<textarea name="pastie" cols="80" rows="24"></textarea>
|
||||||
<br>
|
<br>
|
||||||
<button type="submit">tawashi</button>
|
<button type="submit">tawashi</button>
|
||||||
</br>
|
</br>
|
||||||
|
|
|
@ -14,6 +14,7 @@ int main() {
|
||||||
//std::cout << "Content-type:text/plain\n\n";
|
//std::cout << "Content-type:text/plain\n\n";
|
||||||
|
|
||||||
redis::IncRedis incredis("127.0.0.1", 6379);
|
redis::IncRedis incredis("127.0.0.1", 6379);
|
||||||
|
incredis.connect();
|
||||||
|
|
||||||
tawashi::CGIEnv cgi_env;
|
tawashi::CGIEnv cgi_env;
|
||||||
if (cgi_env.path_info() == "/index.cgi") {
|
if (cgi_env.path_info() == "/index.cgi") {
|
||||||
|
@ -21,10 +22,13 @@ int main() {
|
||||||
resp.send();
|
resp.send();
|
||||||
}
|
}
|
||||||
else if (cgi_env.path_info() == "/paste.cgi") {
|
else if (cgi_env.path_info() == "/paste.cgi") {
|
||||||
incredis.connect();
|
|
||||||
tawashi::SubmitFormResponse resp(incredis);
|
tawashi::SubmitFormResponse resp(incredis);
|
||||||
resp.send();
|
resp.send();
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "Content-type:text/plain\n\n";
|
||||||
|
std::cout << "you shouldn't be here\n";
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,13 +32,13 @@ namespace tawashi {
|
||||||
//}
|
//}
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
std::string make_token (uint64_t parNum) {
|
std::string num_to_token (int64_t parNum) {
|
||||||
assert(0 != parNum);
|
assert(0 < parNum);
|
||||||
std::string retval;
|
std::string retval;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
const auto remainder = parNum % ('z' - 'a' + 1);
|
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);
|
parNum /= ('z' - 'a' + 1);
|
||||||
} while (parNum);
|
} while (parNum);
|
||||||
|
|
||||||
|
|
|
@ -5,5 +5,5 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
std::string make_token (uint64_t parNum) a_pure;
|
std::string num_to_token (int64_t parNum) a_pure;
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -1,18 +1,16 @@
|
||||||
#include "submit_form_response.hpp"
|
#include "submit_form_response.hpp"
|
||||||
#include "incredis/incredis.hpp"
|
#include "incredis/incredis.hpp"
|
||||||
#include "cgi_post.hpp"
|
#include "cgi_post.hpp"
|
||||||
|
#include "num_to_token.hpp"
|
||||||
|
#include <ciso646>
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
namespace {
|
namespace {
|
||||||
const char g_post_key[] = "tawashi";
|
const char g_post_key[] = "pastie";
|
||||||
|
|
||||||
bool submit_to_redis (const std::string& parText) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
SubmitFormResponse::SubmitFormResponse (redis::IncRedis& parRedis) :
|
SubmitFormResponse::SubmitFormResponse (redis::IncRedis& parRedis) :
|
||||||
Response("text/html"),
|
Response("text/plain"),
|
||||||
m_redis(parRedis)
|
m_redis(parRedis)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -20,11 +18,28 @@ namespace tawashi {
|
||||||
void SubmitFormResponse::on_send (std::ostream& parStream) {
|
void SubmitFormResponse::on_send (std::ostream& parStream) {
|
||||||
auto post = cgi::read_post(cgi_env());
|
auto post = cgi::read_post(cgi_env());
|
||||||
auto post_data_it = post.find(g_post_key);
|
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";
|
parStream << "can't find POST data\n";
|
||||||
}
|
}
|
||||||
else if (submit_to_redis(post_data_it->second)) {
|
else if (submit_to_redis(post_data_it->second)) {
|
||||||
parStream << "post submitted correctly\n";
|
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
|
} //namespace tawashi
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "response.hpp"
|
#include "response.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace redis {
|
namespace redis {
|
||||||
class IncRedis;
|
class IncRedis;
|
||||||
|
@ -13,6 +14,8 @@ namespace tawashi {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void on_send (std::ostream& parStream) override;
|
virtual void on_send (std::ostream& parStream) override;
|
||||||
|
bool submit_to_redis (const std::string& parText) const;
|
||||||
|
|
||||||
redis::IncRedis& m_redis;
|
redis::IncRedis& m_redis;
|
||||||
};
|
};
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue