how to oragnize handlers in go as project gets bigger?

huangapple go评论86阅读模式
英文:

how to oragnize handlers in go as project gets bigger?

问题

我正在使用Go构建一个网站,我知道如何使用handlefunc()处理路由。然而,我的网站越来越大,可能会有很多URL。问题是如何处理所有这些URL?我不能只是在main()函数中添加100+或500+个handlefunc()来匹配每个可能的路由或URL。那么如何管理它?提前感谢。

注意:

我的问题不需要任何代码。

英文:

I am building a website with Go and I know how it works and how to handle routes with handlefunc(). however, my website is getting bigger and I will probably have alot of urls. the question is how to handle all this ? I cannot just add for example 100+ or 500+ handelfunc() in my main() to match each possible route or url for example. so how to manage it ? thanks in advance.

NOTE ::

my question doesn't require any code.

答案1

得分: 3

你绝对不应该在主函数中放置500+个HandleFunc

首先,随着项目的扩大,你可能会创建模块和类型。

然后,你可以从主函数中委托给这些模块或类型,以便每个模块或类型可以自行注册处理程序。

此外,你的许多URL可能会遵循某种模式,例如:

"/articles/{id}"

如果是这种情况,你应该只注册一次该模式,然后处理请求的函数可以根据参数{id}进行操作。

有几个库可以帮助管理使用模式的路由,或者你也可以编写自己的路由逻辑。

可以参考以下问题,了解一些相关的替代方案:

https://stackoverflow.com/questions/6564558/wildcards-in-the-pattern-for-http-handlefunc

例如,如果你使用Chi(https://github.com/pressly/chi)来管理路由,在主函数中可以这样做:

r := chi.NewRouter()
r.Mount("/admin", adminRouter())

然后在adminRouter()函数中继续创建在/admin下提供服务的处理程序:

func adminRouter() http.Handler {
    r := chi.NewRouter()
    r.Use(AdminOnly)
    r.Get("/", adminIndex)
    r.Get("/accounts", adminListAccounts)
    return r
}

(以上内容摘自Chi的文档)

这只是一个示例,你可以使用不同的方式实现类似的逻辑。

英文:

You definitely should not put 500+ HandleFunc in your main.

To begin with, as the project goes bigger you'll probably create modules and types.

You can then delegate to these modules or types from your main so that then each module or type would then register handles themselves.

Also, probably many of your URLs will follow patterns, such as:

"/articles/{id}"

If that's the case, you should just register that pattern once, and then the function handling the request can operate based on the parameter {id}.

There are several libraries out there that can help manage the routing if using patterns, or you can also code your own routing logic.

Take a look at this question for some alternatives on that matter:

https://stackoverflow.com/questions/6564558/wildcards-in-the-pattern-for-http-handlefunc

For example, if you where using Chi (https://github.com/pressly/chi) to manage the routing, in your main you can do:

r := chi.NewRouter()
r.Mount("/admin", adminRouter())

And then inside that adminRouter() function you keep creating handlers that will be served under /admin:

func adminRouter() http.Handler {
    r := chi.NewRouter()
    r.Use(AdminOnly)
    r.Get("/", adminIndex)
    r.Get("/accounts", adminListAccounts)
    return r
}

(all this is taken from Chi's documentation)

This is just an example, you can implement similar logic using a different way.

huangapple
  • 本文由 发表于 2017年5月20日 01:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/44075681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定