Web development in C++ 

A short intro to CppCMS

 

Presented by: Ismail Khoffi

At: C++ User group NRW meeting

August 19, 2015

What is this talk about?

some thoughts on web-development in C++/CppCMS

overview of CppCMS

Part 2 (of 2) 

example application (show, discuss some code)

Web development in C++

  • is it possible?
  • is it a good idea?
  • what options exist?

What does Bjarne say?

People say: 'Why can't I write my web-pages in C++?'. 

One: You can, two: Why would you want to? That's not what its specialized for. There are a lots of languages that are specialized for that job.

 

Bjarne Stroustrup at CurryOn 2015 (video here)

"Pros" ...

Performance (embedded systems)

"Make the world a greener and better place"

Performance  !!!

use modern C++ as primary language

... and "cons"

Rapid prototyping?

short time-to-market periods ...

my experience: web-apps often start as small app/prototype and change fast ...

... to grow big and have performance problems! (actually a "pro")

"Conclusion"

extremely powerful for:

  • High load web sites and applications
  • when you do not have other choices (Embedded underpowered devices)
  • integrating a web interface into existing C++ applications/services

 

borrowed from here /more info

"Conclusion" (continued)

do not "exegerate" (choose wisely how much and where 'to C++'):

 

You want interactive and efficient GUI - use client side javascript technology and combine it with solid backed - making it "integrated" is just wrong.

Artynom Beilis (cppCMS core dev)

CppCMS?!

CppCMS is a Free High Performance Web Development Framework (not a CMS) aimed at Rapid Web Application Development. 

CppCMS - Example

Hello World (source)

class hello : public cppcms::application {
public:
    hello(cppcms::service &srv) :
        cppcms::application(srv)
    {
    }
    virtual void main(std::string url);
};

Declaration

CppCMS - Example

void hello::main(std::string /*url*/)
{
    response().out() <<
        "<html>\n"
        "<body>\n"
        "  <h1>Hello C++ User Group NRW</h1>\n"
        "</body>\n"
        "</html>\n";
}

Definition

CppCMS - Example

int main(int argc,char ** argv)
{
    try {
        cppcms::service srv(argc,argv);
        srv.applications_pool().mount(cppcms::applications_factory<hello>());
        srv.run();
    }
    catch(std::exception const &e) {
        std::cerr<<e.what()<<std::endl;
    }
}

main entry point

CppCMS - Example

{
    "service" : {
        "api" : "http",
        "port" : 8080
    },
    "http" : {
        "script" : "/hello"
    }
}

configuration (config.js)

 

run with: 

./hello -c config.js 

CppCMS - why? 

 

well  documented ( API)

 allows to build large yet maintainable and scaleable applications (OOP)

sophisticated templating & caching mechanism

... with performance and security in mind

wide spread usage

integrated JSON handling

CppCMS

more on templates

templates: "V in MVC"

  • special template language 
  • translated to C++ code (via python)
  • can be compiled to a shared/static obj
  • app/controller prepares "content"...
  • ... which is is rendered to HTML

my example code will not use templates

Alternatives (C++)

even more (benchmarks, CppCMS missing):

Alternatives (C++)

even more frameworks (with benchmarks, CppCMS missing):

Example Application

About Todo-backend

inspired by TodoMVC

Example Application

very simple app:

  • no authentication
  • create/delete/modify "todos"
  • front-end/back-end communicate over JSON (unit tests/specs)
  • clear separation of concerns: back-end does not need HTML/Js at all

About Todo-backend

Example Application

kata-like specs (js):

it("adds a new todo to the list of todos at the root url", function(){
    var getAfterPost = postRoot({title:"walk the dog"}).then(getRoot);
    return getAfterPost.then(function(todosFromGet){
      expect(todosFromGet).to.have.length(1);
      expect(todosFromGet[0]).to.have.property("title","walk the dog");
    });
});

About Todo-backend

Example Application

There is no implementation in C++ 

but almost in every other language!

 

The C++ language is far from being a popular Web development language for many reasons: lack of appropriate tools, skills of developers, compilation requirements, slower to change pages and try out ideas, and many more.

Artyom Beilis 

About Todo-backend

Example Application

My Todo-backend implementation

I chose the following setup:

Example Application

My Todo-backend implementation

Thank's for your attention, so far. 

 

Let's go over to part 2 and have a look on some code. Try out the app and discuss it.

Feedback very appreciated...

Made with Slides.com