Add RestListService and RestDetailService; update README.

This commit is contained in:
Chunting Gu
2018-05-12 19:09:37 +08:00
parent 5e6f125f90
commit 189a246221
10 changed files with 321 additions and 87 deletions
+1
View File
@@ -29,6 +29,7 @@ set(SRCS
queue.h
rest_server.cc
rest_server.h
rest_service.cc
rest_service.h
url.cc
url.h
+4 -2
View File
@@ -5,6 +5,8 @@
#if WEBCC_ENABLE_LOG
#include <cstring> // for strrchr()
namespace webcc {
enum LogLevel {
@@ -32,7 +34,7 @@ void LogWrite(int level, const char* file, int line, const char* format, ...);
#if (defined(WIN32) || defined(_WIN64))
// See: https://stackoverflow.com/a/8488201
#define __FILENAME__ strrchr("\\" __FILE__, '\\') + 1
#define __FILENAME__ std::strrchr("\\" __FILE__, '\\') + 1
#define LOG_VERB(format, ...) \
webcc::LogWrite(webcc::VERB, __FILENAME__, __LINE__, format, ##__VA_ARGS__);
@@ -52,7 +54,7 @@ void LogWrite(int level, const char* file, int line, const char* format, ...);
#else
// See: https://stackoverflow.com/a/8488201
#define __FILENAME__ strrchr("/" __FILE__, '/') + 1
#define __FILENAME__ std::strrchr("/" __FILE__, '/') + 1
#define LOG_VERB(format, args...) \
webcc::LogWrite(webcc::VERB, __FILENAME__, __LINE__, format, ##args);
+53
View File
@@ -0,0 +1,53 @@
#include "webcc/rest_service.h"
#include "webcc/logger.h"
namespace webcc {
bool RestListService::Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches,
const UrlQuery& query,
const std::string& request_content,
std::string* response_content) {
if (http_method == kHttpGet) {
return Get(query, response_content);
}
if (http_method == kHttpPost) {
return Post(request_content, response_content);
}
LOG_ERRO("RestListService doesn't support '%s' method.", http_method.c_str());
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool RestDetailService::Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches,
const UrlQuery& query,
const std::string& request_content,
std::string* response_content) {
if (http_method == kHttpGet) {
return Get(url_sub_matches, response_content);
}
if (http_method == kHttpPut) {
return Put(url_sub_matches, request_content, response_content);
}
if (http_method == kHttpPatch) {
return Patch(url_sub_matches, request_content, response_content);
}
if (http_method == kHttpDelete) {
return Delete(url_sub_matches);
}
LOG_ERRO("RestDetailService doesn't support '%s' method.",
http_method.c_str());
return false;
}
} // namespace webcc
+66
View File
@@ -1,6 +1,13 @@
#ifndef WEBCC_REST_SERVICE_H_
#define WEBCC_REST_SERVICE_H_
// NOTE:
// The design of RestListService and RestDetailService is very similar to
// XxxListView and XxxDetailView in Django Rest Framework.
// Deriving from them instead of RestService can simplify your own REST services
// a lot. But if you find the filtered parameters cannot meet your needs, feel
// free to derive from RestService directly.
#include <map>
#include <memory>
#include <string>
@@ -12,6 +19,8 @@ namespace webcc {
class UrlQuery;
////////////////////////////////////////////////////////////////////////////////
// Base class for your REST service.
class RestService {
public:
@@ -34,6 +43,63 @@ public:
typedef std::shared_ptr<RestService> RestServicePtr;
////////////////////////////////////////////////////////////////////////////////
class RestListService : public RestService {
public:
bool Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches,
const UrlQuery& query,
const std::string& request_content,
std::string* response_content) final;
protected:
RestListService() = default;
virtual bool Get(const UrlQuery& query,
std::string* response_content) {
return false;
}
virtual bool Post(const std::string& request_content,
std::string* response_content) {
return false;
}
};
////////////////////////////////////////////////////////////////////////////////
class RestDetailService : public RestService {
public:
bool Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches,
const UrlQuery& query,
const std::string& request_content,
std::string* response_content) final;
protected:
virtual bool Get(const std::vector<std::string>& url_sub_matches,
std::string* response_content) {
return false;
}
virtual bool Put(const std::vector<std::string>& url_sub_matches,
const std::string& request_content,
std::string* response_content) {
return false;
}
virtual bool Patch(const std::vector<std::string>& url_sub_matches,
const std::string& request_content,
std::string* response_content) {
return false;
}
virtual bool Delete(const std::vector<std::string>& url_sub_matches) {
return false;
}
};
} // namespace webcc
#endif // WEBCC_REST_SERVICE_H_