Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different URLs call different get requests #10

Open
Alanscut opened this issue Feb 26, 2020 · 5 comments
Open

Different URLs call different get requests #10

Alanscut opened this issue Feb 26, 2020 · 5 comments

Comments

@Alanscut
Copy link

Is your feature request related to a problem? Please describe.
Thank you very much for your project, it helps me a lot since I'm a green hander for C++. According to your demo, I could easily create a server which has 4 different requests, and we visit them through the same url http://localhost:34568 with different request types(GET, POST ..).

But if I want to create another GET request like http://localhost:34568/rest/api, I have no idea how to do it.

Describe the solution you'd like
if you don't mind, please add an example code in this project.

Additional context
what I want to do is that: when visit http://localhost:34568/first, then excute handle_get1, and when visit http://localhost:34568/second, then excute handle_get2.
handler.cpp

// visit it by url "http://localhost:34568/first"
void handler::handle_get1(http_request message) {
  // do some things
}

// visit it by url "http://localhost:34568/seconde"
void handler::handle_get2(http_request message) {
  // do other things
}
@Meenapintu
Copy link
Owner

Hi @Alanscut ,

Thank you for highlighting the feature, it's a good point so I'll add the demo for same .

@Alanscut
Copy link
Author

Thanks, that's great! Could you please offer a simple demo here 😄 ?

@Meenapintu
Copy link
Owner

Meenapintu commented Feb 29, 2020

Hi @Alanscut
Uploading the actual demo code will take some time as I'm stuck in something else but let me put the logic of implementation here with pseudo code to help you out.

I was looking at the code , http_listener listener(baseURI) only supports the single url so best approach would be writing a router middleware that will route the requests according to paths.

So eventually the current handler will act as a router that will redirect the requests to the specified handler.

pseudo logic for same would be and where . suppose you want to route 2 get requests as you mentioned is comment.
As all the get requests will come to the void handler::handle_get(http_request message) method.
so let's modify it.

void handler::handle_get(http_request message){

}.  // current this code will act as ` void route(http_request message);` of myRouter class below.

==================myRouter.h====
class myRouter{
      private: HashContainer<url,handler> routingData;
      ...
   public :

      void registerHandler(string path_OR_pathRegex, myHandler handler); //this will register the paths with handler , you can call it multiple times so register multiple paths. 
      void  registerDefaultHandler( myHandler handler); //register for undefined paths. 
      void route(http_request message); //this will route to specific handler registered by  `registerHandler` method for a path. 
      ...

}

class myHandler{
      void handle_get1(http_request message);
      void handle_get2(http_request message);
}
// now register your logic handler like this.
myRouter.register("/seconde",&myhandler::handle_get2);
myRouter.register("/first",&myhandler::handle_get1);


Also this can be extend to domain name based routing.

ActionItems:

  1. I've to do some research that, Is there exists such router library implementation or have to implement.

@Alanscut
Copy link
Author

Dear Meena, thanks for your patience and friendly instructions, I'll try it later. 👍

@Meenapintu
Copy link
Owner

Hi @Alanscut ,

Thanks , I've noted the action Item and will complete this asap but If you implement the router in generic way or find any good library to do such job in the mean time, please let me know and feel free to ask for any help. I'm always ready to help and learn.

Thanks,
All the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants