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

How to print out all request routes, including intercepted or intercepted requests without content type #3105

Closed
zkcrescent opened this issue Dec 1, 2023 · 3 comments
Labels
question Further information is requested

Comments

@zkcrescent
Copy link

protobuf 生成的HTTP路由,middleware 和filter 都不行

@zkcrescent zkcrescent added the bug Something isn't working label Dec 1, 2023
Copy link

dosubot bot commented Dec 1, 2023

🤖

Hello @zkcrescent,

Welcome to the Kratos repository! I'm Dosu, a bot here to assist you. While we wait for a human maintainer to respond, feel free to ask me anything regarding solving bugs, answering questions, or even becoming a contributor. I'm here to assist you in the meantime.

在Kratos框架中,您可以使用中间件来打印所有请求路由,包括被拦截的或者没有content type被拦截的请求。中间件是一系列处理器的链,可以在请求到达最终处理器之前处理这些请求。在您的情况下,您可以创建一个中间件来记录所有传入的请求。

在提供的上下文中,已经定义了三个中间件函数:corsFilterauthFilterloggingFilter。每个函数都会记录请求的方法和URI。您可以修改这些函数以记录更多信息。

以下是如何修改loggingFilter以记录关于请求的更多信息的示例:

func loggingFilter(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Log the method, URI, and content type of the request
		log.Println("logging:", r.Method, r.RequestURI, r.Header.Get("Content-Type"))
		// Call the next handler, which can be another middleware in the chain, or the final handler.
		next.ServeHTTP(w, r)
	})
}

TestRoute函数中,使用Filter函数将loggingFilter添加到服务器:

srv := NewServer(
	Filter(corsFilter, loggingFilter),
)

这意味着loggingFilter将应用于服务器处理的所有请求。因此,所有请求,包括被拦截的或者没有content type被拦截的请求,都将被记录。

Kratos框架通过使用Router结构来处理由protobuf生成的HTTP路由。Router结构有一个名为Handle的方法,该方法使用URL路径和方法的匹配器注册新路由。

Handle方法接受HTTP方法,相对路径,HandlerFunc和可选的过滤器。HandlerFunc是一个服务HTTP请求的函数。过滤器是可以修改HTTP请求或响应的函数。

Router结构还有每个HTTP方法(GET,POST,PUT,PATCH,DELETE,CONNECT,OPTIONS,TRACE)的便利方法,这些方法使用适当的HTTP方法调用Handle方法。

Kratos框架通过使用中间件来处理被拦截的请求。Kratos中的中间件是一系列处理器的链,每个处理器决定是否将请求传递给链中的下一个处理器。

在提供的上下文中,使用Invoke方法进行远程过程调用。此方法创建一个新的HTTP请求并设置必要的头。然后使用transport.NewClientContext函数更新请求的上下文。

然后调用invoke方法,该方法使用中间件链来处理请求。使用middleware.Chain函数创建中间件链,该函数接受一系列中间件函数并返回一个新的中间件函数,该函数按照提供的顺序调用每个中间件函数。

希望这些信息对您有所帮助。如果您有任何其他问题或需要进一步的澄清,请随时告诉我。

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@kratos-ci-bot kratos-ci-bot changed the title 如何打印出所有的请求路由,包含被拦截的或者没有content type 被拦截的请求 How to print out all request routes, including intercepted or intercepted requests without content type Dec 1, 2023
@shenqidebaozi
Copy link
Member

Based on what you have mentioned so far, I am not sure what exactly you want to ask

@shenqidebaozi shenqidebaozi added question Further information is requested and removed bug Something isn't working labels Dec 3, 2023
@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Mar 3, 2024
Copy link

dosubot bot commented Mar 3, 2024

Hi, @zkcrescent,

I'm helping the Kratos team manage their backlog and am marking this issue as stale. The issue was opened by you and is related to difficulty in printing out all request routes, including intercepted requests without content type, using protobuf-generated HTTP routes, middleware, and filters. There has been some activity on the issue, including a response from dosubot providing a detailed explanation and example code related to the issue. Additionally, shenqidebaozi has expressed uncertainty about the specific question being asked.

Could you please confirm if this issue is still relevant to the latest version of the Kratos repository? If it is, please let the Kratos team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.

Thank you for your understanding and cooperation.

@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants