Browse Source

Added functionality to persist template bundle items to disk

Wirecog 6 years ago
parent
commit
7b8558b184
3 changed files with 98 additions and 5 deletions
  1. 4
    0
      static.go
  2. 6
    4
      template.go
  3. 88
    1
      templateset.go

+ 4
- 0
static.go View File

@@ -101,6 +101,10 @@ func bundleCSS(cssfiles []string) {
101 101
 
102 102
 func BundleStaticAssets() {
103 103
 
104
+	if UseStaticTemplateBundleFile == true {
105
+		return
106
+	}
107
+
104 108
 	jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
105 109
 	bundleJavaScript(jsfiles)
106 110
 	cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)

+ 6
- 4
template.go View File

@@ -23,10 +23,12 @@ const (
23 23
 )
24 24
 
25 25
 var (
26
-	PrefixNamePartial     = "partials/"
27
-	PrefixNameLayout      = "layouts/"
28
-	TemplateFileExtension = ".tmpl"
29
-	TemplateFilesPath     = "./templates"
26
+	PrefixNamePartial            = "partials/"
27
+	PrefixNameLayout             = "layouts/"
28
+	TemplateFileExtension        = ".tmpl"
29
+	TemplateFilesPath            = "./templates"
30
+	UseStaticTemplateBundleFile  = false
31
+	StaticTemplateBundleFilePath = ""
30 32
 )
31 33
 
32 34
 type Template struct {

+ 88
- 1
templateset.go View File

@@ -6,9 +6,14 @@
6 6
 package isokit
7 7
 
8 8
 import (
9
+	"bytes"
10
+	"encoding/gob"
11
+	"errors"
9 12
 	"html/template"
10 13
 	"io/ioutil"
11 14
 	"log"
15
+	"os"
16
+	"path/filepath"
12 17
 	"strings"
13 18
 )
14 19
 
@@ -109,8 +114,73 @@ func (t *TemplateSet) Render(templateName string, params *RenderParams) {
109 114
 
110 115
 }
111 116
 
117
+func (t *TemplateSet) PersistTemplateBundleToDisk() error {
118
+
119
+	dirPath := filepath.Dir(StaticTemplateBundleFilePath)
120
+	if _, err := os.Stat(dirPath); os.IsNotExist(err) {
121
+
122
+		return errors.New("The specified directory for the StaticTemplateBundleFilePath, " + dirPath + ", does not exist!")
123
+
124
+	} else {
125
+
126
+		var templateContentItemsBuffer bytes.Buffer
127
+		enc := gob.NewEncoder(&templateContentItemsBuffer)
128
+		m := t.bundle.Items()
129
+		err := enc.Encode(&m)
130
+		if err != nil {
131
+			return err
132
+		}
133
+		err = ioutil.WriteFile(StaticTemplateBundleFilePath, templateContentItemsBuffer.Bytes(), 0644)
134
+		if err != nil {
135
+			return err
136
+		} else {
137
+			return nil
138
+		}
139
+
140
+	}
141
+
142
+}
143
+
144
+func (t *TemplateSet) RestoreTemplateBundleFromDisk() error {
145
+
146
+	if _, err := os.Stat(StaticTemplateBundleFilePath); os.IsNotExist(err) {
147
+		return errors.New("The StaticTemplateBundleFilePath, " + StaticTemplateBundleFilePath + ", does not exist")
148
+	} else {
149
+
150
+		data, err := ioutil.ReadFile(StaticTemplateBundleFilePath)
151
+		if err != nil {
152
+			return err
153
+		}
154
+
155
+		var templateBundleMap map[string]string
156
+		var templateBundleMapBuffer bytes.Buffer
157
+		dec := gob.NewDecoder(&templateBundleMapBuffer)
158
+		templateBundleMapBuffer = *bytes.NewBuffer(data)
159
+		err = dec.Decode(&templateBundleMap)
160
+
161
+		if err != nil {
162
+			return err
163
+		}
164
+
165
+		t.ImportTemplatesFromMap(templateBundleMap)
166
+		bundle := &TemplateBundle{items: templateBundleMap}
167
+		t.bundle = bundle
168
+
169
+		return nil
170
+	}
171
+}
172
+
112 173
 func (t *TemplateSet) GatherTemplates() {
113 174
 
175
+	if UseStaticTemplateBundleFile == true {
176
+		err := t.RestoreTemplateBundleFromDisk()
177
+		if err != nil {
178
+			log.Println("Failed to restore template bundle from disk, opting for standard operating procedure instead. Error: ", err)
179
+		} else {
180
+			return
181
+		}
182
+	}
183
+
114 184
 	bundle := NewTemplateBundle()
115 185
 
116 186
 	templatesPath := t.TemplateFilesPath
@@ -121,10 +191,21 @@ func (t *TemplateSet) GatherTemplates() {
121 191
 	t.ImportTemplatesFromMap(bundle.Items())
122 192
 	t.bundle = bundle
123 193
 
194
+	if StaticTemplateBundleFilePath != "" {
195
+		err := t.PersistTemplateBundleToDisk()
196
+		if err != nil {
197
+			log.Println("Failed to persist the template bundle to disk, in GatherTemplates, with error: ", err)
198
+		}
199
+	}
200
+
124 201
 }
125 202
 
126 203
 func (t *TemplateSet) GatherCogTemplates(cogTemplatePath string, prefixName string, templateFileExtension string) {
127 204
 
205
+	if UseStaticTemplateBundleFile == true {
206
+		return
207
+	}
208
+
128 209
 	bundle := NewTemplateBundle()
129 210
 
130 211
 	templatesPath := cogTemplatePath
@@ -134,6 +215,12 @@ func (t *TemplateSet) GatherCogTemplates(cogTemplatePath string, prefixName stri
134 215
 	for k, v := range bundle.Items() {
135 216
 		t.bundle.items[k] = v
136 217
 	}
137
-	//	t.bundle = bundle
218
+
219
+	if StaticTemplateBundleFilePath != "" {
220
+		err := t.PersistTemplateBundleToDisk()
221
+		if err != nil {
222
+			log.Println("Failed to persist the template bundle to disk, in GatherCogTemplates, with error: ", err)
223
+		}
224
+	}
138 225
 
139 226
 }