database stuff

This commit is contained in:
kdeng00
2019-08-18 11:57:08 -04:00
parent 5230e69832
commit d26c16aa69
11 changed files with 211 additions and 48 deletions
+8 -8
View File
@@ -18,32 +18,32 @@ class loginController : public oatpp::web::server::api::ApiController
{
public:
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())
{
std::cout << "p is " << p << std::endl;
std::cout << "working path " << exe_path.string() << std::endl;
}
: oatpp::web::server::api::ApiController(objectMapper), exe_path(std::move(p))
{ }
#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");
std::cout << "user: " << usr->username->c_str() << std::endl;
token_manager tok;
auto token = tok.retrieve_token();
auto token = tok.retrieve_token(exe_path);
auto logRes = loginResultDto::createShared();
logRes->access_token = token.access_token.c_str();
logRes->token_type = token.token_type.c_str();
logRes->expiration = token.expiration;
return createDtoResponse(Status::CODE_200, logRes);
}
#include OATPP_CODEGEN_END(ApiController)
private:
fs::path exe_path;
std::string exe_path;
};
#endif
+25
View File
@@ -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);
}
+9
View File
@@ -12,6 +12,15 @@ class loginResultDto : public oatpp::data::mapping::type::Object
DTO_FIELD(String, access_token);
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)
+69 -2
View File
@@ -1,15 +1,18 @@
#include <exception>
#include <iostream>
#include <filesystem>
#include <memory>
#include <string>
#include "models.h"
#include <mysql/mysql.h>
#include "oatpp/network/server/Server.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "appComponent.hpp"
#include "controller/loginController.hpp"
//#include "loginHandler.hpp"
#include "database/base_repository.h"
namespace fs = std::filesystem;
@@ -35,13 +38,77 @@ void run(const std::string& working_path)
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)
{
oatpp::base::Environment::init();
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);
oatpp::base::Environment::destroy();
+41 -34
View File
@@ -1,20 +1,18 @@
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
#include <string_view>
#include <sstream>
#include <cstdlib>
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
//#include "models.h"
#include "token_manager.h"
namespace fs = std::filesystem;
//extern "C"
//{
token_manager::token_manager()
{
@@ -22,51 +20,60 @@ token_manager::token_manager()
loginResult token_manager::retrieve_token()
{
auto cred = parse_auth_credentials();
loginResult lr;
lr.access_token = "dsfdsf";
lr.token_type = "demo";
return lr;
}
auth_credentials token_manager::parse_auth_credentials()
loginResult token_manager::retrieve_token(std::string_view path)
{
auth_credentials auth;
//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;
auto cred = parse_auth_credentials(path);
nlohmann::json reqObj;
reqObj["client_id"] = tok->ClientId;
reqObj["client_secret"] = tok->ClientSecret;
reqObj["audience"] = tok->Audience;
reqObj["grant_type"] = tok->GrantType;
reqObj["client_id"] = cred.client_id;
reqObj["client_secret"] = cred.client_secret;
reqObj["audience"] = cred.api_identifier;
reqObj["grant_type"] = "client_credentials";
std::string uri{tok->URI};
std::string uri{cred.uri};
uri.append("/");
uri.append(tok->Endpoint);
uri.append(cred.endpoint);
auto r = cpr::Post(cpr::Url{uri},
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);
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;
}
*/
//}