Skip to content

Commit

Permalink
Merge 0425a18 into 1356aa5
Browse files Browse the repository at this point in the history
  • Loading branch information
zussel committed Jun 25, 2021
2 parents 1356aa5 + 0425a18 commit 9b89442
Show file tree
Hide file tree
Showing 292 changed files with 15,785 additions and 1,252 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.11)
STRING(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPER)

SET(APP_MAJOR_VERSION 0)
SET(APP_MINOR_VERSION 7)
SET(APP_MINOR_VERSION 8)
SET(APP_PATCH_LEVEL 0)
SET(APP_VERSION "${APP_MAJOR_VERSION}.${APP_MINOR_VERSION}.${APP_PATCH_LEVEL}")

Expand Down Expand Up @@ -66,6 +66,7 @@ IF (NOT COVERAGE_TESTS)
ENDIF()

OPTION(ARCH "Compiler architecture for Clang/GCC" "")
OPTION(EXAMPLES "Build examples" false)

FIND_PACKAGE( Threads REQUIRED )

Expand Down Expand Up @@ -174,8 +175,10 @@ ENABLE_TESTING()
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(doc)
ADD_SUBDIRECTORY(test)
ADD_SUBDIRECTORY(examples)
ADD_SUBDIRECTORY(sandbox)
if (EXAMPLES)
ADD_SUBDIRECTORY(examples)
ADD_SUBDIRECTORY(sandbox)
ENDIF()

