database stuff
This commit is contained in:
@@ -13,3 +13,9 @@
|
|||||||
[submodule "3rdparty/jwt-cpp"]
|
[submodule "3rdparty/jwt-cpp"]
|
||||||
path = 3rdparty/jwt-cpp
|
path = 3rdparty/jwt-cpp
|
||||||
url = https://github.com/Thalhammer/jwt-cpp
|
url = https://github.com/Thalhammer/jwt-cpp
|
||||||
|
[submodule "3rdparty/ormpp"]
|
||||||
|
path = 3rdparty/ormpp
|
||||||
|
url = https://github.com/qicosmos/ormpp
|
||||||
|
[submodule "3rdparty/mysql-connector-cpp"]
|
||||||
|
path = 3rdparty/mysql-connector-cpp
|
||||||
|
url = https://github.com/mysql/mysql-connector-cpp
|
||||||
|
|||||||
+1
Submodule 3rdparty/mysql-connector-cpp added at 64731f9625
+19
-1
@@ -5,9 +5,12 @@ project(icarus)
|
|||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
|
#add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/appComponent.hpp
|
src/appComponent.hpp
|
||||||
src/controller/loginController.hpp
|
src/controller/loginController.hpp
|
||||||
|
src/database/base_repository.cpp
|
||||||
src/directory_manager.cpp
|
src/directory_manager.cpp
|
||||||
src/dto/loginResultDto.hpp
|
src/dto/loginResultDto.hpp
|
||||||
src/imageFile.cpp
|
src/imageFile.cpp
|
||||||
@@ -16,6 +19,7 @@ set(SOURCES
|
|||||||
src/token_manager.cpp
|
src/token_manager.cpp
|
||||||
)
|
)
|
||||||
set(HEADERS
|
set(HEADERS
|
||||||
|
include/database/base_repository.h
|
||||||
include/models.h
|
include/models.h
|
||||||
include/directory_manager.h
|
include/directory_manager.h
|
||||||
include/imageFile.h
|
include/imageFile.h
|
||||||
@@ -52,18 +56,32 @@ set(TAGLIB_HEADERS
|
|||||||
"${CMAKE_SOURCE_DIR}/3rdparty/taglib/taglib/xm"
|
"${CMAKE_SOURCE_DIR}/3rdparty/taglib/taglib/xm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set (ORM_DIR
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/ormpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set (MYSQL
|
||||||
|
/usr/local/mysql/connector-c++-8.0/lib64
|
||||||
|
)
|
||||||
|
set (MYSQL_INCLUDE
|
||||||
|
/usr/local/mysql/connector-c++-8.0/include/mysqlx
|
||||||
|
/usr/local/mysql/connector-c++-8.0/include/mysqlx/common
|
||||||
|
)
|
||||||
|
|
||||||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
||||||
conan_basic_setup()
|
conan_basic_setup()
|
||||||
|
|
||||||
|
link_directories(${MYSQL})
|
||||||
include_directories(include ${CPR_INCLUDE_DIRS} ${TAGLIB} ${TAGLIB_HEADERS})
|
include_directories(include ${CPR_INCLUDE_DIRS} ${TAGLIB} ${TAGLIB_HEADERS})
|
||||||
|
|
||||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cpr)
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cpr)
|
||||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib)
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/taglib)
|
||||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/oatpp)
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/oatpp)
|
||||||
|
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/mysql-connector-cpp)
|
||||||
|
|
||||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/appsettings.json ${CMAKE_BINARY_DIR}/bin/appsettings.json COPYONLY)
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/appsettings.json ${CMAKE_BINARY_DIR}/bin/appsettings.json COPYONLY)
|
||||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/authcredentials.json ${CMAKE_BINARY_DIR}/bin/authcredentials.json COPYONLY)
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/authcredentials.json ${CMAKE_BINARY_DIR}/bin/authcredentials.json COPYONLY)
|
||||||
|
|
||||||
add_executable(icarus ${SOURCES} ${HEADERS})
|
add_executable(icarus ${SOURCES} ${HEADERS})
|
||||||
target_include_directories(icarus PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jwt-cpp/include)
|
target_include_directories(icarus PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jwt-cpp/include)
|
||||||
target_link_libraries(icarus "-lstdc++fs" tag oatpp ${CONAN_LIBS} ${CPR_LIBRARIES})
|
target_link_libraries(icarus "-lstdc++fs" tag oatpp mysqlclient ${CONAN_LIBS} ${CPR_LIBRARIES})
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef BASE_REPOSITORY_H_
|
||||||
|
#define BASE_REPOSITORY_H_
|
||||||
|
|
||||||
|
#include<string>
|
||||||
|
|
||||||
|
#include <mysql/mysql.h>
|
||||||
|
|
||||||
|
#include "models.h"
|
||||||
|
|
||||||
|
class base_repository
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MYSQL* setup_mysql_connection(database_connection);
|
||||||
|
|
||||||
|
MYSQL_RES* perform_mysql_query(MYSQL*, std::string&);
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -48,6 +48,16 @@ struct auth_credentials
|
|||||||
std::string api_identifier;
|
std::string api_identifier;
|
||||||
std::string client_id;
|
std::string client_id;
|
||||||
std::string client_secret;
|
std::string client_secret;
|
||||||
|
std::string uri;
|
||||||
|
std::string endpoint;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct database_connection
|
||||||
|
{
|
||||||
|
std::string server;
|
||||||
|
std::string username;
|
||||||
|
std::string password;
|
||||||
|
std::string database;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TokenReq
|
struct TokenReq
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef TOKEN_MANAGER_H_
|
#ifndef TOKEN_MANAGER_H_
|
||||||
#define TOKEN_MANAGER_H_
|
#define TOKEN_MANAGER_H_
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include "models.h"
|
#include "models.h"
|
||||||
|
|
||||||
class token_manager
|
class token_manager
|
||||||
@@ -9,10 +11,9 @@ public:
|
|||||||
token_manager();
|
token_manager();
|
||||||
|
|
||||||
loginResult retrieve_token();
|
loginResult retrieve_token();
|
||||||
|
loginResult retrieve_token(std::string_view);
|
||||||
//LoginRes* retrieve_token(TokenReq *tok);
|
|
||||||
private:
|
private:
|
||||||
auth_credentials parse_auth_credentials();
|
auth_credentials parse_auth_credentials(std::string_view);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -18,32 +18,32 @@ class loginController : public oatpp::web::server::api::ApiController
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
loginController(std::string p, OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
loginController(std::string p, OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
||||||
: oatpp::web::server::api::ApiController(objectMapper), exe_path(fs::canonical(p.c_str()).parent_path().string())
|
: oatpp::web::server::api::ApiController(objectMapper), exe_path(std::move(p))
|
||||||
{
|
{ }
|
||||||
std::cout << "p is " << p << std::endl;
|
|
||||||
std::cout << "working path " << exe_path.string() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#include OATPP_CODEGEN_BEGIN(ApiController)
|
#include OATPP_CODEGEN_BEGIN(ApiController)
|
||||||
|
|
||||||
ENDPOINT("POST", "/api/v1/login", root)
|
ENDPOINT("POST", "/api/v1/login", data, BODY_DTO(userDto::ObjectWrapper, usr))
|
||||||
{
|
{
|
||||||
OATPP_LOGI("icarus", "logging in");
|
OATPP_LOGI("icarus", "logging in");
|
||||||
|
|
||||||
|
std::cout << "user: " << usr->username->c_str() << std::endl;
|
||||||
|
|
||||||
token_manager tok;
|
token_manager tok;
|
||||||
auto token = tok.retrieve_token();
|
auto token = tok.retrieve_token(exe_path);
|
||||||
|
|
||||||
auto logRes = loginResultDto::createShared();
|
auto logRes = loginResultDto::createShared();
|
||||||
logRes->access_token = token.access_token.c_str();
|
logRes->access_token = token.access_token.c_str();
|
||||||
logRes->token_type = token.token_type.c_str();
|
logRes->token_type = token.token_type.c_str();
|
||||||
|
logRes->expiration = token.expiration;
|
||||||
|
|
||||||
return createDtoResponse(Status::CODE_200, logRes);
|
return createDtoResponse(Status::CODE_200, logRes);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include OATPP_CODEGEN_END(ApiController)
|
#include OATPP_CODEGEN_END(ApiController)
|
||||||
private:
|
private:
|
||||||
fs::path exe_path;
|
std::string exe_path;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include "database/base_repository.h"
|
||||||
|
|
||||||
|
MYSQL* base_repository::setup_mysql_connection(database_connection details)
|
||||||
|
{
|
||||||
|
MYSQL *connection = mysql_init(NULL);
|
||||||
|
|
||||||
|
// connect to the database with the details attached.
|
||||||
|
if (!mysql_real_connect(connection,details.server.c_str(), details.username.c_str(), details.password.c_str(), details.database.c_str(), 0, NULL, 0)) {
|
||||||
|
printf("Conection error : %s\n", mysql_error(connection));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
MYSQL_RES* base_repository::perform_mysql_query(MYSQL *conn, std::string& query)
|
||||||
|
{
|
||||||
|
// send the query to the database
|
||||||
|
if (mysql_query(conn, query.c_str()))
|
||||||
|
{
|
||||||
|
printf("MySQL query error : %s\n", mysql_error(conn));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mysql_use_result(conn);
|
||||||
|
}
|
||||||
@@ -12,6 +12,15 @@ class loginResultDto : public oatpp::data::mapping::type::Object
|
|||||||
|
|
||||||
DTO_FIELD(String, access_token);
|
DTO_FIELD(String, access_token);
|
||||||
DTO_FIELD(String, token_type);
|
DTO_FIELD(String, token_type);
|
||||||
|
DTO_FIELD(Int32, expiration);
|
||||||
|
};
|
||||||
|
|
||||||
|
class userDto : public oatpp::data::mapping::type::Object
|
||||||
|
{
|
||||||
|
DTO_INIT(userDto, Object)
|
||||||
|
|
||||||
|
DTO_FIELD(String, username);
|
||||||
|
DTO_FIELD(String, password);
|
||||||
};
|
};
|
||||||
|
|
||||||
#include OATPP_CODEGEN_END(DTO)
|
#include OATPP_CODEGEN_END(DTO)
|
||||||
|
|||||||
+69
-2
@@ -1,15 +1,18 @@
|
|||||||
|
#include <exception>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "models.h"
|
||||||
|
#include <mysql/mysql.h>
|
||||||
#include "oatpp/network/server/Server.hpp"
|
#include "oatpp/network/server/Server.hpp"
|
||||||
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
|
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
|
||||||
#include "oatpp/web/server/HttpConnectionHandler.hpp"
|
#include "oatpp/web/server/HttpConnectionHandler.hpp"
|
||||||
|
|
||||||
#include "appComponent.hpp"
|
#include "appComponent.hpp"
|
||||||
#include "controller/loginController.hpp"
|
#include "controller/loginController.hpp"
|
||||||
//#include "loginHandler.hpp"
|
#include "database/base_repository.h"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
@@ -35,13 +38,77 @@ void run(const std::string& working_path)
|
|||||||
server.run();
|
server.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MYSQL* mysql_connection_setup(database_connection mysql_details)
|
||||||
|
{
|
||||||
|
// first of all create a mysql instance and initialize the variables within
|
||||||
|
MYSQL *connection = mysql_init(NULL);
|
||||||
|
|
||||||
|
// connect to the database with the details attached.
|
||||||
|
if (!mysql_real_connect(connection,mysql_details.server.c_str(), mysql_details.username.c_str(), mysql_details.password.c_str(), mysql_details.database.c_str(), 0, NULL, 0)) {
|
||||||
|
printf("Conection error : %s\n", mysql_error(connection));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
|
||||||
|
{
|
||||||
|
// send the query to the database
|
||||||
|
if (mysql_query(connection, sql_query))
|
||||||
|
{
|
||||||
|
printf("MySQL query error : %s\n", mysql_error(connection));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mysql_use_result(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_database()
|
||||||
|
{
|
||||||
|
database_connection mysqlD;
|
||||||
|
mysqlD.server = ""; // where the mysql database is
|
||||||
|
mysqlD.username = ""; // the root user of mysql
|
||||||
|
mysqlD.password = ""; // the password of the root user in mysql
|
||||||
|
mysqlD.database = ""; // the databse to pick
|
||||||
|
|
||||||
|
base_repository base;
|
||||||
|
auto conn = base.setup_mysql_connection(mysqlD);
|
||||||
|
|
||||||
|
// assign the results return to the MYSQL_RES pointer
|
||||||
|
const std::string query = "SELECT *, NULL FROM Song";
|
||||||
|
auto res = base.perform_mysql_query(conn, query);
|
||||||
|
auto num_of_fields = mysql_num_fields(res);
|
||||||
|
|
||||||
|
printf("MySQL Tables in mysql database:\n");
|
||||||
|
auto row_count = 1;
|
||||||
|
|
||||||
|
for (MYSQL_ROW row = NULL; (row = mysql_fetch_row(res)) != NULL;) {
|
||||||
|
std::cout << " row " << row_count << std::endl;
|
||||||
|
for (auto i = 0; i != num_of_fields; ++i) {
|
||||||
|
auto val = row[i];
|
||||||
|
|
||||||
|
if ( val == NULL) {
|
||||||
|
std::cout << "found null value " << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std::cout << val << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* clean up the database result set */
|
||||||
|
mysql_free_result(res);
|
||||||
|
/* clean up the database link */
|
||||||
|
mysql_close(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
oatpp::base::Environment::init();
|
oatpp::base::Environment::init();
|
||||||
std::string working_path = argv[0];
|
std::string working_path = argv[0];
|
||||||
//std::cout << fs::canonical(fs::path(working_path.c_str())).parent_path().string() << std::endl;
|
|
||||||
|
|
||||||
|
test_database();
|
||||||
run(working_path);
|
run(working_path);
|
||||||
|
|
||||||
oatpp::base::Environment::destroy();
|
oatpp::base::Environment::destroy();
|
||||||
|
|||||||
+41
-34
@@ -1,20 +1,18 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
#include <cpr/cpr.h>
|
#include <cpr/cpr.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
//#include "models.h"
|
|
||||||
#include "token_manager.h"
|
#include "token_manager.h"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
//extern "C"
|
|
||||||
//{
|
|
||||||
|
|
||||||
|
|
||||||
token_manager::token_manager()
|
token_manager::token_manager()
|
||||||
{
|
{
|
||||||
@@ -22,51 +20,60 @@ token_manager::token_manager()
|
|||||||
|
|
||||||
loginResult token_manager::retrieve_token()
|
loginResult token_manager::retrieve_token()
|
||||||
{
|
{
|
||||||
auto cred = parse_auth_credentials();
|
|
||||||
|
|
||||||
loginResult lr;
|
loginResult lr;
|
||||||
lr.access_token = "dsfdsf";
|
lr.access_token = "dsfdsf";
|
||||||
lr.token_type = "demo";
|
lr.token_type = "demo";
|
||||||
|
|
||||||
return lr;
|
return lr;
|
||||||
}
|
}
|
||||||
|
loginResult token_manager::retrieve_token(std::string_view path)
|
||||||
auth_credentials token_manager::parse_auth_credentials()
|
|
||||||
{
|
{
|
||||||
auth_credentials auth;
|
auto cred = parse_auth_credentials(path);
|
||||||
//auto ss = working_path.string();
|
|
||||||
//auto path = fs::canonical(fs::path("."));
|
|
||||||
//std::cout << "canonical path " << path.string() << std::endl;
|
|
||||||
|
|
||||||
return auth;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
LoginRes* token_manager::retrieve_token(TokenReq *tok)
|
|
||||||
{
|
|
||||||
LoginRes *res = new LoginRes;
|
|
||||||
|
|
||||||
nlohmann::json reqObj;
|
nlohmann::json reqObj;
|
||||||
reqObj["client_id"] = tok->ClientId;
|
reqObj["client_id"] = cred.client_id;
|
||||||
reqObj["client_secret"] = tok->ClientSecret;
|
reqObj["client_secret"] = cred.client_secret;
|
||||||
reqObj["audience"] = tok->Audience;
|
reqObj["audience"] = cred.api_identifier;
|
||||||
reqObj["grant_type"] = tok->GrantType;
|
reqObj["grant_type"] = "client_credentials";
|
||||||
|
|
||||||
std::string uri{tok->URI};
|
std::string uri{cred.uri};
|
||||||
uri.append("/");
|
uri.append("/");
|
||||||
uri.append(tok->Endpoint);
|
uri.append(cred.endpoint);
|
||||||
|
|
||||||
auto r = cpr::Post(cpr::Url{uri},
|
auto r = cpr::Post(cpr::Url{uri},
|
||||||
cpr::Body{reqObj.dump()},
|
cpr::Body{reqObj.dump()},
|
||||||
cpr::Header{{"Content-Type", "application/json"}});
|
cpr::Header{{"Content-Type", "application/json"},
|
||||||
|
{"Connection", "keep-alive"}});
|
||||||
|
|
||||||
auto post_res = nlohmann::json::parse(r.text);
|
auto post_res = nlohmann::json::parse(r.text);
|
||||||
strcpy(res->Token, post_res["access_token"].get<std::string>().c_str());
|
|
||||||
strcpy(res->TokenType, post_res["token_type"].get<std::string>().c_str());
|
|
||||||
res->Expiration = post_res["expires_in"].get<int>();
|
|
||||||
strcpy(res->Message, "Success");
|
|
||||||
|
|
||||||
return res;
|
loginResult lr;
|
||||||
|
lr.access_token = post_res["access_token"].get<std::string>();
|
||||||
|
lr.token_type = post_res["token_type"].get<std::string>();
|
||||||
|
lr.expiration = post_res["expires_in"].get<int>();
|
||||||
|
|
||||||
|
return lr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auth_credentials token_manager::parse_auth_credentials(std::string_view path)
|
||||||
|
{
|
||||||
|
auto exe_path = fs::canonical(path).parent_path().string();
|
||||||
|
exe_path.append("/authcredentials.json");
|
||||||
|
|
||||||
|
std::fstream a(exe_path, std::ios::in);
|
||||||
|
std::stringstream s;
|
||||||
|
s << a.rdbuf();
|
||||||
|
a.close();
|
||||||
|
|
||||||
|
auto con = nlohmann::json::parse(s.str());
|
||||||
|
|
||||||
|
auth_credentials auth;
|
||||||
|
auth.uri = "https://";
|
||||||
|
auth.uri.append(con["Domain"]);
|
||||||
|
auth.api_identifier = con["ApiIdentifier"];
|
||||||
|
auth.client_id = con["ClientId"];
|
||||||
|
auth.client_secret = con["ClientSecret"];
|
||||||
|
auth.endpoint = "oauth/token";
|
||||||
|
|
||||||
|
return auth;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
//}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user