Skip to content

Commit

Permalink
Merge pull request harness#2098 from bradrydzewski/master
Browse files Browse the repository at this point in the history
ability to serve website from disk
  • Loading branch information
bradrydzewski authored Jun 30, 2017
2 parents 7055966 + 7008e79 commit 8c91205
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 12 deletions.
20 changes: 8 additions & 12 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package router

import (
"net/http"
"os"

"github.com/gin-gonic/gin"

Expand All @@ -13,8 +12,6 @@ import (
"github.com/drone/drone/server/debug"
"github.com/drone/drone/server/metrics"
"github.com/drone/drone/server/template"

"github.com/drone/drone-ui/dist"
)

// Load loads the router
Expand All @@ -24,13 +21,11 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e.Use(gin.Recovery())
e.SetHTMLTemplate(template.T)

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)
ui := server.NewWebsite()
for _, path := range ui.Routes() {
e.GET(path, func(c *gin.Context) {
ui.File(c.Writer, c.Request)
})
} else {
e.Static("/static", dir)
}

e.Use(header.NoCache)
Expand All @@ -40,10 +35,11 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e.Use(session.SetUser())
e.Use(token.Refresh)

e.GET("/login", server.ShowLogin)
e.GET("/login/form", server.ShowLoginForm)
e.GET("/logout", server.GetLogout)
e.NoRoute(server.ShowIndex)
e.NoRoute(func(c *gin.Context) {
u := session.User(c)
ui.Page(c.Writer, c.Request, u)
})

user := e.Group("/api/user")
{
Expand Down
33 changes: 33 additions & 0 deletions server/template/files/index_polymer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="author" content="bradrydzewski">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title></title>
<script>
window.ENV = {};
window.ENV.server = window.location.protocol+"//"+window.location.host;
{{ if .csrf }}window.ENV.csrf = "{{ .csrf }}"{{ end }}
{{ if .user }}
window.USER = {{ json .user }};
{{ end }}
</script>
<script src="/bower_components/webcomponentsjs/webcomponents-loader.js"></script>

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono">
<link rel="import" href="/src/drone/drone-app.html">

<style>
html, body {
padding:0px;
margin:0px;
}
</style>
</head>
<body>
<drone-app></drone-app>
</body>
</html>
39 changes: 39 additions & 0 deletions server/template/template_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var files = []struct {
}, {
name: "index.html",
data: index,
}, {
name: "index_polymer.html",
data: indexpolymer,
}, {
name: "login.html",
data: login,
Expand Down Expand Up @@ -83,6 +86,42 @@ var index = `<!DOCTYPE html>
</html>
`

// files/index_polymer.html
var indexpolymer = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="author" content="bradrydzewski">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title></title>
<script>
window.ENV = {};
window.ENV.server = window.location.protocol+"//"+window.location.host;
{{ if .csrf }}window.ENV.csrf = "{{ .csrf }}"{{ end }}
{{ if .user }}
window.USER = {{ json .user }};
{{ end }}
</script>
<script src="/bower_components/webcomponentsjs/webcomponents-loader.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono">
<link rel="import" href="/src/drone/drone-app.html">
<style>
html, body {
padding:0px;
margin:0px;
}
</style>
</head>
<body>
<drone-app></drone-app>
</body>
</html>
`

// files/login.html
var login = `<!DOCTYPE html>
<html>
Expand Down
78 changes: 78 additions & 0 deletions server/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package server

import (
"net/http"
"os"

"github.com/drone/drone-ui/dist"
"github.com/drone/drone/model"
"github.com/drone/drone/server/template"
"github.com/drone/drone/shared/token"
)

// Website defines an interface to serve the user interface.
type Website interface {
Page(rw http.ResponseWriter, r *http.Request, u *model.User)
File(rw http.ResponseWriter, r *http.Request)
Routes() []string
}

type website struct {
fs http.Handler
}

// NewWebsite returns a new website loader.
func NewWebsite() Website {
path := os.Getenv("DRONE_WWW")
if path != "" {
return NewLocalWebsite(path)
}
return &website{
fs: http.FileServer(dist.AssetFS()),
}
}

// Page serves a page in the user interface.
func (w *website) Page(rw http.ResponseWriter, r *http.Request, u *model.User) {
rw.WriteHeader(200)

path := r.URL.Path
switch path {
case "/login/form":
params := map[string]interface{}{}
template.T.ExecuteTemplate(rw, "login.html", params)

case "/login":
if err := r.FormValue("error"); err != "" {
params := map[string]interface{}{"error": err}
template.T.ExecuteTemplate(rw, "error.html", params)
} else {
http.Redirect(rw, r, "/authorize", 303)
}

default:
var csrf string
if u != nil {
csrf, _ = token.New(
token.CsrfToken,
u.Login,
).Sign(u.Hash)
}
params := map[string]interface{}{
"user": u,
"csrf": csrf,
}
template.T.ExecuteTemplate(rw, "index.html", params)
}
}

// File serves a static file for the user interface.
func (w *website) File(rw http.ResponseWriter, r *http.Request) {
w.fs.ServeHTTP(rw, r)
}

func (w *website) Routes() []string {
return []string{
"/static/*filepath",
}
}
66 changes: 66 additions & 0 deletions server/ui_local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package server

import (
"net/http"

"github.com/drone/drone/model"
"github.com/drone/drone/server/template"
"github.com/drone/drone/shared/token"
)

type local struct {
dir string
fs http.Handler
}

// NewLocalWebsite returns a new website loader.
func NewLocalWebsite(path string) Website {
return &local{
dir: path,
fs: http.FileServer(
http.Dir(path),
),
}
}

// Page serves a page in the user interface.
func (w *local) Page(rw http.ResponseWriter, r *http.Request, u *model.User) {
rw.WriteHeader(200)

path := r.URL.Path
switch path {
case "/login":
if err := r.FormValue("error"); err != "" {
// TODO login error
} else {
http.Redirect(rw, r, "/authorize", 303)
}

default:
var csrf string
if u != nil {
csrf, _ = token.New(
token.CsrfToken,
u.Login,
).Sign(u.Hash)
}
params := map[string]interface{}{
"user": u,
"csrf": csrf,
}

template.T.ExecuteTemplate(rw, "index_polymer.html", params)
}
}

// File serves a static file for the user interface.
func (w *local) File(rw http.ResponseWriter, r *http.Request) {
w.fs.ServeHTTP(rw, r)
}

func (w *local) Routes() []string {
return []string{
"/src/*filepath",
"/bower_components/*filepath",
}
}

0 comments on commit 8c91205

Please sign in to comment.