INSTALL(
DIRECTORY ${PROJECT_BINARY_DIR}/doc/web/
Expand Down
14 changes: 13 additions & 1 deletion cmake/FindMySQL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ if(WIN32)
$ENV{ProgramFiles}/MySQL/*/include
$ENV{SystemDrive}/MySQL/*/include
$ENV{ProgramW6432}/MySQL/*/include
$ENV{ProgramFiles}/MariaDB/*/include
$ENV{SystemDrive}/MariaDB/*/include
$ENV{ProgramW6432}/MariaDB/*/include
)
else(WIN32)
find_path(MYSQL_INCLUDE_DIR mysql.h
Expand Down Expand Up @@ -48,18 +51,23 @@ if(WIN32)
SET(build_dist Release)
endif(CMAKE_BUILD_TYPE_TOLOWER MATCHES "debug")

FIND_LIBRARY(MYSQL_LIBRARY NAMES libmysql
FIND_LIBRARY(MYSQL_LIBRARY NAMES libmysql libmariadb
PATHS
$ENV{MYSQL_DIR}/lib/${binary_dist}
$ENV{MYSQL_DIR}/libmysql/${build_dist}
$ENV{MYSQL_DIR}/client/${build_dist}
$ENV{ProgramFiles}/MySQL/*/lib/${binary_dist}
$ENV{SystemDrive}/MySQL/*/lib/${binary_dist}
$ENV{ProgramFiles}/MariaDB/*/lib/${binary_dist}
$ENV{SystemDrive}/MariaDB/*/lib/${binary_dist}
$ENV{MYSQL_DIR}/lib/opt
$ENV{MYSQL_DIR}/client/release
$ENV{ProgramFiles}/MySQL/*/lib/opt
$ENV{SystemDrive}/MySQL/*/lib/opt
$ENV{ProgramW6432}/MySQL/*/lib
$ENV{ProgramFiles}/MariaDB/*/lib/opt
$ENV{SystemDrive}/MariaDB/*/lib/opt
$ENV{ProgramW6432}/MariaDB/*/lib
)
else(WIN32)
find_library(MYSQL_LIBRARY NAMES libmysql
Expand Down Expand Up @@ -91,6 +99,10 @@ else(WIN32)
)
endif(WIN32)

IF (MYSQL_INCLUDE_DIR)
MESSAGE(STATUS "MariaDB include ${MYSQL_INCLUDE_DIR}")
ENDIF (MYSQL_INCLUDE_DIR)

IF (MYSQL_INCLUDE_DIR AND MYSQL_LIBRARY)
SET(MYSQL_FOUND TRUE)
SET( MYSQL_LIBRARIES ${MYSQL_LIBRARY} )
Expand Down
4 changes: 3 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ADD_SUBDIRECTORY(net)
ADD_SUBDIRECTORY(net)
ADD_SUBDIRECTORY(http)
ADD_SUBDIRECTORY(demo)
43 changes: 43 additions & 0 deletions examples/demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SET(NET_LIBRARIES)

IF(WIN32)
MESSAGE(STATUS "Appending Windows Socket libs: wsock32 ws2_32")
LIST(APPEND NET_LIBRARIES wsock32 ws2_32)
ENDIF()

SET (DEMO_SOURCES
main.cpp
services/auth_service.cpp
services/auth_service.hpp
models/credential.hpp
models/user.hpp
pages/movie_page.cpp
pages/movie_page.hpp
models/movie.hpp
models/person.hpp
services/movie_service.cpp
services/movie_service.hpp pages/main_page.cpp pages/main_page.hpp)

ADD_EXECUTABLE(demo ${DEMO_SOURCES})

add_custom_command(TARGET demo PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/examples/demo/web/ $<TARGET_FILE_DIR:demo>/web)

add_custom_command(TARGET demo PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/examples/demo/templates/ $<TARGET_FILE_DIR:demo>/templates)

TARGET_LINK_LIBRARIES(demo
matador-utils
matador-logger
matador-sql
matador-object
matador-orm
matador-net
matador-http
${CMAKE_DL_LIBS}
${CMAKE_THREAD_LIBS_INIT}
${NET_LIBRARIES}
${SQLITE3_LIBRARY}
)
62 changes: 62 additions & 0 deletions examples/demo/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "services/movie_service.hpp"
#include "pages/movie_page.hpp"
#include "pages/main_page.hpp"

#include "models/person.hpp"
#include "models/movie.hpp"

#include "matador/logger/log_manager.hpp"

#include "matador/orm/persistence.hpp"
#include "matador/orm/session.hpp"

#include "matador/http/http_server.hpp"
#include "matador/http/request.hpp"
#include "matador/http/static_file_service.hpp"

using namespace matador;

int main(int /*argc*/, char* /*argv*/[])
{
// setup application logging
matador::add_log_sink(matador::create_file_sink("log/server.log"));
matador::add_log_sink(matador::create_stdout_sink());

// setup database
matador::persistence p("sqlite://moviedb.sqlite");
p.enable_log();


p.attach<person>("person");
p.attach<movie>("movie");

p.create();

// load entities
session s(p);
s.load();

// initialize network stack
net::init();

// create server
http::server server(19082, "web");
server.add_routing_middleware();

// add routes
http::serve_static_files_at("/css/*.*", server);
http::serve_static_files_at("/js/*.*", server);
http::serve_static_files_at("/fonts/*.*", server);

main_page mainpage(server, p);
movie_page moviepage(server, p);
movie_service mservice(server, p);

// start server
server.run();

// cleanup network stack
net::cleanup();

return 0;
}
17 changes: 17 additions & 0 deletions examples/demo/models/credential.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef MATADOR_CREDENTIAL_HPP
#define MATADOR_CREDENTIAL_HPP

struct credential
{
std::string username {};
std::string password {};

template < class S >
void serialize(S &serializer)
{
serializer.serialize("username", username);
serializer.serialize("password", password);
}
};

#endif //MATADOR_CREDENTIAL_HPP
48 changes: 48 additions & 0 deletions examples/demo/models/movie.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef MATADOR_MOVIE_HPP
#define MATADOR_MOVIE_HPP

#include "matador/utils/identifier.hpp"
#include "matador/utils/cascade_type.hpp"

#include "matador/object/has_many.hpp"

#include <string>
#include <utility>

#include "person.hpp"

struct movie
{
enum e_genre {
ACTION,
ROMANCE,
COMEDY,
SCIENCE_FICTION,
HORROR,
THRILLER
};

movie() = default;
movie(std::string t, unsigned short y, const matador::object_ptr<person>& dir)
: title(std::move(t)), year(y), director(dir)
{}

matador::identifier<unsigned long> id;
std::string title;
matador::has_many<e_genre> genres;
unsigned short year {};
matador::has_many<person> actors;
matador::has_one<person> director;

template < class Serializer >
void serialize(Serializer &serializer)
{
serializer.serialize("id", id);
serializer.serialize("title", title);
//serializer.serialize("genres", genres, "movie_id", "genre", matador::cascade_type::ALL);
serializer.serialize("year", year);
serializer.serialize("actors", actors, "movie_id", "actor_id", matador::cascade_type::NONE);
serializer.serialize("director", director, matador::cascade_type::NONE);
}
};
#endif //MATADOR_MOVIE_HPP
30 changes: 30 additions & 0 deletions examples/demo/models/person.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef MATADOR_PERSON_HPP
#define MATADOR_PERSON_HPP

#include <utility>

#include "matador/utils/identifier.hpp"
#include "matador/utils/date.hpp"

struct person
{
matador::identifier<unsigned long> id;
std::string name;
matador::date birthday;

person() = default;
person(std::string n, matador::date bd)
: name(std::move(n)), birthday(std::move(bd))
{}

template < class Serializer >
void serialize(Serializer &serializer)
{
serializer.serialize("id", id);
serializer.serialize("name", name);
serializer.serialize("birthday", birthday);
}
};


#endif //MATADOR_PERSON_HPP
25 changes: 25 additions & 0 deletions examples/demo/models/user.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef MATADOR_USER_HPP
#define MATADOR_USER_HPP


struct user
{
long id {};
std::string username {};
std::string password {};
std::string first_name {};
std::string last_name {};

template < class S >
void serialize(S &serializer)
{
serializer.serialize("id", id);
serializer.serialize("username", username);
serializer.serialize("password", password);
serializer.serialize("first_name", first_name);
serializer.serialize("last_name", last_name);
}
};


#endif //MATADOR_USER_HPP
32 changes: 32 additions & 0 deletions examples/demo/pages/main_page.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "main_page.hpp"

#include "matador/http/http_server.hpp"
#include "matador/http/template_engine.hpp"

#include "matador/logger/log_manager.hpp"

using namespace matador;
using namespace matador::http;

main_page::main_page(matador::http::server &s, matador::persistence &p)
: log_(matador::create_logger("MainPage"))
, persistence_(p)
{
file f("../templates/main.matador", "r");
auto tmpl = read_as_text(f);
f.close();

index_template_ = template_engine::build(tmpl);

s.on_get("/", [this](const request &req) {
return view(req);
});
}

matador::http::response main_page::view(const matador::http::request &p)
{
json data;
data["title"] = "Movies";

return response::ok(template_engine::render(index_template_, data), mime_types::TYPE_TEXT_HTML);
}
35 changes: 35 additions & 0 deletions examples/demo/pages/main_page.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef MATADOR_MAIN_PAGE_HPP
#define MATADOR_MAIN_PAGE_HPP

#include "matador/http/response.hpp"

#include "matador/logger/logger.hpp"

namespace matador {
class persistence;
namespace http {
namespace detail {
class template_part;
}
class server;
class request;
}
}

class main_page
{
public:
main_page(matador::http::server &s, matador::persistence &p);

matador::http::response view(const matador::http::request &p);

private:
matador::logger log_;

matador::persistence& persistence_;

std::shared_ptr<matador::http::detail::template_part> index_template_;
};


#endif //MATADOR_MAIN_PAGE_HPP
Loading

0 comments on commit 9b89442

Please sign in to comment.