rikitiki  v0.1.67
Build C++ web server modules that allow easy routing and deployment.
advanced.h
1 /* Copyright (C) 2012-2013 Justin Berger
2  The full license is available in the LICENSE file at the root of this project and is also available at http://opensource.org/licenses/MIT. */
3 
4 #include <rikitiki/rikitiki>
5 #include <rikitiki/configuration/configuration>
6 #include <rikitiki/ctemplate/ctemplate>
7 
8 using namespace rikitiki;
9 using namespace rikitiki::ctemplates;
10 
11 namespace rikitiki {
12  namespace examples {
25  struct AdvancedModule {
26 
27  void Register(Server& server){
28  server.AddPreprocessor( new HeaderFooterPreprocessor() );
29  typedef AdvancedModule T;
30  server.AddHandler( CreateRoute<>::With(this, "/adv", &T::Index) );
31  server.AddHandler( CreateRoute<>::With(this, "/adv/post", &T::Get, ConnContext::GET ) );
32  server.AddHandler( CreateRoute<>::With(this, "/adv/post", &T::Post, ConnContext::POST ));
33  server.AddHandler( CreateRoute<>::With(this, "/adv/qs", &T::QueryString));
34  }
35 
36  void Index(ConnContext& ctx){
37  ctemplate::TemplateDictionary td("commands.tpl");
38  for(unsigned int i = 0;i < ctx.server->handlers.size();i++){
39  if(ctx.server->handlers[i]->visible()){
40  ctemplate::TemplateDictionary* row = td.AddSectionDictionary("HANDLER");
41  row->SetValue("name", ctx.server->handlers[i]->name());
42  row->SetValue("description", ctx.server->handlers[i]->desc());
43  }
44  }
45  ctx << td;
46  }
47 
48  void makePage(ConnContext& ctx, const std::string& message){
49  ctemplate::TemplateDictionary td("example.tpl");
50  td.SetValue("message", message.size() ? message : "You didn't enter anything!");
51  ctx << td;
52  }
53 
54  void Post(ConnContext& ctx){
55  makePage(ctx, ctx.Post()["message"]);
56  }
57 
58  void Get(ConnContext& ctx){
59  makePage(ctx, "Please enter a message below");
60  }
61 
62  void QueryString(ConnContext& ctx){
63  ctx << ContentType::application_javascript
64  << "{";
65  for(auto param : ctx.QueryString()){
66  ctx << "\t '" << param.first << "': '" << param.second << "', \n";
67  }
68  ctx << "}";
69  }
70  };
71 
72  }
73 }
74