woodpecker/controller/user.go

83 lines
1.7 KiB
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package controller
2015-04-08 22:43:59 +00:00
import (
2015-09-30 01:21:17 +00:00
"net/http"
2015-09-09 21:34:28 +00:00
2015-09-30 01:21:17 +00:00
"github.com/gin-gonic/gin"
2015-04-08 22:43:59 +00:00
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/context"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/shared/token"
"github.com/hashicorp/golang-lru"
2015-04-08 22:43:59 +00:00
)
2015-09-30 01:21:17 +00:00
var cache *lru.Cache
2015-09-09 21:34:28 +00:00
2015-09-30 01:21:17 +00:00
func init() {
var err error
cache, err = lru.New(1028)
if err != nil {
panic(err)
}
2015-04-08 22:43:59 +00:00
}
2015-09-30 01:21:17 +00:00
func GetSelf(c *gin.Context) {
c.IndentedJSON(200, session.User(c))
}
2015-04-08 22:43:59 +00:00
2015-09-30 01:21:17 +00:00
func GetFeed(c *gin.Context) {
user := session.User(c)
db := context.Database(c)
feed, err := model.GetUserFeed(db, user, 25, 0)
2015-04-08 22:43:59 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithStatus(http.StatusInternalServerError)
return
2015-04-08 22:43:59 +00:00
}
2015-09-30 01:21:17 +00:00
c.IndentedJSON(http.StatusOK, feed)
2015-04-08 22:43:59 +00:00
}
2015-09-30 01:21:17 +00:00
func GetRepos(c *gin.Context) {
user := session.User(c)
db := context.Database(c)
repos, err := model.GetRepoList(db, user)
2015-04-08 22:43:59 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithStatus(http.StatusInternalServerError)
return
2015-04-08 22:43:59 +00:00
}
2015-09-30 01:21:17 +00:00
c.IndentedJSON(http.StatusOK, repos)
2015-04-08 22:43:59 +00:00
}
2015-04-11 05:22:55 +00:00
2015-09-30 01:21:17 +00:00
func GetRemoteRepos(c *gin.Context) {
user := session.User(c)
remote := context.Remote(c)
// attempt to get the repository list from the
// cache since the operation is expensive
v, ok := cache.Get(user.Login)
if ok {
c.IndentedJSON(http.StatusOK, v)
return
}
repos, err := remote.Repos(user)
2015-08-18 23:09:27 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithStatus(http.StatusInternalServerError)
return
2015-08-18 23:09:27 +00:00
}
2015-09-30 01:21:17 +00:00
cache.Add(user.Login, repos)
c.IndentedJSON(http.StatusOK, repos)
2015-08-18 23:09:27 +00:00
}
2015-09-30 01:21:17 +00:00
func PostToken(c *gin.Context) {
user := session.User(c)
2015-09-09 21:34:28 +00:00
token := token.New(token.UserToken, user.Login)
tokenstr, err := token.Sign(user.Hash)
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusInternalServerError, err)
} else {
2015-09-30 01:21:17 +00:00
c.String(http.StatusOK, tokenstr)
}
2015-04-11 05:22:55 +00:00
}