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 7, 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;
}

Type 127.0.0.1/hello?name=test&age=12 in your browser , You will see "Hello test" on the page

**P.S.**For users' convenience ,We also support the following request method:127.0.0.1/hello/test/12,this URL will be route to the handler correspond to '/hello/:name/:age' [](Request& req, Response& res, const std::string& a, int b);

Router not only supports lambda expressions but also support member functions of a certain class. If you want to put handler into an object you can just follow these instructions below

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

Cinatra is both easy-to-use and flexible, and it supports AOP , users can easily deal with core logic and non-core logic separately . See the following example. struct CheckLoginAspect { void before(Request& req, Response& res) { //If uid doesn't exist in seesion and not visiting login or test_post page if (!req.session().exists("uid")&&req.path()!="/login.html"&& req.path() != "/test_post"&&req.path().compare(0, 7, "/public")) { // navigate to login page 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