本文整理匯總了Golang中github.com/gin-gonic/gin.RouterGroup.Use方法的典型用法代碼示例。如果您正苦於以下問題:Golang RouterGroup.Use方法的具體用法?Golang RouterGroup.Use怎麽用?Golang RouterGroup.Use使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.RouterGroup
的用法示例。
在下文中一共展示了RouterGroup.Use方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Setup
// Setup configures how to handle routes
func Setup(group *gin.RouterGroup, db *db.DB) {
group.Use()
{
group.GET("/", renderItemHandler(db))
group.GET("/:shortcode", renderItemHandler(db))
}
}
示例2: SetupMiddlewares
func SetupMiddlewares(group *gin.RouterGroup) {
if command.Opts.Debug {
group.Use(requestInspectMiddleware())
}
group.Use(dbCheckMiddleware())
}
示例3: Use
// Use is a function
func Use(r *gin.RouterGroup) {
r.Use()
{
r.GET("/image/all", getAllImages)
r.GET("/image/search", getSearchImages)
r.POST("/image", createImage)
}
}
示例4: Setup
// Setup will setup all API routes.
func Setup(group *gin.RouterGroup, db *db.DB) {
group.Use()
{
group.GET("/items/:code", itemsShowHandler(db))
group.DELETE("/items/:code", itemsDestroyHandler(db))
group.GET("/items", itemsIndexHandler(db))
group.POST("/items", itemsCreateHandler(db))
}
}
示例5: Use
// Use is an exported function that
// define routing for user
func Use(r *gin.RouterGroup) {
r.Use()
{
r.GET("/user", getUsers)
r.GET("/user/:id", getUser)
r.POST("/user", postUser)
r.PATCH("/user/:id", updateUser)
r.DELETE("/user/:id", deleteUser)
}
}
示例6: Run
func (svc *Service) Run(cfg Config) error {
cfg = cfg // save config in global
// init gin
if !cfg.Debug {
gin.SetMode(gin.ReleaseMode)
}
// Middleware
router := gin.New()
// use glog for logging
router.Use(ginglog.Logger(cfg.LogFlushInterval))
// monitoring GO internals and counter middleware
counterAspect := &ginmon.CounterAspect{0}
asps := []aspects.Aspect{counterAspect}
router.Use(ginmon.CounterHandler(counterAspect))
router.Use(gomonitor.Metrics(9000, asps))
// last middleware
router.Use(gin.Recovery())
svc.ProxyCfg = conf.New()
glog.Infof("svc config: %+v", svc.ProxyCfg)
// OAuth2 secured if conf.Oauth2Enabled is set
var private *gin.RouterGroup
if cfg.Oauth2Enabled {
zalando.AccessTuples = []zalando.AccessTuple{{"employees", "sszuecs", "Sandor Szücs"}, {"employees", "njuettner", "Nick Jüttner"}, {"employees", "rdifazio", "Raffaele Di Fazio"}}
private = router.Group("")
private.Use(ginoauth2.Auth(zalando.UidCheck, cfg.OAuth2Endpoints))
}
//
// Handlers
//
if cfg.Oauth2Enabled {
private.GET("/", svc.rootHandler)
private.GET("/sum/:name", svc.sumHandler)
private.GET("/eql/:name", svc.eqlHandler)
} else {
router.GET("/", svc.rootHandler)
router.GET("/sum/:name", svc.sumHandler)
router.GET("/eql/:name", svc.eqlHandler)
}
// TLS config
var tls_config tls.Config = tls.Config{}
if !cfg.Httponly {
tls_config.Certificates = []tls.Certificate{cfg.CertKeyPair}
tls_config.NextProtos = []string{"http/1.1"}
tls_config.Rand = rand.Reader // Strictly not necessary, should be default
}
// run frontend server
serve := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.SvcPort),
Handler: router,
TLSConfig: &tls_config,
}
if cfg.Httponly {
serve.ListenAndServe()
} else {
conn, err := net.Listen("tcp", serve.Addr)
if err != nil {
panic(err)
}
tlsListener := tls.NewListener(conn, &tls_config)
err = serve.Serve(tlsListener)
if err != nil {
glog.Fatalf("Can not Serve TLS, caused by: %s\n", err)
}
}
return nil
}
示例7: Run
func (svc *Service) Run(cfg ServerSettings) error {
config = cfg // save config in global
// init gin
if !config.Configuration.DebugEnabled {
gin.SetMode(gin.ReleaseMode)
}
var oauth2Endpoint = oauth2.Endpoint{
AuthURL: config.Configuration.AuthURL,
TokenURL: config.Configuration.TokenURL,
}
// Middleware
router := gin.New()
// use glog for logging
router.Use(ginglog.Logger(config.Configuration.LogFlushInterval))
// monitoring GO internals and counter middleware
counterAspect := &ginmon.CounterAspect{0}
asps := []aspects.Aspect{counterAspect}
router.Use(ginmon.CounterHandler(counterAspect))
router.Use(gomonitor.Metrics(9000, asps))
router.Use(ginoauth2.RequestLogger([]string{"uid", "team"}, "data"))
// last middleware
router.Use(gin.Recovery())
// OAuth2 secured if conf.Oauth2Enabled is set
var private *gin.RouterGroup
//ATM team or user auth is mutually exclusive, we have to look for a better solution
if config.Configuration.Oauth2Enabled {
private = router.Group("")
if config.Configuration.TeamAuthorization {
var accessTuple []zalando.AccessTuple = make([]zalando.AccessTuple, len(config.Configuration.AuthorizedTeams))
for i, v := range config.Configuration.AuthorizedTeams {
accessTuple[i] = zalando.AccessTuple{Realm: v.Realm, Uid: v.Uid, Cn: v.Cn}
}
zalando.AccessTuples = accessTuple
private.Use(ginoauth2.Auth(zalando.GroupCheck, oauth2Endpoint))
} else {
var accessTuple []zalando.AccessTuple = make([]zalando.AccessTuple, len(config.Configuration.AuthorizedUsers))
for i, v := range config.Configuration.AuthorizedUsers {
accessTuple[i] = zalando.AccessTuple{Realm: v.Realm, Uid: v.Uid, Cn: v.Cn}
}
private.Use(ginoauth2.Auth(zalando.UidCheck, oauth2Endpoint))
}
}
//non authenticated routes
router.GET("/", rootHandler)
router.GET("/health", healthHandler)
//authenticated routes
if config.Configuration.Oauth2Enabled {
private.GET("/deployments", deployList)
private.GET("/deployments/:name", deployInfo)
private.POST("/deployments", deployCreate)
private.PUT("/deployments/:name", deployUpsert)
private.DELETE("/deployments/:name", deployDelete)
private.PATCH("/deployments/:name/replicas/:num", deployReplicasModify)
} else {
router.GET("/deployments", deployList)
router.GET("/deployments/:name", deployInfo)
router.POST("/deployments", deployCreate)
router.PUT("/deployments/:name", deployUpsert)
router.DELETE("/deployments/:name", deployDelete)
router.PATCH("/deployments/:name/replicas/:num", deployReplicasModify)
}
// TLS config
var tls_config tls.Config = tls.Config{}
if !config.Httponly {
tls_config.Certificates = []tls.Certificate{config.CertKeyPair}
tls_config.NextProtos = []string{"http/1.1"}
tls_config.Rand = rand.Reader // Strictly not necessary, should be default
}
// run backend
Start()
// run frontend server
serve := &http.Server{
Addr: fmt.Sprintf(":%d", config.Configuration.Port),
Handler: router,
TLSConfig: &tls_config,
}
if config.Httponly {
serve.ListenAndServe()
} else {
conn, err := net.Listen("tcp", serve.Addr)
if err != nil {
panic(err)
}
tlsListener := tls.NewListener(conn, &tls_config)
err = serve.Serve(tlsListener)
if err != nil {
glog.Fatalf("Can not Serve TLS, caused by: %s\n", err)
}
}
return nil
}
示例8: InitApi
func InitApi(r *gin.RouterGroup) {
r.Use(recoverWrapper())
r.Use(basicAuthorizer())
InitBuildsController(r.Group("/builds"))
}
示例9: Run
//Run starts Howler
func (svc *Service) Run(cfg ServerSettings) error {
config = cfg // save config in global
// init gin
if !config.Configuration.DebugEnabled {
gin.SetMode(gin.ReleaseMode)
}
var oauth2Endpoint = oauth2.Endpoint{
AuthURL: config.Configuration.AuthURL,
TokenURL: config.Configuration.TokenURL,
}
// Middleware
router := gin.New()
// use glog for logging
router.Use(ginglog.Logger(config.Configuration.LogFlushInterval))
// monitoring GO internals and counter middleware
counterAspect := &ginmon.CounterAspect{0}
asps := []aspects.Aspect{counterAspect}
router.Use(ginmon.CounterHandler(counterAspect))
router.Use(gomonitor.Metrics(9000, asps))
router.Use(ginoauth2.RequestLogger([]string{"uid", "team"}, "data"))
// last middleware
router.Use(gin.Recovery())
// OAuth2 secured if conf.Oauth2Enabled is set
var private *gin.RouterGroup
if config.Configuration.Oauth2Enabled {
private = router.Group("")
var accessTuple = make([]zalando.AccessTuple, len(config.Configuration.AuthorizedUsers))
for i, v := range config.Configuration.AuthorizedUsers {
accessTuple[i] = zalando.AccessTuple{Realm: v.Realm, Uid: v.UID, Cn: v.Cn}
}
zalando.AccessTuples = accessTuple
private.Use(ginoauth2.Auth(zalando.UidCheck, oauth2Endpoint))
}
router.GET("/", rootHandler)
if config.Configuration.Oauth2Enabled {
//authenticated routes
private.GET("/status", getStatus)
private.POST("/events", createEvent)
} else {
//non authenticated routes
router.GET("/status", getStatus)
router.POST("/events", createEvent)
}
// TLS config
var tlsConfig = tls.Config{}
if !config.Httponly {
tlsConfig.Certificates = []tls.Certificate{config.CertKeyPair}
tlsConfig.NextProtos = []string{"http/1.1"}
tlsConfig.Rand = rand.Reader // Strictly not necessary, should be default
}
// run frontend server
serve := &http.Server{
Addr: fmt.Sprintf(":%d", config.Configuration.Port),
Handler: router,
TLSConfig: &tlsConfig,
}
if config.Httponly {
serve.ListenAndServe()
} else {
conn, err := net.Listen("tcp", serve.Addr)
if err != nil {
panic(err)
}
tlsListener := tls.NewListener(conn, &tlsConfig)
err = serve.Serve(tlsListener)
if err != nil {
glog.Fatalf("Can not Serve TLS, caused by: %s\n", err)
}
}
return nil
}
示例10: Setup
// Setup will configure the router group for severing a static
// Ember.js application.
func Setup(group *gin.RouterGroup) {
group.Use()
{
group.Static("", "./public/")
}
}
示例11: prepareMiddleware
func prepareMiddleware(admin, public, registered, sameRegisteredUser *gin.RouterGroup) {
admin.Use(checkAdmin)
registered.Use(checkRegisteredUser)
sameRegisteredUser.Use(checkSameRegisteredUser)
}