Skip to content

Commit

Permalink
Start abstracting template stuff
Browse files Browse the repository at this point in the history
This is for #32.

This adds a starting framework for loading, caching, and executing
templates on the server and the client.  Currently only the server uses
this functionality.
  • Loading branch information
zorchenhimer committed Apr 13, 2019
1 parent d513fe8 commit 83477dc
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 22 deletions.
89 changes: 89 additions & 0 deletions common/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package common

import (
"fmt"
html "html/template"
"net/http"
"strings"
text "text/template"
)

// Holds the server's templates
var serverTemplates map[string]*html.Template

// Holds the client's chat templates
var chatTemplates map[string]*text.Template

var isServer bool = false

// keys and files to load for that template
var serverTemplateDefs map[string][]string = map[string][]string{
"pin": []string{"./static/base.html", "./static/thedoor.html"},
"main": []string{"./static/base.html", "./static/main.html"},
"help": []string{"./static/base.html", "./static/help.html"},
}

var chatTemplateDefs map[string]string = map[string]string{
fmt.Sprint(DTInvalid, 0): "wot",

fmt.Sprint(DTChat, MsgChat): `<span>{{.Badge}} <span class="name" style="color:{{.Color}}">{{.From}}` +
`</span><b>:</b> <span class="msg">{{.Message}}</span></span>`,
fmt.Sprint(DTChat, MsgAction): `<span style="color:{{.Color}}"><span class="name">{{.From}}` +
`</span> <span class="cmdme">{{.Message}}</span></span>`,
}

// Called from the server
func InitTemplates() error {
isServer = true
serverTemplates = make(map[string]*html.Template)
chatTemplates = make(map[string]*text.Template)

// Parse server templates
for key, files := range serverTemplateDefs {
t, err := html.ParseFiles(files...)
if err != nil {
return fmt.Errorf("Unable to parse templates for %s: %v", key, err)
}

serverTemplates[key] = t
}

// Parse client templates
//for key, def := range chatTemplateDefs {
// t := text.New(key)
// err, _ := t.Parse(def)
// if err != nil {
// return fmt.Errorf("Unabel to parse chat template %q: %v", key, err)
// }

// chatTemplates[key] = t
//}

return nil
}

// TODO
func LoadChatTemplates() error {
return nil
}

func ExecuteChatTemplate(typeA, typeB int, data interface{}) (string, error) {
key := fmt.Sprint(typeA, typeB)
t := chatTemplates[key]
builder := &strings.Builder{}

if err := t.Execute(builder, data); err != nil {
return "", err
}

return builder.String(), nil
}

func ExecuteServerTemplate(w http.ResponseWriter, key string, data interface{}) error {
t, ok := serverTemplates[key]
if !ok {
return fmt.Errorf("Template with the key %q does not exist", key)
}

return t.Execute(w, data)
}
25 changes: 3 additions & 22 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -223,12 +222,6 @@ func checkRoomAccess(w http.ResponseWriter, r *http.Request) bool {
}

func handlePinTemplate(w http.ResponseWriter, r *http.Request, errorMessage string) {
t, err := template.ParseFiles("./static/base.html", "./static/thedoor.html")
if err != nil {
common.LogErrorf("Error parsing template file: %v", err)
return
}

type Data struct {
Title string
SubmitText string
Expand All @@ -245,19 +238,13 @@ func handlePinTemplate(w http.ResponseWriter, r *http.Request, errorMessage stri
Notice: errorMessage,
}

err = t.Execute(w, data)
err := common.ExecuteServerTemplate(w, "pin", data)
if err != nil {
common.LogErrorf("Error executing file, %v", err)
}
}

func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./static/base.html", "./static/help.html")
if err != nil {
common.LogErrorf("Error parsing template file, %v\n", err)
return
}

type Data struct {
Title string
Commands map[string]string
Expand All @@ -278,7 +265,7 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
data.AdminCommands = getHelp(common.CmdlAdmin)
}

err = t.Execute(w, data)
err := common.ExecuteServerTemplate(w, "help", data)
if err != nil {
common.LogErrorf("Error executing file, %v", err)
}
Expand Down Expand Up @@ -314,12 +301,6 @@ func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
common.LogDebugln("Granted access")
}

t, err := template.ParseFiles("./static/base.html", "./static/main.html")
if err != nil {
common.LogErrorf("Error parsing template file, %v\n", err)
return
}

type Data struct {
Video, Chat bool
MessageHistoryCount int
Expand Down Expand Up @@ -347,7 +328,7 @@ func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, must-revalidate")
}

err = t.Execute(w, data)
err := common.ExecuteServerTemplate(w, "main", data)
if err != nil {
common.LogErrorf("Error executing file, %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func main() {
os.Exit(1)
}

if err := common.InitTemplates(); err != nil {
common.LogErrorln(err)
os.Exit(1)
}

exit := make(chan bool)
go handleInterrupt(exit)

Expand Down

0 comments on commit 83477dc

Please sign in to comment.