CPPWebFramework
1.0.0
C++ Web 框架 (CWF) 是一个 MVC Web 框架,开源,根据 MIT 许可,使用 C++ 和 Qt 来开发 Web 应用程序。 CWF 旨在消耗很少的计算资源(例如内存和处理),并且请求响应时间较短。使用 MVC(模型-视图-控制器)架构,您可以创建类来处理业务层(模型),在网页中使用 CSTL(C++ 服务器页面标准标记库)来处理数据表示(视图)并使用控制器作为两层之间的控制器(Controller)。 CWF 的灵感来自于 Java Servlet、JSTL 和 Spring 框架。
因为它是在Qt中创建的,所以C++ Web Framework可以运行在Qt支持的相同平台上:
CWF只有一个配置文件,称为CPPWeb.ini,并且在其组件的开发中只使用C++和Qt的政策,以避免安装大量库和冲突,保持多平台特性,方便安装并保持学习曲线低,以使 Web 开发尽可能简单,即使对于初学者也是如此。
# include " cppwebapplication.h "
class HelloWorldController : public CWF ::Controller
{
public:
void doGet (CWF::Request &request, CWF::Response &response) const override
{
response. write ( " <html><body>Hello World!</body></html> " );
}
};
// Call
// http://localhost:8080/hello
int main ( int argc, char *argv[])
{
CWF::CppWebApplication server (argc, argv, " /PATH_TO_EXAMPLE/server/ " );
server. addController <HelloWorldController>( " /hello " );
return server. start ();
}
// hellomodel.h (Model)
# include < QObject >
class HelloModel : public QObject
{
Q_OBJECT
public slots:
QString greeting () const
{
return " Hello User! " ;
}
};
// helloview.view (View)
<html>
<head>
<title>Hello</title>
</head>
<body>
<out value= " #{model.greeting} " />
</body>
</html>
// hellocontroller.h (Controller)
# include < cwf/controller.h >
# include < model/hellomodel.h >
class HelloController : public CWF ::Controller
{
public:
void doGet (CWF::Request &request, CWF::Response &response) const override
{
HelloModel model;
request. addAttribute ( " model " , &model);
request. getRequestDispatcher ( " /pages/helloview.view " ). forward (request, response);
}
};
// main.cpp
# include < cwf/cppwebapplication.h >
# include < controller/hellocontroller.h >
// Call
// http://localhost:8080/hello
int main ( int argc, char *argv[])
{
CWF::CppWebApplication server (argc, argv, " /PATH_TO_EXAMPLE/server/ " );
server. addController <HelloController>( " /hello " );
return server. start ();
}
# include < cwf/sqlquery.h >
# include < cwf/cppwebapplication.h >
# include < cwf/sqldatabasestorage.h >
/*
* SQL Script
* create table countries (co_id serial primary key, co_name varchar unique);
* insert into countries (co_name) values ('BRAZIL'), ('UNITED STATES OF AMERICA'), ('CANADA');
*/
CWF::SqlDatabaseStorage storage ( " QPSQL " , " localhost " , " postgres " , " postgres " , " 1234 " , 5432 );
class CountriesController : public CWF ::Controller
{
public:
void doGet (CWF::Request &request, CWF::Response &response) const override
{
CWF::SqlQuery qry (storage);
qry. exec ( " select * from countries " );
response. write (qry. toJson ());
}
};
// Call
// http://localhost:8080/countries
int main ( int argc, char *argv[])
{
CWF::CppWebApplication server (argc, argv, " /PATH_TO_EXAMPLE/server/ " );
server. addController <CountriesController>( " /countries " );
return server. start ();
}
# include < usermodel.h >
# include < cwf/cppwebapplication.h >
# include < cwf/sqldatabasestorage.h >
/*
* ORM (Experimental) - Tested only on PostgreSQL
*/
CWF::SqlDatabaseStorage conexao ( " QPSQL " , " localhost " , " postgres " , " postgres " , " 1234 " , 5432 );
class ORMController : public CWF ::Controller
{
public:
void doGet (CWF::Request &request, CWF::Response &response) const override
{
UserModel user{conexao};
user. setName ( " Herik Lima " );
user. setPhone ( " +55 11 9 99999-0000 " );
user. setCountry ( " Brazil " );
user. setState ( " São Paulo " );
response. write ( QByteArray ( " <html><body> " ) + (user. save () ? " Saved " : " Error " ) + " </body></html> " );
}
};
// Call
// http://localhost:8080/orm
int main ( int argc, char *argv[])
{
CWF::CppWebApplication server (argc, argv, " /home/herik/CPPWebFramework/examples/ORM/server " );
UserModel{conexao}. updateDB (); // Create or update the table in database
server. addController <ORMController>( " /orm " );
return server. start ();
}
// usermodel.h
# ifndef USERMODEL_H
# define USERMODEL_H
# include < cwf/model.h >
class UserModel : public CWF ::Model
{
Q_OBJECT
Q_PROPERTY (QString name READ getName WRITE setName)
Q_PROPERTY (QString phone READ getPhone WRITE setPhone)
Q_PROPERTY (QString country READ getCountry WRITE setCountry)
Q_PROPERTY (QString state READ getState WRITE setState)
QString name;
QString phone;
QString country;
QString state;
public:
explicit UserModel (CWF::SqlDatabaseStorage &connection) : CWF::Model(connection, " usuario " ) {}
public slots:
QString getName () const { return name; }
void setName ( const QString &value) { name = value; }
QString getPhone () const { return phone; }
void setPhone ( const QString &value) { phone = value; }
QString getCountry () const { return country; }
void setCountry ( const QString &value) { country = value; }
QString getState () const { return state; }
void setState ( const QString &value) { state = value; }
};
# endif // USERMODEL_H
jemalloc可选安装(推荐)
测试 C++ Web 框架示例的步骤
使用 Docker 容器测试 C++ Web 框架的 HelloWorldDocker 示例的步骤