Ocean/Templates/Init.go

82 lines
3.3 KiB
Go
Raw Normal View History

package Templates
import (
"archive/zip"
"bytes"
"fmt"
"github.com/SommerEngineering/Ocean/CustomerDB"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"github.com/SommerEngineering/Ocean/Tools"
"html/template"
"io/ioutil"
)
2015-06-17 15:44:52 +00:00
// Init the template package.
func init() {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the template engine.`)
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the template engine done.`)
2015-06-17 15:44:52 +00:00
// We read the templates out of the grid file system:
dbSession, gridFS := CustomerDB.GridFS()
defer dbSession.Close()
2015-06-17 15:44:52 +00:00
// Read the current version of the ZIP file:
if gridFile, errGridFile := gridFS.Open(`templates.zip`); errGridFile != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the templates out of the GridFS!`, errGridFile.Error())
return
} else {
2015-06-17 15:44:52 +00:00
// Read all data in the memory cache:
defer gridFile.Close()
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameCONFIGURATION, `Read the templates.zip file from the grid file system.`, `Upload time UTC: `+Tools.FormatTime(gridFile.UploadDate().UTC()))
if data, ioError := ioutil.ReadAll(gridFile); ioError != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to read the templates.`, ioError.Error())
return
} else {
zipData = data
}
}
2015-06-17 15:44:52 +00:00
// Opens the ZIP file from memory:
reader, readerError := zip.NewReader(bytes.NewReader(zipData), int64(len(zipData)))
if readerError != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameREAD, `Was not able to read the ZIP file.`, readerError.Error())
return
}
2015-06-17 15:44:52 +00:00
// The in-memory container for all templates:
templates = template.New(`root`)
2015-06-17 15:44:52 +00:00
// Loop over all files inside the ZIP file:
for _, file := range reader.File {
2015-06-17 15:44:52 +00:00
// Opens a file:
fileReader, openError := file.Open()
if openError == nil {
2015-06-17 15:44:52 +00:00
// Read all bytes:
contentData, readError := ioutil.ReadAll(fileReader)
fileReader.Close()
if readError != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameREAD, `Was not able to read the content of the desired template.`, readError.Error(), file.FileInfo().Name())
continue
}
2015-06-17 15:44:52 +00:00
// Converts the bytes to string:
templateData := string(contentData)
2015-06-17 15:44:52 +00:00
// Try to parse the string as template:
if _, err := templates.Parse(templateData); err != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityMiddle, LM.ImpactMiddle, LM.MessageNamePARSE, fmt.Sprintf(`The template '%s' cannot be parsed.`, file.FileInfo().Name()), err.Error())
} else {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameEXECUTE, fmt.Sprintf(`The template '%s' was parsed.`, file.FileInfo().Name()))
}
} else {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open a template.`, file.FileInfo().Name())
}
}
isInit = true
}