add: CORS and OPTIONS

add: 'off' schedule in db init
add: copy db to build dir
This commit is contained in:
Tobias Reisinger 2019-09-08 23:24:31 +02:00
parent e93ea44350
commit 8679bfe680
7 changed files with 32 additions and 59 deletions

35
.gitignore vendored
View file

@ -1,36 +1,5 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
build build
cmake-build-debug cmake-build-debug
.idea .idea
core.sqlite

View file

@ -58,6 +58,9 @@ foreach(cspFile ${SCP_LIST})
set(VIEWSRC ${VIEWSRC} ${classname}.cc) set(VIEWSRC ${VIEWSRC} ${classname}.cc)
endforeach() endforeach()
configure_file("config.json" "config.json" COPYONLY)
configure_file("core.sqlite" "core.sqlite" COPYONLY)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(core ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC} ${PLUGIN_SRC} ${MODEL_SRC} ${HELPER_SRC}) add_executable(core ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC} ${PLUGIN_SRC} ${MODEL_SRC} ${HELPER_SRC})

View file

@ -1,23 +1,21 @@
#pragma once #pragma once
#include <drogon/HttpController.h> #include <drogon/HttpController.h>
using namespace drogon; using namespace drogon;
namespace api namespace api::v1
{
namespace v1
{ {
class controllers:public drogon::HttpController<controllers> class controllers:public drogon::HttpController<controllers>
{ {
public: public:
METHOD_LIST_BEGIN METHOD_LIST_BEGIN
METHOD_ADD(controllers::post_discover, "/discover", Post); METHOD_ADD(controllers::post_discover, "/discover", Post, Options);
METHOD_ADD(controllers::get_all, "/", Get); METHOD_ADD(controllers::get_all, "/", Get, Options);
METHOD_ADD(controllers::get_one_by_id, "/{1}", Get); METHOD_ADD(controllers::get_one_by_id, "/{1}", Get, Options);
METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete); METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete, Options);
METHOD_ADD(controllers::put_one_by_id, "/{1}", Put, "filters::json_required", "filters::controllers::valid_json"); METHOD_ADD(controllers::put_one_by_id, "/{1}", Put, Options, "filters::json_required", "filters::controllers::valid_json");
METHOD_ADD(controllers::get_relays_all, "/{1}/relays/", Get); METHOD_ADD(controllers::get_relays_all, "/{1}/relays/", Get, Options);
METHOD_ADD(controllers::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get); METHOD_ADD(controllers::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get, Options);
METHOD_ADD(controllers::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, "filters::json_required", "filters::relays::valid_json"); METHOD_ADD(controllers::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, Options, "filters::json_required", "filters::relays::valid_json");
METHOD_LIST_END METHOD_LIST_END
static void post_discover(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback); static void post_discover(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
@ -31,4 +29,3 @@ namespace api
static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num); static void put_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
}; };
} }
}

View file

@ -1,18 +1,15 @@
#pragma once #pragma once
#include <drogon/HttpController.h> #include <drogon/HttpController.h>
using namespace drogon; using namespace drogon;
namespace api namespace api::v1
{
namespace v1
{ {
class relays:public drogon::HttpController<relays> class relays:public drogon::HttpController<relays>
{ {
public: public:
METHOD_LIST_BEGIN METHOD_LIST_BEGIN
METHOD_ADD(relays::get_all, "/", Get); METHOD_ADD(relays::get_all, "/", Get, Options);
METHOD_LIST_END METHOD_LIST_END
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback); static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
}; };
} }
}

View file

@ -1,19 +1,17 @@
#pragma once #pragma once
#include <drogon/HttpController.h> #include <drogon/HttpController.h>
using namespace drogon; using namespace drogon;
namespace api namespace api::v1
{
namespace v1
{ {
class schedules:public drogon::HttpController<schedules> class schedules:public drogon::HttpController<schedules>
{ {
public: public:
METHOD_LIST_BEGIN METHOD_LIST_BEGIN
METHOD_ADD(schedules::post_new, "/", Post, "filters::json_required", "filters::schedules::valid_json"); METHOD_ADD(schedules::post_new, "/", Post, Options, "filters::json_required", "filters::schedules::valid_json");
METHOD_ADD(schedules::get_all, "/", Get); METHOD_ADD(schedules::get_all, "/", Get, Options);
METHOD_ADD(schedules::get_one_by_id, "/{1}", Get); METHOD_ADD(schedules::get_one_by_id, "/{1}", Get, Options);
METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete); METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete, Options);
METHOD_ADD(schedules::put_one_by_id, "/{1}", Put, "filters::json_required", "filters::schedules::valid_json"); METHOD_ADD(schedules::put_one_by_id, "/{1}", Put, Options, "filters::json_required", "filters::schedules::valid_json");
//METHOD_ADD(controllers::get_relays_all,"/{1}/relays",Get); //METHOD_ADD(controllers::get_relays_all,"/{1}/relays",Get);
//METHOD_ADD(controllers::get_relays_one,"/{1}/relays/{2}",Get); //METHOD_ADD(controllers::get_relays_one,"/{1}/relays/{2}",Get);
METHOD_LIST_END METHOD_LIST_END
@ -27,4 +25,3 @@ namespace api
//void get_relays_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id,std::string relay_id); //void get_relays_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id,std::string relay_id);
}; };
} }
}

View file

@ -7,6 +7,12 @@
#include "globals.h" #include "globals.h"
#include "config.h" #include "config.h"
static void
add_cors_headers(const drogon::HttpRequestPtr &requestPtr, const drogon::HttpResponsePtr &responsePtr)
{
responsePtr->addHeader("Access-Control-Allow-Origin", "*");
}
static void static void
terminate(int signum) terminate(int signum)
{ {
@ -43,6 +49,8 @@ main()
//Load config file //Load config file
drogon::app().loadConfigFile("config.json"); drogon::app().loadConfigFile("config.json");
drogon::app().registerPostHandlingAdvice(add_cors_headers);
//drogon::app().getLoop()->runEvery(1, &test); //drogon::app().getLoop()->runEvery(1, &test);
//Run HTTP framework,the method will block in the internal event loop //Run HTTP framework,the method will block in the internal event loop

View file

@ -36,3 +36,5 @@ create table schedules
name VARCHAR(128), name VARCHAR(128),
periods BLOB periods BLOB
); );
INSERT INTO schedules (id, name, periods) VALUES ('off', 'off', x'00');