Skip to content

Latest commit

 

History

History
94 lines (72 loc) · 2.29 KB

README.md

File metadata and controls

94 lines (72 loc) · 2.29 KB

Gin GAE Middleware Build Status GoDoc Coverage Status

Gin middlewares providing Google App Engine integrations.

Provided middlewares

Only checked are provided currently.

  • GAE Context - Set a variable on the Gin context, containing the GAE Context.
  • GAE User
    • Set a variable on the Gin context, containing the GAE User, logged in using the standard user authentication.
    • Set a variable on the Gin context, containing the GAE User, logged in using OAuth.
  • GAE Authentication - Fail a request with a 401 if user is not authenticated.

Usage

You always have to include GAE Context, as all the others depend on that.

GAE Context

package app

import (
	"appengine"
	"github.com/frankbille/gingae"
	"github.com/gin-gonic/gin"
)

func init() {
	r := gin.New()
	r.Use(gingae.GaeContext())
	r.GET("/posts", func(c *gin.Context) {
		gaeCtx := c.Get(gingae.Context).(appengine.Context)
		
		// Do stuff which requires the GAE Context
	})
	http.Handle("/", r)
}

GAE User (standard)

package app

import (
	"appengine/user"
	"github.com/frankbille/gingae"
	"github.com/gin-gonic/gin"
)

func init() {
	r := gin.New()
	// You always have to include GaeContext, as all the other middlewares depend on it.
	r.Use(gingae.GaeContext())
	r.Use(gingae.GaeUser())
	r.GET("/admin", func(c *gin.Context) {
		gaeUser := c.Get(gingae.User).(user.User)
		
		// Do stuff with the GAE User
	})
	http.Handle("/", r)
}

GAE User (OAuth)

package app

import (
	"appengine/user"
	"github.com/frankbille/gingae"
	"github.com/gin-gonic/gin"
)

func init() {
	r := gin.New()
	// You always have to include GaeContext, as all the other middlewares depend on it.
	r.Use(gingae.GaeContext())
	r.Use(gingae.GaeUserOAuth("profile"))
	r.GET("/admin", func(c *gin.Context) {
		if c.Get(gingae.UserOAuthError) != nil {
			// Handle OAuth failures
		}
		
		gaeUser := c.Get(gingae.User).(user.User)
		
		// Do stuff with the GAE User
	})
	http.Handle("/", r)
}