diff --git a/api/Makefile b/api/Makefile new file mode 100644 index 000000000..0d90c05f7 --- /dev/null +++ b/api/Makefile @@ -0,0 +1,21 @@ +CXX=clang++ +CFLAGS=-std=c++11 -Wall -O2 +LDFLAGS=-luv -lhttp_parser ../speedy/r3/.libs/libr3.a -L/usr/local/lib -lpcre +# debug only +INC=-I../ +SOURCES=$(wildcard *.cpp) +EXECUTABLE=test.out +# OBJECTS=$(SOURCES:.cpp=.o) + +all: $(EXECUTABLE) + +$(EXECUTABLE): $(SOURCES) + $(CXX) $(CFLAGS) $(SOURCES) -o $(EXECUTABLE) $(INC) $(LDFLAGS) + +# .cpp.o: +# $(CXX) $(CFLAGS) $< -o $@ $(LDFLAGS) $(INC) + +.PHONY: +clean: + rm -f *.out + rm -f *.o diff --git a/api/resource.hpp b/api/resource.hpp new file mode 100644 index 000000000..92353fc76 --- /dev/null +++ b/api/resource.hpp @@ -0,0 +1,75 @@ +#ifndef MEMGRAPH_API_RESOURCE_HPP +#define MEMGRAPH_API_RESOURCE_HPP + +#include <memory> +#include "speedy/speedy.hpp" +#include "utils/crtp.hpp" + +namespace api +{ + +struct GET +{ + GET() = default; + + template <class T> + void link(speedy::Speedy& app, const std::string& path, T& resource) + { + using namespace std::placeholders; + app.get(path, std::bind(&T::get, resource, _1, _2)); + } +}; + +struct POST +{ + POST() = default; + + template <class T> + void link(speedy::Speedy& app, const std::string& path, T& resource) + { + using namespace std::placeholders; + app.post(path, std::bind(&T::post, resource, _1, _2)); + } +}; + +namespace detail +{ + +template <class T, class M> +struct Method : public M +{ + Method(speedy::Speedy& app, const std::string& path) + { + M::link(app, path, static_cast<T&>(*this)); + } +}; + +template <class T, class... Ms> +struct Methods; + +template <class T, class M, class... Ms> +struct Methods<T, M, Ms...> : public Method<T, M>, public Methods<T, Ms...> +{ + Methods(speedy::Speedy& app, const std::string& path) + : Method<T, M>(app, path), Methods<T, Ms...>(app, path) {} +}; + +template <class T, class M> +struct Methods<T, M> : public Method<T, M> +{ + using Method<T, M>::Method; +}; + +} + +template <class T, class... Ms> +class Resource : public detail::Methods<T, Ms...> +{ +public: + Resource(speedy::Speedy& app, const std::string& path) + : detail::Methods<T, Ms...>(app, path) {} +}; + +} + +#endif diff --git a/api/test.cpp b/api/test.cpp new file mode 100644 index 000000000..533d21db5 --- /dev/null +++ b/api/test.cpp @@ -0,0 +1,35 @@ +#include <iostream> + +#include "speedy/speedy.hpp" +#include "resource.hpp" + +class Animal : public api::Resource<Animal, api::GET, api::POST> +{ +public: + Animal(speedy::Speedy& app) : Resource(app, "/animal") {} + + void get(http::Request& req, http::Response& res) + { + return res.send("Ok, here is a Dog"); + } + + void post(http::Request& req, http::Response& res) + { + return res.send("Oh, you gave me an animal?"); + } +}; + +int main(void) +{ + uv::UvLoop loop; + speedy::Speedy app(loop); + + auto animal = Animal(app); + + http::Ipv4 ip("0.0.0.0", 3400); + app.listen(ip); + + loop.run(uv::UvLoop::Mode::Default); + + return 0; +}