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
 
101
 
102
 func BundleStaticAssets() {
102
 func BundleStaticAssets() {
103
 
103
 
104
+	if UseStaticTemplateBundleFile == true {
105
+		return
106
+	}
107
+
104
 	jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
108
 	jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
105
 	bundleJavaScript(jsfiles)
109
 	bundleJavaScript(jsfiles)
106
 	cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)
110
 	cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)

+ 6
- 4
template.go View File

23
 )
23
 )
24
 
24
 
25
 var (
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
 type Template struct {
34
 type Template struct {

+ 88
- 1
templateset.go View File

6
 package isokit
6
 package isokit
7
 
7
 
8
 import (
8
 import (
9
+	"bytes"
10
+	"encoding/gob"
11
+	"errors"
9
 	"html/template"
12
 	"html/template"
10
 	"io/ioutil"
13
 	"io/ioutil"
11
 	"log"
14
 	"log"
15
+	"os"
16
+	"path/filepath"
12
 	"strings"
17
 	"strings"
13
 )
18
 )
14
 
19
 
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
 func (t *TemplateSet) GatherTemplates() {
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
 	bundle := NewTemplateBundle()
184
 	bundle := NewTemplateBundle()
115
 
185
 
116
 	templatesPath := t.TemplateFilesPath
186
 	templatesPath := t.TemplateFilesPath
121
 	t.ImportTemplatesFromMap(bundle.Items())
191
 	t.ImportTemplatesFromMap(bundle.Items())
122
 	t.bundle = bundle
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
 func (t *TemplateSet) GatherCogTemplates(cogTemplatePath string, prefixName string, templateFileExtension string) {
203
 func (t *TemplateSet) GatherCogTemplates(cogTemplatePath string, prefixName string, templateFileExtension string) {
127
 
204
 
205
+	if UseStaticTemplateBundleFile == true {
206
+		return
207
+	}
208
+
128
 	bundle := NewTemplateBundle()
209
 	bundle := NewTemplateBundle()
129
 
210
 
130
 	templatesPath := cogTemplatePath
211
 	templatesPath := cogTemplatePath
134
 	for k, v := range bundle.Items() {
215
 	for k, v := range bundle.Items() {
135
 		t.bundle.items[k] = v
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
 }