mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-27 15:07:43 +00:00
Restructure templates to have a base "page" template with common markup.
This commit is contained in:
parent
35a6407490
commit
0ecaeef5e3
7 changed files with 99 additions and 79 deletions
|
|
@ -9,7 +9,9 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"appengine"
|
"appengine"
|
||||||
"appengine/mail"
|
"appengine/mail"
|
||||||
|
|
@ -25,6 +27,26 @@ var router *mux.Router
|
||||||
var githubOauthConfig oauth.Config
|
var githubOauthConfig oauth.Config
|
||||||
var sessionStore *sessions.CookieStore
|
var sessionStore *sessions.CookieStore
|
||||||
var sessionConfig SessionConfig
|
var sessionConfig SessionConfig
|
||||||
|
var templates map[string]*template.Template
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
initTemplates()
|
||||||
|
sessionStore, sessionConfig = initSession()
|
||||||
|
initGithubOAuthConfig()
|
||||||
|
|
||||||
|
router = mux.NewRouter()
|
||||||
|
router.HandleFunc("/", indexHandler).Name("index")
|
||||||
|
|
||||||
|
router.HandleFunc("/session/sign-in", signInHandler).Name("sign-in")
|
||||||
|
router.HandleFunc("/session/sign-out", signOutHandler).Name("sign-out")
|
||||||
|
router.HandleFunc("/github/callback", githubOAuthCallbackHandler)
|
||||||
|
|
||||||
|
router.HandleFunc("/digest/send", sendDigestHandler).Name("send-digest").Methods("POST")
|
||||||
|
router.HandleFunc("/digest/cron", digestCronHandler)
|
||||||
|
|
||||||
|
router.HandleFunc("/admin/digest", digestAdminHandler)
|
||||||
|
http.Handle("/", router)
|
||||||
|
}
|
||||||
|
|
||||||
func initGithubOAuthConfig() {
|
func initGithubOAuthConfig() {
|
||||||
path := "config/github-oauth"
|
path := "config/github-oauth"
|
||||||
|
|
@ -45,25 +67,33 @@ func initGithubOAuthConfig() {
|
||||||
githubOauthConfig.TokenURL = "https://github.com/login/oauth/access_token"
|
githubOauthConfig.TokenURL = "https://github.com/login/oauth/access_token"
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func initTemplates() {
|
||||||
sessionStore, sessionConfig = initSession()
|
sharedFileNames, err := filepath.Glob("templates/shared/*.html")
|
||||||
initGithubOAuthConfig()
|
if err != nil {
|
||||||
|
log.Panicf("Could not read shared template file names %s", err.Error())
|
||||||
router = mux.NewRouter()
|
}
|
||||||
router.HandleFunc("/", indexHandler).Name("index")
|
templateFileNames, err := filepath.Glob("templates/*.html")
|
||||||
|
if err != nil {
|
||||||
router.HandleFunc("/session/sign-in", signInHandler).Name("sign-in")
|
log.Panicf("Could not read template file names %s", err.Error())
|
||||||
router.HandleFunc("/session/sign-out", signOutHandler).Name("sign-out")
|
}
|
||||||
router.HandleFunc("/github/callback", githubOAuthCallbackHandler)
|
templates = make(map[string]*template.Template)
|
||||||
|
for _, templateFileName := range templateFileNames {
|
||||||
router.HandleFunc("/digest/send", sendDigestHandler).Name("send-digest").Methods("POST")
|
templateName := filepath.Base(templateFileName)
|
||||||
router.HandleFunc("/digest/cron", digestCronHandler)
|
templateName = strings.TrimSuffix(templateName, filepath.Ext(templateName))
|
||||||
|
fileNames := make([]string, 0, len(sharedFileNames)+2)
|
||||||
router.HandleFunc("/admin/digest", digestAdminHandler)
|
// The base template has to come first, except for the email template, which
|
||||||
http.Handle("/", router)
|
// doesn't use it
|
||||||
|
if templateName != "digest-email" {
|
||||||
|
fileNames = append(fileNames, "templates/base/page.html")
|
||||||
|
}
|
||||||
|
fileNames = append(fileNames, templateFileName)
|
||||||
|
fileNames = append(fileNames, sharedFileNames...)
|
||||||
|
templates[templateName], err = template.ParseFiles(fileNames...)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Could not parse template files for %s: %s", templateFileName, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var templates = template.Must(template.ParseGlob("templates/*.html"))
|
|
||||||
|
|
||||||
func signInHandler(w http.ResponseWriter, r *http.Request) {
|
func signInHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, githubOauthConfig.AuthCodeURL(""), http.StatusFound)
|
http.Redirect(w, r, githubOauthConfig.AuthCodeURL(""), http.StatusFound)
|
||||||
|
|
@ -85,7 +115,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var data = map[string]string{
|
var data = map[string]string{
|
||||||
"SignInUrl": signInUrl.String(),
|
"SignInUrl": signInUrl.String(),
|
||||||
}
|
}
|
||||||
if err := templates.ExecuteTemplate(w, "index-signed-out", data); err != nil {
|
if err := templates["index-signed-out"].Execute(w, data); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -118,7 +148,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
"SendDigestUrl": sendDigestUrl.String(),
|
"SendDigestUrl": sendDigestUrl.String(),
|
||||||
"Digest": digest,
|
"Digest": digest,
|
||||||
}
|
}
|
||||||
if err := templates.ExecuteTemplate(w, "index", data); err != nil {
|
if err := templates["index"].Execute(w, data); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -174,14 +204,14 @@ func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var data = map[string]interface{}{
|
||||||
|
"Styles": getDigestStyles(),
|
||||||
|
"Digest": digest,
|
||||||
|
}
|
||||||
var digestHtml bytes.Buffer
|
var digestHtml bytes.Buffer
|
||||||
digestHtml.WriteString("<html><head><style>")
|
if err := templates["digest-email"].Execute(&digestHtml, data); err != nil {
|
||||||
digestHtml.Write(getDigestStyles())
|
|
||||||
digestHtml.WriteString("</style></head><body>")
|
|
||||||
if err := templates.ExecuteTemplate(&digestHtml, "digest", digest); err != nil {
|
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
digestHtml.WriteString("</body></html>")
|
|
||||||
|
|
||||||
emails, _, err := githubClient.Users.ListEmails(nil)
|
emails, _, err := githubClient.Users.ListEmails(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -209,12 +239,12 @@ func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) {
|
||||||
return true, err
|
return true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDigestStyles() []byte {
|
func getDigestStyles() template.CSS {
|
||||||
b, err := ioutil.ReadFile("static/digest.css")
|
b, err := ioutil.ReadFile("static/digest.css")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Could not read digest CSS: %s", err.Error())
|
log.Panicf("Could not read digest CSS: %s", err.Error())
|
||||||
}
|
}
|
||||||
return b
|
return template.CSS(string(b[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func githubOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
|
func githubOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -280,7 +310,7 @@ func digestAdminHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var data = map[string]interface{}{
|
var data = map[string]interface{}{
|
||||||
"Digest": digest,
|
"Digest": digest,
|
||||||
}
|
}
|
||||||
if err := templates.ExecuteTemplate(w, "digest-admin", data); err != nil {
|
if err := templates["digest-admin"].Execute(w, data); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
app/templates/base/page.html
Normal file
14
app/templates/base/page.html
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>{{template "title" .}}</title>
|
||||||
|
<link rel="stylesheet" href="/static/main.css">
|
||||||
|
<link rel="stylesheet" href="/static/digest.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>{{template "title" .}}</h1>
|
||||||
|
|
||||||
|
{{template "body" .}}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -1,18 +1,7 @@
|
||||||
{{define "digest-admin"}}
|
{{define "title"}}GitHop Admin{{end}}
|
||||||
|
|
||||||
<!DOCTYPE html>
|
{{define "body"}}
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<title>GitHop Admin</title>
|
|
||||||
<link rel="stylesheet" href="/static/main.css">
|
|
||||||
<link rel="stylesheet" href="/static/digest.css">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>GitHop Admin</h1>
|
|
||||||
|
|
||||||
{{template "digest" .Digest}}
|
{{template "digest" .Digest}}
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
8
app/templates/digest-email.html
Normal file
8
app/templates/digest-email.html
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style>{{.Styles}}</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{{template "digest" .Digest}}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -1,19 +1,9 @@
|
||||||
{{define "index-signed-out"}}
|
{{define "title"}}GitHop{{end}}
|
||||||
|
|
||||||
<!DOCTYPE html>
|
{{define "body"}}
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<title>GitHop</title>
|
|
||||||
<link rel="stylesheet" href="/static/main.css">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>GitHop!</h1>
|
|
||||||
|
|
||||||
<a href="{{.SignInUrl}}">
|
<a href="{{.SignInUrl}}">
|
||||||
Sign In
|
Sign In
|
||||||
</a>
|
</a>
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,6 @@
|
||||||
{{define "index"}}
|
{{define "title"}}GitHop{{end}}
|
||||||
|
|
||||||
<!DOCTYPE html>
|
{{define "body"}}
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<title>GitHop</title>
|
|
||||||
<link rel="stylesheet" href="/static/main.css">
|
|
||||||
<link rel="stylesheet" href="/static/digest.css">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>GitHop!</h1>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<a href="{{.SignOutUrl}}">
|
<a href="{{.SignOutUrl}}">
|
||||||
|
|
@ -22,7 +13,5 @@
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{{template "digest" .Digest}}
|
{{template "digest" .Digest}}
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue