Browse Source

Provided facility for template set bundle to be sent in serverless environments, where the ability to write to disk is not available.

Wirecog 5 years ago
parent
commit
781d8122d6
3 changed files with 92 additions and 0 deletions
  1. 24
    0
      serversidehandlers.go
  2. 1
    0
      template.go
  3. 67
    0
      templateset.go

+ 24
- 0
serversidehandlers.go View File

@@ -0,0 +1,24 @@
1
+// +build !clientonly
2
+
3
+package isokit
4
+
5
+import (
6
+	"bytes"
7
+	"encoding/gob"
8
+	"log"
9
+	"net/http"
10
+)
11
+
12
+func TemplateBundleHandler(ts *TemplateSet) http.Handler {
13
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14
+		var templateContentItemsBuffer bytes.Buffer
15
+		enc := gob.NewEncoder(&templateContentItemsBuffer)
16
+		m := ts.Bundle().Items()
17
+		err := enc.Encode(&m)
18
+		if err != nil {
19
+			log.Print("encoding err: ", err)
20
+		}
21
+		w.Header().Set("Content-Type", "application/octet-stream")
22
+		w.Write(templateContentItemsBuffer.Bytes())
23
+	})
24
+}

+ 1
- 0
template.go View File

@@ -28,6 +28,7 @@ var (
28 28
 	TemplateFileExtension        = ".tmpl"
29 29
 	TemplateFilesPath            = "./templates"
30 30
 	UseStaticTemplateBundleFile  = false
31
+	UseTemplateBundleFromBuffer  = false
31 32
 	StaticTemplateBundleFilePath = ""
32 33
 	ShouldBundleStaticAssets     = true
33 34
 )

+ 67
- 0
templateset.go View File

@@ -24,6 +24,7 @@ type TemplateSet struct {
24 24
 	Funcs             template.FuncMap
25 25
 	bundle            *TemplateBundle
26 26
 	TemplateFilesPath string
27
+	bundleBuffer      bytes.Buffer
27 28
 }
28 29
 
29 30
 func NewTemplateSet() *TemplateSet {
@@ -143,6 +144,27 @@ func (t *TemplateSet) PersistTemplateBundleToDisk() error {
143 144
 
144 145
 }
145 146
 
147
+func (t *TemplateSet) PersistTemplateBundleToTemplateSetBuffer() error {
148
+
149
+	dirPath := filepath.Dir(StaticTemplateBundleFilePath)
150
+	if _, err := os.Stat(dirPath); os.IsNotExist(err) {
151
+
152
+		return errors.New("The specified directory for the StaticTemplateBundleFilePath, " + dirPath + ", does not exist!")
153
+
154
+	} else {
155
+
156
+		enc := gob.NewEncoder(&t.bundleBuffer)
157
+		m := t.bundle.Items()
158
+		err := enc.Encode(&m)
159
+		if err != nil {
160
+			return err
161
+		} else {
162
+			return nil
163
+		}
164
+	}
165
+
166
+}
167
+
146 168
 func (t *TemplateSet) RestoreTemplateBundleFromDisk() error {
147 169
 
148 170
 	if _, err := os.Stat(StaticTemplateBundleFilePath); os.IsNotExist(err) {
@@ -172,6 +194,35 @@ func (t *TemplateSet) RestoreTemplateBundleFromDisk() error {
172 194
 	}
173 195
 }
174 196
 
197
+func (t *TemplateSet) RestoreTemplateBundleFromTemplateSetBuffer() error {
198
+
199
+	if _, err := os.Stat(StaticTemplateBundleFilePath); os.IsNotExist(err) {
200
+		return errors.New("The StaticTemplateBundleFilePath, " + StaticTemplateBundleFilePath + ", does not exist")
201
+	} else {
202
+
203
+		data := t.bundleBuffer.Bytes()
204
+		if err != nil {
205
+			return err
206
+		}
207
+
208
+		var templateBundleMap map[string]string
209
+		var templateBundleMapBuffer bytes.Buffer
210
+		dec := gob.NewDecoder(&templateBundleMapBuffer)
211
+		templateBundleMapBuffer = *bytes.NewBuffer(data)
212
+		err = dec.Decode(&templateBundleMap)
213
+
214
+		if err != nil {
215
+			return err
216
+		}
217
+
218
+		t.ImportTemplatesFromMap(templateBundleMap)
219
+		bundle := &TemplateBundle{items: templateBundleMap}
220
+		t.bundle = bundle
221
+
222
+		return nil
223
+	}
224
+}
225
+
175 226
 func (t *TemplateSet) GatherTemplates() {
176 227
 
177 228
 	if UseStaticTemplateBundleFile == true {
@@ -183,6 +234,15 @@ func (t *TemplateSet) GatherTemplates() {
183 234
 		}
184 235
 	}
185 236
 
237
+	if UseTemplateBundleFromBuffer == true && UseStaticTemplateBundleFile == false {
238
+		err := t.RestoreTemplateBundleFromTemplateSetBuffer()
239
+		if err != nil {
240
+			log.Println("Didn't find a template bundle from the template set buffer, will generate a new template bundle.")
241
+		} else {
242
+			return
243
+		}
244
+	}
245
+
186 246
 	bundle := NewTemplateBundle()
187 247
 
188 248
 	templatesPath := t.TemplateFilesPath
@@ -200,6 +260,13 @@ func (t *TemplateSet) GatherTemplates() {
200 260
 		}
201 261
 	}
202 262
 
263
+	if UseTemplateBundleFromBuffer == true && StaticTemplateBundleFilePath == "" {
264
+		err := t.PersistTemplateBundleToTemplateSetBuffer()
265
+		if err != nil {
266
+			log.Println("Failed to persist the template bundle to disk, in GatherTemplates, with error: ", err)
267
+		}
268
+	}
269
+
203 270
 }
204 271
 
205 272
 func (t *TemplateSet) GatherCogTemplates(cogTemplatePath string, prefixName string, templateFileExtension string) {