Add documentation

This commit is contained in:
Thorsten Sommer committed 2015-06-17 17:44:52 +02:00
1 parent 69d0bdabf4
commit 6dab89a1d4
144 files changed
+1107 -549

No files matched your search

+14 -1
View File
@@ -12,17 +12,21 @@ import (
"io/ioutil"
)
// 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.`)
// We read the templates out of the grid file system:
dbSession, gridFS := CustomerDB.GridFS()
defer dbSession.Close()
// 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 {
// 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 {
@@ -33,18 +37,24 @@ func init() {
}
}
// Read the content from the ZIP file:
// 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
}
// The in-memory container for all templates:
templates = template.New(`root`)
// Loop over all files inside the ZIP file:
for _, file := range reader.File {
// Opens a file:
fileReader, openError := file.Open()
if openError == nil {
// Read all bytes:
contentData, readError := ioutil.ReadAll(fileReader)
fileReader.Close()
@@ -53,7 +63,10 @@ func init() {
continue
}
// Converts the bytes to string:
templateData := string(contentData)
// 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 {
+5
View File
@@ -7,14 +7,19 @@ import (
"net/http"
)
// Executes a HTML template by name.
func ProcessHTML(templateName string, response http.ResponseWriter, data interface{}) {
// Case: A request before the init event is done.
if !isInit {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameINIT, `The template engine is not (yet) init.`)
return
}
// Send first the MIME type:
MimeTypes.Write2HTTP(response, MimeTypes.TypeWebHTML)
// Execute the template:
if executeError := templates.ExecuteTemplate(response, templateName, data); executeError != nil {
Log.LogFull(senderName, LM.CategoryAPP, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameEXECUTE, `Was not able to execute the template.`, templateName, executeError.Error())
}
+4 -4
View File
@@ -6,8 +6,8 @@ import (
)
var (
templates *template.Template = nil
senderName LM.Sender = `System::Templates`
zipData []byte = nil
isInit bool = false
templates *template.Template = nil // The in-memory cache for the templates
senderName LM.Sender = `System::Templates` // This is the name for logging event from this package
zipData []byte = nil // The in-memory cache of the ZIP file
isInit bool = false // State, if the init event is done
)