Add documentation
This commit is contained in:
1 parent
69d0bdabf4
commit
6dab89a1d4
144 files changed
+1107
-549
No files matched your search
@@ -9,8 +9,10 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// Get the file bytes for any web content file.
|
||||
func GetContent(path string) (content []byte, err error) {
|
||||
|
||||
// Open the ZIP file reader with the data out of the memory:
|
||||
reader, readerError := zip.NewReader(bytes.NewReader(zipData), int64(len(zipData)))
|
||||
if readerError != nil {
|
||||
err = errors.New("Was not able to read the ZIP file: " + readerError.Error())
|
||||
@@ -18,13 +20,18 @@ func GetContent(path string) (content []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Iterate over all files within the ZIP:
|
||||
for _, file := range reader.File {
|
||||
|
||||
// Is this the desired file?
|
||||
if file.Name == path {
|
||||
|
||||
// Open the file:
|
||||
fileReader, openError := file.Open()
|
||||
defer fileReader.Close()
|
||||
|
||||
if openError == nil {
|
||||
|
||||
// Read all bytes:
|
||||
contentData, readError := ioutil.ReadAll(fileReader)
|
||||
|
||||
if readError != nil {
|
||||
@@ -33,6 +40,7 @@ func GetContent(path string) (content []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Return the data:
|
||||
content = contentData
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,12 +8,16 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The handler to deliver web framework files e.g. jQuery, Bootstrap, etc.
|
||||
func HandlerDeliverFramework(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
// If the system is going down, send an error:
|
||||
if Shutdown.IsDown() {
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
}
|
||||
|
||||
// Replace the prefix:
|
||||
path := strings.Replace(request.URL.Path, "/framework/", "", 1)
|
||||
sendError := SendContent(response, path)
|
||||
|
||||
|
||||
@@ -8,24 +8,32 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
/*
|
||||
Init the web content package. It is used to provided some static data for web
|
||||
frameworks like e.g. jQuery, d3.js, Bootstrap, etc.
|
||||
*/
|
||||
func init() {
|
||||
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Run init for the web content.`)
|
||||
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Init for web content done.`)
|
||||
|
||||
// Ensure that we init this package only once:
|
||||
if isInit {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityHigh, LM.ImpactNone, LM.MessageNameINIT, `The web content is already fine.`)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the filename out of the database:
|
||||
filename = ConfigurationDB.Read(`FilenameWebResources`)
|
||||
dbSession, gridFS := CustomerDB.GridFS()
|
||||
defer dbSession.Close()
|
||||
|
||||
// Open the file out of the grid file system:
|
||||
if gridFile, errGridFile := gridFS.Open(filename); errGridFile != nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the web content out of the GridFS!`, filename, errGridFile.Error())
|
||||
return
|
||||
} else {
|
||||
// Read all the data in the memeory cache:
|
||||
defer gridFile.Close()
|
||||
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 web content file.`, filename, ioError.Error())
|
||||
|
||||
@@ -10,8 +10,10 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Send any web content file to the client.
|
||||
func SendContent(response http.ResponseWriter, path string) (err error) {
|
||||
|
||||
// Read the bytes:
|
||||
content, contError := GetContent(path)
|
||||
if contError != nil {
|
||||
err = errors.New("Was not able to read the needed content: " + contError.Error())
|
||||
@@ -19,6 +21,7 @@ func SendContent(response http.ResponseWriter, path string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Determine some meta data:
|
||||
contentLength := len(content)
|
||||
contentType, typeError := MimeTypes.DetectType(path)
|
||||
|
||||
@@ -28,10 +31,12 @@ func SendContent(response http.ResponseWriter, path string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Send the meta data to the client:
|
||||
response.Header().Add("Content-Length", fmt.Sprintf("%d", contentLength))
|
||||
response.Header().Add("Content-Type", contentType.MimeType)
|
||||
response.WriteHeader(http.StatusOK)
|
||||
|
||||
// Write the binary data to the client:
|
||||
buffer := bytes.NewBuffer(content)
|
||||
_, writeError := buffer.WriteTo(response)
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
isInit = false
|
||||
filename = ""
|
||||
zipData []byte = nil
|
||||
senderName LM.Sender = `System::WebContent`
|
||||
isInit = false // Ensure that the init happens only once
|
||||
filename = "" // The filename of web content file at the grid file system
|
||||
zipData []byte = nil // The memory cache of the data
|
||||
senderName LM.Sender = `System::WebContent` // This is the name for logging event from this package
|
||||
)
|
||||
Reference in new issue
Block a user