c++cppcms

CppCMS URL Mapping issue


I'm playing around with CppCMS and I've got the static "Hello World" to work. However I'm having a real hard time getting the URL mapping to work. I'm sure I'm being silly and missing something obvious.

The ISSUE I'm having is the URL's don't seem to be working. When I attempt to visit :8080/home/smile I just receive the default "Main" page.

Here's the code:

`#include <cppcms/application.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppcms/applications_pool.h>
#include <iostream>
#include <stdlib.h>

class hello : public cppcms::application {
public:
    hello(cppcms::service &srv) :
            cppcms::application(srv)
    {
    dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
    mapper().assign("number","/number/{1}");

    dispatcher().assign("/smile",&hello::smile,this);
    mapper().assign("smile","/smile");

    dispatcher().assign("",&hello::welcome,this);
    mapper().assign("");

    mapper().root("/hello");
    }
        void number(std::string num)
    {
        int no = atoi(num.c_str());
        response().out() << "The number is " << no << "<br/>\n";
        response().out() << "<a href='" << url("/") << "'>Go back</a>";
    }
        void smile()
    {
        response().out() << ":-) <br/>\n";
        response().out() << "<a href='" << url("/") << "'>Go back</a>";
    }
        void welcome()
    {
        response().out() <<
            "<h1> Welcome To Page with links </h1>\n"
            "<a href='" << url("/number",1)  << "'>1</a><br>\n"
            "<a href='" << url("/number",15) << "'>15</a><br>\n"
            "<a href='" << url("/smile") << "' >:-)</a><br>\n";
    }
    virtual void main(std::string url);
};

void hello::main(std::string /*url*/)
{
    response().out() <<
       "<html>\n"
        "<body>\n"
       "  <h1>Hello World</h1>\n"
       "<center><br>This is a simple C++ website</br></center>"
    "</body>\n"
      "</html>\n";

}

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

Any help appreciated.


Solution

  • Looks like the original tutorial forgets to mention that you need remove the virtual void main function it made in the first tutorial. If you remove that it works as intended.

    Here is the fixed source code:

    #include <cppcms/application.h>
    #include <cppcms/service.h>
    #include <cppcms/http_response.h>
    #include <cppcms/url_dispatcher.h>
    #include <cppcms/url_mapper.h>
    #include <cppcms/applications_pool.h>
    #include <iostream>
    #include <stdlib.h>
    
    class hello : public cppcms::application {
    public:
        hello(cppcms::service &srv) :
                cppcms::application(srv)
        {
        dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
        mapper().assign("number","/number/{1}");
    
        dispatcher().assign("/smile",&hello::smile,this);
        mapper().assign("smile","/smile");
    
        dispatcher().assign("",&hello::welcome,this);
        mapper().assign("");
    
        mapper().root("/hello");
        }
            void number(std::string num)
        {
            int no = atoi(num.c_str());
            response().out() << "The number is " << no << "<br/>\n";
            response().out() << "<a href='" << url("/") << "'>Go back</a>";
        }
            void smile()
        {
            response().out() << ":-) <br/>\n";
            response().out() << "<a href='" << url("/") << "'>Go back</a>";
        }
            void welcome()
        {
            response().out() <<
                "<h1> Welcome To Page with links </h1>\n"
                "<a href='" << url("/number",1)  << "'>1</a><br>\n"
                "<a href='" << url("/number",15) << "'>15</a><br>\n"
                "<a href='" << url("/smile") << "' >:-)</a><br>\n";
        }
    
    };
    
    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;
     }
    }