Skip to content

Commit

Permalink
add cpp event and http templates
Browse files Browse the repository at this point in the history
  • Loading branch information
SquatsTonight committed Dec 14, 2020
1 parent 85a0a3c commit d1f2d67
Show file tree
Hide file tree
Showing 181 changed files with 32,354 additions and 206 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,7 @@ custom-fsharp/*/.vs
custom-fsharp/*/NuGetScratch
custom-fsharp/*/.NETCoreApp,Version=v3.1.AssemblyAttributes.fs
custom-fsharp/*/.dotnet
custom-cpp/*/bin
custom-cpp/*/sample/release
event-custom-cpp/*/bin
event-custom-cpp/*/sample/release
http-custom-cpp/*/bin
http-custom-cpp/*/sample/release
3 changes: 2 additions & 1 deletion src/lib/init/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"custom-ruby": "{{ templatePath }}/custom-ruby",
"custom-rust": "{{ templatePath }}/custom-rust",
"custom-typescript": "{{ templatePath }}/custom-typescript",
"custom-cpp": "{{ templatePath }}/custom-cpp"
"event-custom-cpp": "{{ templatePath }}/event-custom-cpp",
"http-custom-cpp": "{{ templatePath }}/http-custom-cpp"
}
148 changes: 0 additions & 148 deletions templates/custom-cpp/{{ projectName }}/README.md

This file was deleted.

8 changes: 8 additions & 0 deletions templates/event-custom-cpp/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "event-custom-cpp",
"description": "cpp template for Custom Runtime",
"vars": {
"service": "{{ projectName }}"
},
"copyOnlyPaths": [ "libpistache.a" ]
}
10 changes: 10 additions & 0 deletions templates/event-custom-cpp/{{ projectName }}/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
build-img:
docker build -t fc-cpp-runtime -f build-image/Dockerfile build-image

build: build-img
docker run --rm -it -v $$(pwd):/tmp fc-cpp-runtime bash -c "cd /tmp && ./build.sh"

deploy: build
fun deploy -y

.PHONY: deploy
11 changes: 11 additions & 0 deletions templates/event-custom-cpp/{{ projectName }}/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## CPP template for Custom Runtime

### Deploy

