Skip to content

Custom Handlers #60

Answered by fundon
m1212e asked this question in Q&A
Discussion options

You must be logged in to vote

Currently, AFIT is not stable, check here. So we cant do that.

Use async-trait crate to helping us.

#[derive(Clone)]
struct Handler {
  /**
   * Request handlers for incoming requests to this server
   */
  handlers: Arc<tokio::sync::RwLock<HashMap<String, HandlerFunction>>>,
}

#[async_trait::async_trait]
impl viz::Handler<Request> for Handler {
  type Output = viz::Result<Response>;

  async fn call(&self, req: Request) -> Self::Output {
      let path = req.path().clone();
      let method = req.method().clone();
      let count = self.count.fetch_add(1, Ordering::SeqCst);
      Ok(format!("method = {method}, path = {path}, count = {count}").into_response())
  }
}

Sorry, I forgot to ad…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by m1212e
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants