Skip to content
This repository has been archived by the owner on Nov 15, 2019. It is now read-only.

Introduction

VOID_133 edited this page Aug 6, 2015 · 6 revisions

Welcome to the cinatra wiki! cinatra–a sinatra inspired modern c++ web framework

What is cinatra?

Cinatra is a opensource project created by Purecpp community, Now we release the first version cinatra0.9.0. Cinatra is a modern web framework written in modern C++, aiming at providing developer with a stable easy-to-use,flexible and high-performance web framework . Cinatra can make developers fully focus on the core logic of their code regardless of the details of http. Cinatra is inspired by sinatra , and it's different from it.

How to use it

  • Download the code from github git clone https://github.com/topcpporg/cinatra.git
  • Install boost library,因为框架用到asio和coroutine,需要1.57及以上的版本。
  • Compile it! Both Cmakelist and Visual Studio 2013 Project file is given, you can directly compile it from source on either Linux or Windows platform

Samples

#include <cinatra/cinatra.hpp>
using namespace cinatra;
int main()
{
	SimpleApp app;
	app.route("/", [](Request&  req , Response& res)
	{
		res.end("Hello Cinatra");
	});
	app.listen("http").run();
	return 0;
}

When the code is running , just type 127.0.0.1 in your browser , you will see "Hello Cinatra" on it. Isn't it easy to use? Cintra has helped you with lots of things, you just need to focus on the core logic, let's go on and see a more tricky example.

#include <cinatra/cinatra.hpp>
using namespace cinatra;
int main()
{
	SimpleApp app;
	app.route("/hello", [](Request&  req , Response& res)
	{
	        res.end("Hello " + req.query().get_val("name"));	
    });
	app.route("/hello/:name/:age", [](Request& req, Response& res, const std::string& a, int b)
	{
		res.end("Name: " + a + " Age: " + boost::lexical_cast<std::string>(b));
	});
 
	app.listen("http").run();
	
	return 0;
}

浏览器中输入:127.0.0.1/hello?name=test&age=12,页面将输出” Hello test”。

**提示:**为了让用户用起来更方便,我们还支持下面这种url请求方式:127.0.0.1/hello/test/12,这个url将会被路由到”/hello/:name/:age”对应的handler:[](Request& req, Response& res, const std::string& a, int b);

Router不仅仅支持lambda表达式还支持类的成员函数,如果你想把handler放到类对象中,你可以这样做。

struct MyStruct
{
	void hello(Request& req, Response& res)
	{
		res.end("Hello " + req.session().get<std::string>("uid") + "!");
	}
};
MyStruct t;
// 访问/hello
app.route("/hello", &MyStruct::hello, &t);

cinatra不仅仅使用简单,还很灵活,它支持AOP,我们可以很方便的将非核心逻辑和核心逻辑分离,比如下面的例子。

struct CheckLoginAspect
{
	void before(Request& req, Response& res)
	{
		//如果session没有uid且访问的不是login和test_post页面
		if (!req.session().exists("uid")&&req.path()!="/login.html"&&
			req.path() != "/test_post"&&req.path().compare(0, 7, "/public"))
 		{
 			// 跳转到登陆页面
 			res.redirect("/login.html");
 		}
	}
 
	void after(Request&  req , Response&  res)
	{
 
	}
};
 
#include <cinatra/cinatra.hpp>
using namespace cinatra;
int main()
{
	Cinatra<CheckLoginAspect> app;
	app.route("/", [](Request&  req , Response& res)
	{
		res.end("Hello Cinatra");
	});
	app.listen("http").run();
	
	return 0;
}

上面的例子中我们增加了一个检查是否登录的切面,如果用户没登录将会重定向到登录页面。我们还可以自由组合多个切面,cinatra可以很方便地扩展任意多个切面。你可以这样扩展切面。

struct CheckLoginAspect
{
	void before(Request& req, Response& res)
	{
		//如果session没有uid且访问的不是login和test_post页面
		if (!req.session().exists("uid")&&req.path()!="/login.html"&&
			req.path() != "/test_post"&&req.path().compare(0, 7, "/public"))
 		{
 			// 跳转到登陆页面
 			res.redirect("/login.html");
 		}
	}
 
	void after(Request&  req , Response&  res)
	{
 
	}
};
 
struct LogAspect
{
	void before(cinatra::Request& req, cinatra::Response& res)
	{
		std::cout << "log before" << std::endl;
	}
 
	void after(cinatra::Request& /* req */, cinatra::Response& /* res */)
	{
		std::cout << "log after" << std::endl;
	}
};
 
#include <cinatra/cinatra.hpp>
using namespace cinatra;
int main()
{
	Cinatra<CheckLoginAspect, LogAspect> app; //扩展了一个记录日志的切面
	app.route("/", [](Request&  req , Response& res)
	{
		res.end("Hello Cinatra");
	});
 
	app.listen("http").run();
 
	return 0;
}

性能

用ab工具和另外一个c++ web框架crow做了一下性能对比:

crow:

cinatra:

可以看到cinatra比crow的性能略高。现在第一个版本还没有专门去做优化,主要是完成了基本功能,后续会持续优化的,也欢迎大家帮忙做进一步的性能测试。

cinatra的设计

cinatra的设计非常简单,只有几个组件,下面是cinatra的逻辑视图。

用户仅用cinatra即可,其它的事情框架已经帮用户做好了,用户只用关注核心逻辑即可,这些核心逻辑都在handler中处理,而这些handler完全由用户自定义和扩展。

roadmap

目前支持了http1.0和1.1,支持了session和cookie。

后续计划:

  1. https
  2. html模板
  3. websocket
  4. cinatra打造purecpp社区

C++开源社区http://purecpp.org/

更多的例子请到社区和github上看。

如果你发现了问题请及时到社区反馈给我们,也欢迎提出宝贵意见。希望更多的人能参与进来。

如果你觉得cinatra不错,请不要吝惜给一个star^_^。

Clone this wiki locally