```bash
$ event-custom-cpp make deploy
```
### Invoke
```bash
$ event-custom-cpp fun invoke -e "HelloCustomCpp"
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Aliyun {
namespace FC {
CUSTOM_HEADER2(X_Fc_Log, "x-fc-log-result") // test only
FcBaseHandler* CustomRuntimeHandler::normalHandler = nullptr;
FcHttpBaseHandler* CustomRuntimeHandler::httpHandler = nullptr;

void CustomRuntimeHandler::onRequest(
const Http::Request& req,
Expand Down Expand Up @@ -46,43 +45,21 @@ void CustomRuntimeHandler::onRequest(
if (req.method() == Http::Method::Post) {
logger.LogInfo("handling initialize");
LogString::StartInit(fcContext.requestId);
if (CustomRuntimeHandler::normalHandler == nullptr && CustomRuntimeHandler::httpHandler == nullptr )
if (CustomRuntimeHandler::normalHandler == nullptr)
{
response.send(Http::Code::Internal_Server_Error, "The init handler is not registered.");
}
else {
if (CustomRuntimeHandler::normalHandler) {
FcBaseHandler *fcHandler = CustomRuntimeHandler::normalHandler;
fcHandler->SetLogger(&logger);
fcHandler->OnInitialize(fcContext);
}
FcBaseHandler *fcHandler = CustomRuntimeHandler::normalHandler;
fcHandler->SetLogger(&logger);
fcHandler->OnInitialize(fcContext);

if (CustomRuntimeHandler::httpHandler) {
FcHttpBaseHandler *fcHandler = CustomRuntimeHandler::httpHandler;
fcHandler->SetLogger(&logger);
fcHandler->OnInitialize(fcContext);
}
LogString::EndInit(fcContext.requestId);
initLog = logger.GetLog();
response.send(Http::Code::Ok);
handled = true;
}
}
} else if (cmd == "/http-invoke") {
logger.LogInfo("handling http invoke");
if (CustomRuntimeHandler::httpHandler == nullptr)
{
response.send(Http::Code::Internal_Server_Error, "The http handler is not registered.");
}
else {
FcHttpBaseHandler * fcHttpHandler = CustomRuntimeHandler::httpHandler;
fcHttpHandler->SetLogger(&logger);
LogString::StartInvoke(fcContext.requestId);
response.headers().add<X_Fc_Log>(initLog + logger.GetLog());
fcHttpHandler->OnInvoke(fcContext, req, response);
LogString::EndInvoke(fcContext.requestId);
handled = true;
}
}

if (!handled){
Expand Down
78 changes: 78 additions & 0 deletions templates/event-custom-cpp/{{ projectName }}/include/handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once
#include <pistache/net.h>
#include <pistache/http.h>
#include <pistache/peer.h>
#include <pistache/http_headers.h>
#include <pistache/cookie.h>
#include <pistache/endpoint.h>
#include <pistache/common.h>
#include <queue>
#include <map>
#include <chrono>
#include "fc_context.h"
#include "logging.h"
namespace Aliyun {
namespace FC {
class FcBaseHandler
{
public:
FcBaseHandler() :mLogger(nullptr) {}
void SetLogger(Logger* logger) {mLogger = logger;}
virtual void OnInvoke(const std::string& payload, const FcContext& context, std::string& response) = 0;
virtual void OnInitialize(const FcContext& context) = 0;

private:
Logger* mLogger;
};

class FcHttpBaseHandler
{
public:
FcHttpBaseHandler() :mLogger(nullptr) {}
void SetLogger(Logger* logger) {mLogger = logger;}
virtual void OnInvoke(const FcContext& context, const Pistache::Http::Request& req,
Pistache::Http::ResponseWriter& response) = 0;
virtual void OnInitialize(const FcContext& context) = 0;

private:
Logger* mLogger;
};

class CustomRuntimeHandler : public Pistache::Http::Handler {

HTTP_PROTOTYPE(CustomRuntimeHandler)

public:
void onRequest(
const Pistache::Http::Request& req,
Pistache::Http::ResponseWriter response) override;

static FcBaseHandler* normalHandler;
};

#define CUSTOM_HEADER2(header_name, header_real_name) \
class header_name : public Pistache::Http::Header::Header { \
public: \
NAME(header_real_name) \
\
header_name() = default; \
\
explicit header_name(const char* value) \
: value_{value} {} \
\
explicit header_name(std::string value) \
: value_(std::move(value)) {} \
\
void parseRaw(const char *str, size_t len) final \
{ value_ = { str, len };} \
\
void write(std::ostream& os) const final \
{ os << value_; }; \
\
std::string val() const { return value_; }; \
\
private: \
std::string value_; \
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "handler.h"
namespace Aliyun {
namespace FC {
namespace Handlers {
class EchoHandler : public FcBaseHandler
{
public:
void OnInvoke(const std::string& payload, const FcContext& context, std::string& response) override;
void OnInitialize(const FcContext& context) override;
private:
static std::string mInitHandler;
};
}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "echo_handler.h"
using namespace std;
using namespace Pistache;

namespace Aliyun {
namespace FC {
namespace Handlers {
std::string EchoHandler::mInitHandler;
void EchoHandler::OnInvoke(const string& payload, const FcContext& context, string& response)
{
response = EchoHandler::mInitHandler + payload;
}

void EchoHandler::OnInitialize(const FcContext& context)
{
EchoHandler::mInitHandler = context.initializer;
}
}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* register invoke and init handler.
*/
#include "echo_handler.h"
#include "entrypoint.h"
using namespace Aliyun::FC::Handlers;
void SetInvokeAndInitHander()
{
CustomRuntimeHandler::normalHandler = new EchoHandler();
}
Loading

0 comments on commit d1f2d67

Please sign in to comment.