mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-27 15:07:43 +00:00
Add template function to lessen boilerplate for getting a route's URL.
This commit is contained in:
parent
c66ee02a55
commit
0804172e9e
3 changed files with 17 additions and 19 deletions
|
|
@ -69,6 +69,15 @@ func initGithubOAuthConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTemplates() {
|
func initTemplates() {
|
||||||
|
funcMap := template.FuncMap{
|
||||||
|
"routeUrl": func(name string) (string, error) {
|
||||||
|
url, err := router.Get(name).URL()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return url.String(), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
sharedFileNames, err := filepath.Glob("templates/shared/*.html")
|
sharedFileNames, err := filepath.Glob("templates/shared/*.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Could not read shared template file names %s", err.Error())
|
log.Panicf("Could not read shared template file names %s", err.Error())
|
||||||
|
|
@ -89,7 +98,8 @@ func initTemplates() {
|
||||||
}
|
}
|
||||||
fileNames = append(fileNames, templateFileName)
|
fileNames = append(fileNames, templateFileName)
|
||||||
fileNames = append(fileNames, sharedFileNames...)
|
fileNames = append(fileNames, sharedFileNames...)
|
||||||
templates[templateName], err = template.ParseFiles(fileNames...)
|
_, templateFileName = filepath.Split(fileNames[0])
|
||||||
|
templates[templateName], err = template.New(templateFileName).Funcs(funcMap).ParseFiles(fileNames...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Could not parse template files for %s: %s", templateFileName, err.Error())
|
log.Panicf("Could not parse template files for %s: %s", templateFileName, err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -112,11 +122,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
session, _ := sessionStore.Get(r, sessionConfig.CookieName)
|
session, _ := sessionStore.Get(r, sessionConfig.CookieName)
|
||||||
userId, ok := session.Values[sessionConfig.UserIdKey].(int)
|
userId, ok := session.Values[sessionConfig.UserIdKey].(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
signInUrl, _ := router.Get("sign-in").URL()
|
if err := templates["index-signed-out"].Execute(w, nil); err != nil {
|
||||||
var data = map[string]string{
|
|
||||||
"SignInUrl": signInUrl.String(),
|
|
||||||
}
|
|
||||||
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
|
||||||
|
|
@ -134,15 +140,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
signOutUrl, _ := router.Get("sign-out").URL()
|
if err := templates["index"].Execute(w, nil); err != nil {
|
||||||
viewDigestUrl, _ := router.Get("view-digest").URL()
|
|
||||||
sendDigestUrl, _ := router.Get("send-digest").URL()
|
|
||||||
var data = map[string]interface{}{
|
|
||||||
"SignOutUrl": signOutUrl.String(),
|
|
||||||
"ViewDigestUrl": viewDigestUrl.String(),
|
|
||||||
"SendDigestUrl": sendDigestUrl.String(),
|
|
||||||
}
|
|
||||||
if err := templates["index"].Execute(w, data); err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
{{define "body"}}
|
{{define "body"}}
|
||||||
|
|
||||||
<a href="{{.SignInUrl}}">
|
<a href="{{routeUrl "sign-in"}}">
|
||||||
Sign In
|
Sign In
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,14 @@
|
||||||
{{define "body"}}
|
{{define "body"}}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<a href="{{.SignOutUrl}}">
|
<a href="{{routeUrl "sign-out"}}">
|
||||||
Sign Out
|
Sign Out
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<a href="{{.ViewDigestUrl}}">View Digest</a>
|
<a href="{{routeUrl "view-digest"}}">View Digest</a>
|
||||||
-
|
-
|
||||||
<form id="send-form" method="POST" action="{{.SendDigestUrl}}">
|
<form id="send-form" method="POST" action="{{routeUrl "send-digest"}}">
|
||||||
<input type="submit" value="Email Digest">
|
<input type="submit" value="Email Digest">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue