load ui assets from filesystem

This commit is contained in:
Brad Rydzewski 2017-03-20 23:51:41 +08:00
parent 7f7f9235cc
commit f0cbe62d81
8 changed files with 206 additions and 328 deletions

View file

@ -2,6 +2,7 @@ package router
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
@ -21,12 +22,20 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.SetHTMLTemplate(template.Load())
if pattern := os.Getenv("DRONE_TEMPLATE_GLOB"); pattern == "" {
e.SetHTMLTemplate(template.Load())
} else {
e.SetHTMLTemplate(template.Glob(pattern))
}
fs := http.FileServer(dist.AssetFS())
e.GET("/static/*filepath", func(c *gin.Context) {
fs.ServeHTTP(c.Writer, c.Request)
})
if dir := os.Getenv("DRONE_STATIC_DIR"); dir == "" {
fs := http.FileServer(dist.AssetFS())
e.GET("/static/*filepath", func(c *gin.Context) {
fs.ServeHTTP(c.Writer, c.Request)
})
} else {
e.Static("/static", dir)
}
e.Use(header.NoCache)
e.Use(header.Options)
@ -174,3 +183,10 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
return e
}
// type FileHandler interface {
// Index(res http.ResponseWriter, data interface{}) error
// Login(res http.ResponseWriter, data interface{}) error
// Error(res http.ResponseWriter, data interface{}) error
// Asset(res http.ResponseWriter, req *http.Request)
// }

View file

@ -1,13 +0,0 @@
package server
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Broker handles connections to the embedded message broker.
func Broker(c *gin.Context) {
broker := c.MustGet("broker").(http.Handler)
broker.ServeHTTP(c.Writer, c.Request)
}

View file

@ -1,62 +0,0 @@
package server
import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
func GetGlobalSecrets(c *gin.Context) {
secrets, err := store.GetGlobalSecretList(c)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var list []*model.TeamSecret
for _, s := range secrets {
list = append(list, s.Clone())
}
c.JSON(http.StatusOK, list)
}
func PostGlobalSecret(c *gin.Context) {
in := &model.TeamSecret{}
err := c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error())
return
}
in.ID = 0
err = store.SetGlobalSecret(c, in)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to persist global secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func DeleteGlobalSecret(c *gin.Context) {
name := c.Param("secret")
secret, err := store.GetGlobalSecret(c, name)
if err != nil {
c.String(http.StatusNotFound, "Cannot find secret %s.", name)
return
}
err = store.DeleteGlobalSecret(c, secret)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to delete global secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}

View file

@ -1,68 +0,0 @@
package server
import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
func GetSecrets(c *gin.Context) {
repo := session.Repo(c)
secrets, err := store.GetSecretList(c, repo)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var list []*model.RepoSecret
for _, s := range secrets {
list = append(list, s.Clone())
}
c.JSON(http.StatusOK, list)
}
func PostSecret(c *gin.Context) {
repo := session.Repo(c)
in := &model.RepoSecret{}
err := c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error())
return
}
in.ID = 0
in.RepoID = repo.ID
err = store.SetSecret(c, in)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to persist secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func DeleteSecret(c *gin.Context) {
repo := session.Repo(c)
name := c.Param("secret")
secret, err := store.GetSecret(c, repo, name)
if err != nil {
c.String(http.StatusNotFound, "Cannot find secret %s.", name)
return
}
err = store.DeleteSecret(c, secret)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to delete secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}

177
server/secret.go Normal file
View file

@ -0,0 +1,177 @@
package server
import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
func GetGlobalSecrets(c *gin.Context) {
secrets, err := store.GetGlobalSecretList(c)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var list []*model.TeamSecret
for _, s := range secrets {
list = append(list, s.Clone())
}
c.JSON(http.StatusOK, list)
}
func PostGlobalSecret(c *gin.Context) {
in := &model.TeamSecret{}
err := c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error())
return
}
in.ID = 0
err = store.SetGlobalSecret(c, in)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to persist global secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func DeleteGlobalSecret(c *gin.Context) {
name := c.Param("secret")
secret, err := store.GetGlobalSecret(c, name)
if err != nil {
c.String(http.StatusNotFound, "Cannot find secret %s.", name)
return
}
err = store.DeleteGlobalSecret(c, secret)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to delete global secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func GetSecrets(c *gin.Context) {
repo := session.Repo(c)
secrets, err := store.GetSecretList(c, repo)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var list []*model.RepoSecret
for _, s := range secrets {
list = append(list, s.Clone())
}
c.JSON(http.StatusOK, list)
}
func PostSecret(c *gin.Context) {
repo := session.Repo(c)
in := &model.RepoSecret{}
err := c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error())
return
}
in.ID = 0
in.RepoID = repo.ID
err = store.SetSecret(c, in)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to persist secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func DeleteSecret(c *gin.Context) {
repo := session.Repo(c)
name := c.Param("secret")
secret, err := store.GetSecret(c, repo, name)
if err != nil {
c.String(http.StatusNotFound, "Cannot find secret %s.", name)
return
}
err = store.DeleteSecret(c, secret)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to delete secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func GetTeamSecrets(c *gin.Context) {
team := c.Param("team")
secrets, err := store.GetTeamSecretList(c, team)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var list []*model.TeamSecret
for _, s := range secrets {
list = append(list, s.Clone())
}
c.JSON(http.StatusOK, list)
}
func PostTeamSecret(c *gin.Context) {
team := c.Param("team")
in := &model.TeamSecret{}
err := c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error())
return
}
in.ID = 0
in.Key = team
err = store.SetTeamSecret(c, in)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to persist team secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func DeleteTeamSecret(c *gin.Context) {
team := c.Param("team")
name := c.Param("secret")
secret, err := store.GetTeamSecret(c, team, name)
if err != nil {
c.String(http.StatusNotFound, "Cannot find secret %s.", name)
return
}
err = store.DeleteTeamSecret(c, secret)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to delete team secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}

View file

@ -1,113 +0,0 @@
package server
import (
"strings"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
const (
slashDeploy = "deploy"
slashRestart = "restart"
slashStatus = "status"
)
// Slack is handler function that handles Slack slash commands.
func Slack(c *gin.Context) {
command := c.Param("command")
text := c.PostForm("text")
args := strings.Split(text, " ")
if command == "" {
command = args[0]
args = args[1:]
}
switch command {
case slashStatus:
slackStatus(c, args)
case slashRestart:
slackRestart(c, args)
case slashDeploy:
slackDeploy(c, args)
default:
c.String(200, "sorry, I didn't understand [%s]", text)
}
}
func slackDeploy(c *gin.Context, args []string) {
if len(args) != 3 {
c.String(200, "Invalid command. Please provide [repo] [build number] [environment]")
return
}
var (
repo = args[0]
num = args[1]
env = args[2]
)
owner, name, _ := parseRepoBranch(repo)
c.String(200, "deploying build %s/%s#%s to %s", owner, name, num, env)
}
func slackRestart(c *gin.Context, args []string) {
var (
repo = args[0]
num = args[1]
)
owner, name, _ := parseRepoBranch(repo)
c.String(200, "restarting build %s/%s#%s", owner, name, num)
}
func slackStatus(c *gin.Context, args []string) {
var (
owner string
name string
branch string
)
if len(args) > 0 {
owner, name, branch = parseRepoBranch(args[0])
}
repo, err := store.GetRepoOwnerName(c, owner, name)
if err != nil {
c.String(200, "cannot find repository %s/%s", owner, name)
return
}
if branch == "" {
branch = repo.Branch
}
build, err := store.GetBuildLast(c, repo, branch)
if err != nil {
c.String(200, "cannot find status for %s/%s@%s", owner, name, branch)
return
}
c.String(200, "%s@%s build number %d finished with status %s",
repo.FullName,
build.Branch,
build.Number,
build.Status,
)
}
func parseRepoBranch(repo string) (owner, name, branch string) {
parts := strings.Split(repo, "@")
if len(parts) == 2 {
branch = parts[1]
repo = parts[0]
}
parts = strings.Split(repo, "/")
if len(parts) == 2 {
owner = parts[0]
name = parts[1]
}
return owner, name, branch
}

View file

@ -1,67 +0,0 @@
package server
import (
"net/http"
"github.com/drone/drone/model"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
func GetTeamSecrets(c *gin.Context) {
team := c.Param("team")
secrets, err := store.GetTeamSecretList(c, team)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var list []*model.TeamSecret
for _, s := range secrets {
list = append(list, s.Clone())
}
c.JSON(http.StatusOK, list)
}
func PostTeamSecret(c *gin.Context) {
team := c.Param("team")
in := &model.TeamSecret{}
err := c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Invalid JSON input. %s", err.Error())
return
}
in.ID = 0
in.Key = team
err = store.SetTeamSecret(c, in)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to persist team secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}
func DeleteTeamSecret(c *gin.Context) {
team := c.Param("team")
name := c.Param("secret")
secret, err := store.GetTeamSecret(c, team, name)
if err != nil {
c.String(http.StatusNotFound, "Cannot find secret %s.", name)
return
}
err = store.DeleteTeamSecret(c, secret)
if err != nil {
c.String(http.StatusInternalServerError, "Unable to delete team secret. %s", err.Error())
return
}
c.String(http.StatusOK, "")
}

View file

@ -24,6 +24,14 @@ func Load() *template.Template {
return tmpl
}
// Glob loads the templates matching the given pattern. This function
// will not compile if go generate is not executed before.
func Glob(pattern string) *template.Template {
return template.Must(
template.New("_").Funcs(template.FuncMap{"json": marshal}).ParseGlob(pattern),
)
}
// marshal is a helper function to render data as JSON inside the template.
func marshal(v interface{}) template.JS {
a, _ := json.Marshal(v)