didn't realize gin supports net.Context. Change to support Context pattern!

This commit is contained in:
Brad Rydzewski
2015-10-21 16:14:02 -07:00
parent af2ef2347a
commit cfdfbcfd3b
56 changed files with 1495 additions and 1051 deletions

View File

@@ -1,24 +1,19 @@
package context
import (
"database/sql"
"github.com/drone/drone/engine"
"github.com/drone/drone/remote"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
func SetDatabase(db *sql.DB) gin.HandlerFunc {
func SetStore(s store.Store) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("database", db)
store.ToContext(c, s)
c.Next()
}
}
func Database(c *gin.Context) *sql.DB {
return c.MustGet("database").(*sql.DB)
}
func SetRemote(remote remote.Remote) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("remote", remote)

View File

@@ -4,8 +4,9 @@ import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/context"
"github.com/drone/drone/remote"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/store"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
@@ -42,9 +43,8 @@ func SetRepo() gin.HandlerFunc {
name = c.Param("name")
)
db := context.Database(c)
user := User(c)
repo, err := model.GetRepoName(db, owner, name)
repo, err := store.GetRepoOwnerName(c, owner, name)
if err == nil {
c.Set("repo", repo)
c.Next()
@@ -55,7 +55,7 @@ func SetRepo() gin.HandlerFunc {
// to see if the repository actually exists. If yes,
// we can prompt the user to add.
if user != nil {
remote := context.Remote(c)
remote := remote.FromContext(c)
repo, err = remote.Repo(user, owner, name)
if err != nil {
log.Errorf("Cannot find remote repository %s/%s for user %s. %s",
@@ -107,7 +107,6 @@ func SetPerm() gin.HandlerFunc {
return func(c *gin.Context) {
user := User(c)
repo := Repo(c)
remote := context.Remote(c)
perm := &model.Perm{}
if user != nil {
@@ -148,7 +147,7 @@ func SetPerm() gin.HandlerFunc {
// check the remote system to get the users permissiosn.
default:
var err error
perm, err = remote.Perm(user, repo.Owner, repo.Name)
perm, err = remote.FromContext(c).Perm(user, repo.Owner, repo.Name)
if err != nil {
perm.Pull = false
perm.Push = false

View File

@@ -4,8 +4,8 @@ import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/context"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
@@ -39,9 +39,8 @@ func SetUser() gin.HandlerFunc {
var user *model.User
t, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
var db = context.Database(c)
var err error
user, err = model.GetUserLogin(db, t.Text)
user, err = store.GetUserLogin(c, t.Text)
return user.Hash, err
})
if err == nil {

View File

@@ -3,10 +3,9 @@ package token
import (
"time"
"github.com/drone/drone/model"
"github.com/drone/drone/remote"
"github.com/drone/drone/router/middleware/context"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/store"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
@@ -21,7 +20,7 @@ func Refresh(c *gin.Context) {
// check if the remote includes the ability to
// refresh the user token.
remote_ := context.Remote(c)
remote_ := remote.FromContext(c)
refresher, ok := remote_.(remote.Refresher)
if !ok {
c.Next()
@@ -41,8 +40,7 @@ func Refresh(c *gin.Context) {
// database.
ok, _ = refresher.Refresh(user)
if ok {
db := context.Database(c)
err := model.UpdateUser(db, user)
err := store.UpdateUser(c, user)
if err != nil {
// we only log the error at this time. not sure
// if we really want to fail the request, do we?

View File

@@ -139,7 +139,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
auth.POST("/token", controller.GetLoginToken)
}
gitlab := e.Group("/api/gitlab/:owner/:name")
gitlab := e.Group("/gitlab/:owner/:name")
{
gitlab.Use(session.SetRepo())
gitlab.GET("/commits/:sha", controller.GetCommit)
@@ -162,7 +162,7 @@ func normalize(h http.Handler) http.Handler {
parts := strings.Split(r.URL.Path, "/")[1:]
switch parts[0] {
case "settings", "api", "login", "logout", "", "authorize", "hook", "static":
case "settings", "api", "login", "logout", "", "authorize", "hook", "static", "gitlab":
// no-op
default: