eedb/src/app/main.cpp
2017-02-26 09:32:45 +01:00

218 lines
7.8 KiB
C++

#include <eedb/auth/PgUserAuth.hpp>
#include <eedb/auth/Services.hpp>
#include <eedb/db/connection.hpp>
#include <memory>
#include <Wt/WApplication>
#include <Wt/WBootstrapTheme>
#include <Wt/WBorderLayout>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WHBoxLayout>
#include <Wt/WLineEdit>
#include <Wt/WMenu>
#include <Wt/WMessageBox>
#include <Wt/WNavigationBar>
#include <Wt/WPopupMenu>
#include <Wt/WPopupWidget>
#include <Wt/WPushButton>
#include <Wt/WServer>
#include <Wt/WStackedWidget>
#include <Wt/WTabWidget>
#include <Wt/WText>
#include <Wt/WTextArea>
#include <Wt/WTextEdit>
#include <Wt/WVBoxLayout>
#include <Wt/WWidget>
#include <Wt/Auth/PasswordService>
#include <Wt/Auth/PasswordStrengthValidator>
#include <Wt/Auth/PasswordVerifier>
#include <Wt/Auth/AuthWidget>
#include <Wt/Auth/Dbo/AuthInfo>
#include <Wt/Auth/Dbo/UserDatabase>
#include <Wt/Auth/FacebookService>
#include <Wt/Auth/GoogleService>
#include <Wt/Auth/HashFunction>
#include <Wt/Auth/Login>
class AuthApplication : public Wt::WApplication {
public:
AuthApplication(const Wt::WEnvironment & env) : Wt::WApplication(env) {
login_.changed().connect(this, &AuthApplication::authEvent);
auto config = std::make_shared< sqlpp::postgresql::connection_config >();
config->host = "192.168.1.101";
config->port = 5432;
config->user = "postgres";
config->password = "postgres";
config->dbname = "eedb";
config->debug = true;
conn_ = std::make_unique< eedb::db::PgConnection >(config);
userDatabase_ = std::make_unique< eedb::auth::PgUserAuth >(*conn_, env);
userDatabase_->setAuthService(eedb::auth::Services::authService());
root()->addStyleClass("container");
auto theme = new Wt::WBootstrapTheme(this);
theme->setVersion(Wt::WBootstrapTheme::Version3);
theme->setResponsive(true);
setTheme(theme);
useStyleSheet("/resources/style.css");
_loginWidget = new Wt::Auth::AuthWidget(*eedb::auth::Services::authService(), *userDatabase_, login_);
_loginWidget->model()->addPasswordAuth(eedb::auth::Services::passwordService());
_loginWidget->model()->addOAuth(eedb::auth::Services::oAuthServices());
_loginWidget->setRegistrationEnabled(true);
_loginWidget->processEnvironment();
root()->addWidget(_loginWidget);
}
void dddd() {}
void authEvent() {
using namespace Wt;
if(login_.loggedIn()) {
Wt::WContainerWidget * container = root();
setInternalPath("/app");
root()->removeStyleClass("container");
// Create a navigation bar with a link to a web page.
Wt::WNavigationBar * navigation = new Wt::WNavigationBar(container);
navigation->setTitle("Corpy Inc.", "http://www.google.com/search?q=corpy+inc");
navigation->setResponsive(true);
Wt::WStackedWidget * contentsStack = new Wt::WStackedWidget(container);
contentsStack->addStyleClass("contents");
// Setup a Left-aligned menu.
Wt::WMenu * leftMenu = new Wt::WMenu(contentsStack, container);
navigation->addMenu(leftMenu);
Wt::WText * searchResult = new Wt::WText("Buy or Sell... Bye!");
leftMenu->addItem("Home", new Wt::WText("There is no better place!"));
leftMenu->addItem("Sales", searchResult);
// Setup a Right-aligned menu.
Wt::WMenu * rightMenu = new Wt::WMenu();
Wt::WMenu * sessionMenu = new Wt::WMenu();
navigation->addMenu(rightMenu, Wt::AlignRight);
navigation->addMenu(sessionMenu, Wt::AlignRight);
// Create a popup submenu for the Help menu.
Wt::WPopupMenu * popup = new Wt::WPopupMenu();
popup->addItem("Contents");
popup->addItem("Index");
popup->addSeparator();
popup->addItem("About");
popup->itemSelected().connect(std::bind(
[=](Wt::WMenuItem * item) {
Wt::WMessageBox * messageBox = new Wt::WMessageBox(
"Help", Wt::WString::fromUTF8("<p>Showing Help: {1}</p>").arg(item->text()), Wt::Information, Wt::Ok);
messageBox->buttonClicked().connect(std::bind([=]() { delete messageBox; }));
messageBox->show();
},
std::placeholders::_1));
Wt::WMenuItem * help = new Wt::WMenuItem("Help");
help->setMenu(popup);
rightMenu->addItem(help);
sessionMenu->addItem("session", _loginWidget);
// Add a Search control.
Wt::WLineEdit * edit = new Wt::WLineEdit();
edit->setEmptyText("Enter a search item");
edit->enterPressed().connect(std::bind([=]() {
leftMenu->select(2); // is the index of the "Sales"
searchResult->setText(Wt::WString("Nothing found for {1}.").arg(edit->text()));
}));
navigation->addSearch(edit, Wt::AlignRight);
Wt::WBorderLayout * layout = new Wt::WBorderLayout();
container->setLayout(layout);
const char * cell = "{1} item";
Wt::WStackedWidget * contents = new Wt::WStackedWidget();
auto menu = new Wt::WMenu(contents, container);
menu->setStyleClass("nav nav-pills nav-stacked");
// Add menu items using the default lazy loading policy.
menu->addItem("Internal paths", new Wt::WText("Internal paths contents"));
menu->addItem("Anchor", new Wt::WTextArea("Anchor contents"));
menu->addItem("Tab widget", new Wt::WTextArea("Tab widget contents"));
menu->addItem("Menu", new Wt::WTextArea("Menu contents"));
layout->addWidget(navigation, Wt::WBorderLayout::North);
Wt::WText * item = new Wt::WText(Wt::WString(cell).arg("North"));
item->setStyleClass("green-box");
layout->addWidget(menu, Wt::WBorderLayout::West);
item = new Wt::WText(Wt::WString(cell).arg("East"));
item->setStyleClass("green-box");
layout->addWidget(item, Wt::WBorderLayout::East);
item = new Wt::WText(Wt::WString(cell).arg("South"));
item->setStyleClass("green-box");
layout->addWidget(item, Wt::WBorderLayout::South);
item = new Wt::WText(Wt::WString(cell).arg("Center"));
item->setStyleClass("green-box");
layout->addWidget(contents, Wt::WBorderLayout::Center);
} else {
Wt::Auth::AuthWidget * authWidget = new Wt::Auth::AuthWidget(*eedb::auth::Services::authService(), *userDatabase_, login_);
root()->clear();
authWidget->model()->addPasswordAuth(eedb::auth::Services::passwordService());
authWidget->model()->addOAuth(eedb::auth::Services::oAuthServices());
authWidget->setRegistrationEnabled(true);
root()->addWidget(authWidget);
Wt::log("notice") << "User logged out.";
}
}
private:
std::unique_ptr< eedb::db::PgConnection > conn_;
std::unique_ptr< eedb::auth::PgUserAuth > userDatabase_;
Wt::Auth::Login login_;
Wt::Auth::AuthWidget * _loginWidget;
Wt::WWidget * homePage_;
Wt::WContainerWidget * sideBarContent_;
};
Wt::WApplication * createApplication(const Wt::WEnvironment & env) {
return new AuthApplication(env);
}
int main(int argc, char ** argv) {
try {
Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
server.addEntryPoint(Wt::Application, createApplication);
eedb::auth::Services::configureAuth();
server.run();
} catch(Wt::WServer::Exception & e) {
std::cerr << e.what() << std::endl;
} catch(std::exception & e) {
std::cerr << "exception: " << e.what() << std::endl;
}
}