add: CORS and OPTIONS
add: 'off' schedule in db init add: copy db to build dir
This commit is contained in:
		
							parent
							
								
									e93ea44350
								
							
						
					
					
						commit
						8679bfe680
					
				
					 7 changed files with 32 additions and 59 deletions
				
			
		
							
								
								
									
										35
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										35
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
cmake-build-debug
 | 
			
		||||
.idea
 | 
			
		||||
 | 
			
		||||
core.sqlite
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -58,6 +58,9 @@ foreach(cspFile ${SCP_LIST})
 | 
			
		|||
    set(VIEWSRC ${VIEWSRC} ${classname}.cc)
 | 
			
		||||
endforeach()
 | 
			
		||||
 | 
			
		||||
configure_file("config.json" "config.json" COPYONLY)
 | 
			
		||||
configure_file("core.sqlite" "core.sqlite" COPYONLY)
 | 
			
		||||
 | 
			
		||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
 | 
			
		||||
add_executable(core ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC} ${PLUGIN_SRC} ${MODEL_SRC} ${HELPER_SRC})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,23 +1,21 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
#include <drogon/HttpController.h>
 | 
			
		||||
using namespace drogon;
 | 
			
		||||
namespace api
 | 
			
		||||
{
 | 
			
		||||
    namespace v1
 | 
			
		||||
namespace api::v1
 | 
			
		||||
    {
 | 
			
		||||
        class controllers:public drogon::HttpController<controllers>
 | 
			
		||||
        {
 | 
			
		||||
        public:
 | 
			
		||||
            METHOD_LIST_BEGIN
 | 
			
		||||
            METHOD_ADD(controllers::post_discover, "/discover", Post);
 | 
			
		||||
            METHOD_ADD(controllers::get_all, "/", Get);
 | 
			
		||||
            METHOD_ADD(controllers::get_one_by_id, "/{1}", Get);
 | 
			
		||||
            METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete);
 | 
			
		||||
            METHOD_ADD(controllers::put_one_by_id, "/{1}", Put, "filters::json_required", "filters::controllers::valid_json");
 | 
			
		||||
            METHOD_ADD(controllers::post_discover, "/discover", Post, Options);
 | 
			
		||||
            METHOD_ADD(controllers::get_all, "/", Get, Options);
 | 
			
		||||
            METHOD_ADD(controllers::get_one_by_id, "/{1}", Get, Options);
 | 
			
		||||
            METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete, Options);
 | 
			
		||||
            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_one_by_id_and_num, "/{1}/relays/{2}", Get);
 | 
			
		||||
            METHOD_ADD(controllers::put_relays_one_by_id_and_num, "/{1}/relays/{2}", Put, "filters::json_required", "filters::relays::valid_json");
 | 
			
		||||
            METHOD_ADD(controllers::get_relays_all, "/{1}/relays/", Get, Options);
 | 
			
		||||
            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, Options, "filters::json_required", "filters::relays::valid_json");
 | 
			
		||||
            METHOD_LIST_END
 | 
			
		||||
 | 
			
		||||
            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);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,18 +1,15 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
#include <drogon/HttpController.h>
 | 
			
		||||
using namespace drogon;
 | 
			
		||||
namespace api
 | 
			
		||||
{
 | 
			
		||||
    namespace v1
 | 
			
		||||
namespace api::v1
 | 
			
		||||
    {
 | 
			
		||||
        class relays:public drogon::HttpController<relays>
 | 
			
		||||
        {
 | 
			
		||||
        public:
 | 
			
		||||
            METHOD_LIST_BEGIN
 | 
			
		||||
            METHOD_ADD(relays::get_all, "/", Get);
 | 
			
		||||
            METHOD_ADD(relays::get_all, "/", Get, Options);
 | 
			
		||||
            METHOD_LIST_END
 | 
			
		||||
 | 
			
		||||
            static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,19 +1,17 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
#include <drogon/HttpController.h>
 | 
			
		||||
using namespace drogon;
 | 
			
		||||
namespace api
 | 
			
		||||
{
 | 
			
		||||
    namespace v1
 | 
			
		||||
namespace api::v1
 | 
			
		||||
    {
 | 
			
		||||
        class schedules:public drogon::HttpController<schedules>
 | 
			
		||||
        {
 | 
			
		||||
        public:
 | 
			
		||||
            METHOD_LIST_BEGIN
 | 
			
		||||
            METHOD_ADD(schedules::post_new, "/", Post, "filters::json_required", "filters::schedules::valid_json");
 | 
			
		||||
            METHOD_ADD(schedules::get_all, "/", Get);
 | 
			
		||||
            METHOD_ADD(schedules::get_one_by_id, "/{1}", Get);
 | 
			
		||||
            METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete);
 | 
			
		||||
            METHOD_ADD(schedules::put_one_by_id, "/{1}", Put, "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, Options);
 | 
			
		||||
            METHOD_ADD(schedules::get_one_by_id, "/{1}", Get, Options);
 | 
			
		||||
            METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete, Options);
 | 
			
		||||
            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_one,"/{1}/relays/{2}",Get);
 | 
			
		||||
            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);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										8
									
								
								main.cc
									
										
									
									
									
								
							
							
						
						
									
										8
									
								
								main.cc
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -7,6 +7,12 @@
 | 
			
		|||
#include "globals.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
 | 
			
		||||
terminate(int signum)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +49,8 @@ main()
 | 
			
		|||
    //Load config file
 | 
			
		||||
    drogon::app().loadConfigFile("config.json");
 | 
			
		||||
 | 
			
		||||
    drogon::app().registerPostHandlingAdvice(add_cors_headers);
 | 
			
		||||
 | 
			
		||||
    //drogon::app().getLoop()->runEvery(1, &test);
 | 
			
		||||
 | 
			
		||||
    //Run HTTP framework,the method will block in the internal event loop
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,3 +36,5 @@ create table schedules
 | 
			
		|||
    name    VARCHAR(128),
 | 
			
		||||
    periods BLOB
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
INSERT INTO schedules (id, name, periods) VALUES ('off', 'off', x'00');
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue