forked from gboddin/drone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request harness#2098 from bradrydzewski/master
ability to serve website from disk
- Loading branch information
Showing
5 changed files
with
224 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |