英文:
Organize routes and helpers
问题
我正在构建一个使用Go语言(golang)编写的API,但我有几个问题...
所以在我的main函数或init函数中(因为我可能会使用appengine),我打算调用一个函数,该函数将使用gorilla mux定义所有的路由。我的应用程序的每个部分(帖子、评论等)都将有自己的包,其中包含结构/方法/函数。
问题:
-
因为我打算在一个函数中定义路由,所以我需要在这个文件中导入所有的包,以便将请求发送到正确的处理程序吗?
-
对于辅助函数,例如我想要将响应的
content type
设置为application/json
,以满足所有需要的处理程序,我该如何做到这一点?
我不是在寻找框架,只是想知道如何以Go语言的方式解决这些问题。
英文:
I am starting building a api in go(golang), but I have few questions...
So in my main function or init function(because I might use appengine) I was thinking in calling a function which will define all my routes using gorilla mux. Each pice of my application(post, comments etc...) will have its one package with its structures/methods/functions.
Questions:
-
Because I was thinking in defining the routes in one function, do I need to import in this file all my packages, to send the requests to the right handlers?
-
What about helper function, for example I would like to set
content type
of the response to beapplication/json
for all the handlers where this is necessary, how I will be able to do that?
I'm not looking for frameworks, just some pointer about how can I overcome those questions in golang way.
答案1
得分: 4
如果您在一个函数中定义了所有的路由,那么包含该函数的文件将需要导入实现处理程序的包。引用另一个包中的类型或函数的唯一方法是导入该包。
下面是一个帮助函数,用于设置内容类型并将值编码为JSON:
func JSONHandler(f func(w http.ResponseWriter, r *http.Request) interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := f(w, r)
if v != nil {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(v); err != nil {
log.Println(err)
}
}
})
}
该函数的参数是一个返回要编码为JSON响应的值的函数。例如,此函数将客户端的用户代理作为JSON返回。
func UserAgentHandler(w http.ResponseWriter, r *http.Request) interface{} {
return struct { UserAgent string }{ req.Header.Get("User-Agent") }
}
使用以下代码将此处理程序与Gorilla mux的r
注册:
r.Handle("/user-agent", JSONHandler(UserAgentHandler))
有许多方法可以改进JSONHandler。
英文:
If you define all of the routes in a single function, then the file containing this function will need to import the packages that implement the handlers. The only way to refer to a type or function in another package is to import the package.
Here's a helper for setting the content type and encoding a value to JSON:
func JSONHandler(f func(w http.ResponseWriter, r *http.Request) interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := f(w, r)
if v != nil {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(v); err != nil {
log.Println(err)
}
}
})
}
The argument to this function is a function that returns a value to encode to the response as JSON. For example, this function returns the client's user agent as JSON.
func UserAgentHandler(w http.ResponseWriter, r *http.Request) interface{} {
return struct { UserAgent string }{ req.Header.Get("User-Agent") }
}
Use the following code to register this handler with the Gorilla mux r
:
r.Handle("/user-agent", JSONHandler(UserAgentHandler))
There are many ways to improve JSONHandler.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论