如何在Goji(Golang)中使用不同的中间件创建单独的路由组? [英] How can I create separate route groups with different middleware in Goji (Golang)?

查看:142
本文介绍了如何在Goji(Golang)中使用不同的中间件创建单独的路由组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Goji( https://github.com/zenazn/goji ),并希望定义具有自己的中间件的路由组。例如, / company 下的所有路径都应使用LDAP身份验证,并定义一个中间件来执行此操作。 / external 下的所有路径使用不同类型的身份验证,因此它们具有不同的中间件定义。但是,这是一个单一的应用程序服务于同一个端口,所以我不想完全创建单独的Web服务 - 只是路径(和一些特定的路线)可能会使用不同的中间件。



我在Goji看到的所有示例都使用一套中间件来处理所有路由,所以我不确定如何以一种干净的方式来完成此任务。此外,如果我可以为路由组中的所有路由指定基本路径,那就好了,类似于我在其他一些路由框架中看到的。



我是在Goji库(或net / http的扩展名)中缺少这个功能,它允许我将路由组合在一起,并让每个组使用自己的中间件堆栈?



我会就像这样(psedocode):

  //使用LDAP身份验证器:
// GET / company / employees
//和
// POST / company / records
companyGroup =& RouteGroup {basePath:/ company}
companyGroup.Use(LDAPAuthenticator )
companyGroup.Add(goji.Get(/ employees,Employees.ListAll))
companyGroup.Add(goji.Post(/ records,Records.Create))

//使用特殊的外部用户身份验证程序:GET / external / products
externalGroup =& RouteGroup {basePath:/ external}
externalGroup.Use(ExternalUserAuthenticator)
externalG roup.Add(goji.Get(/ products,Products.ListAll))


解决方案



  //使用LDAP身份验证程序
companyGroup:= web.New()
companyGroup.Use(LDAPAuthenticator)
companyGroup.Get(/ company / employees,Employees.ListAll)
companyGroup.Post( / company / records,Records.Create)
goji.Handle(/ company / *,companyGroup)

//使用特殊的外部用户身份验证程序:GET / external / products
externalGroup:= web.New()
externalGroup.Use(ExternalUserAuthenticator)
externalGroup.Get(/ external / products,Products.ListAll)
goji.Handle( / external / *,externalGroup)

您需要为每个组分配网络。请记住,您需要在组成员中指定完整路径。


I am using Goji (https://github.com/zenazn/goji) and would like to define groups of routes that have their own middleware. For example, all paths under /company should use an LDAP authentication and have a middleware defined to do this. All paths under /external use a different type of authentication so they have a different middleware definition. But this is a single application served on the same port, so I don't want to create separate web services altogether -- just the paths (and some specific routes) may use different middleware.

All the examples I've seen with Goji are using a single set of middleware for all routes, so I am not sure how to accomplish this in a clean way. Additionally it would be nice if I could specify a base path for all routes within a route group, similar to how I've seen in some other routing frameworks.

Am I missing this functionality in the Goji library (or net/http by extension) that allows me to group routes together and have each group use its own middleware stack?

What I would like to achieve is something like this (psedocode):

// Use an LDAP authenticator for:
// GET /company/employees
// and
// POST /company/records
companyGroup = &RouteGroup{"basePath": "/company"}
companyGroup.Use(LDAPAuthenticator)
companyGroup.Add(goji.Get("/employees", Employees.ListAll))
companyGroup.Add(goji.Post("/records", Records.Create))

// Use a special external user authenticator for: GET /external/products
externalGroup = &RouteGroup{"basePath": "/external"}
externalGroup.Use(ExternalUserAuthenticator)
externalGroup.Add(goji.Get("/products", Products.ListAll))

解决方案

You should be able to solve your problem with something like this:

// Use an LDAP authenticator 
companyGroup := web.New()
companyGroup.Use(LDAPAuthenticator)
companyGroup.Get("/company/employees", Employees.ListAll)
companyGroup.Post("/company/records", Records.Create)
goji.Handle("/company/*", companyGroup)

// Use a special external user authenticator for: GET /external/products
externalGroup := web.New()
externalGroup.Use(ExternalUserAuthenticator)
externalGroup.Get("/external/products", Products.ListAll)
goji.Handle("/external/*", externalGroup)

You need to give each group its own web. Just keep in mind you need to specify the full path within the group members.

这篇关于如何在Goji(Golang)中使用不同的中间件创建单独的路由组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