Add documentation
This commit is contained in:
1 parent
69d0bdabf4
commit
6dab89a1d4
144 files changed
+1107
-549
No files matched your search
@@ -9,7 +9,10 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// Try to read a static file.
|
||||
func FindAndReadFile(filename string) (result []byte) {
|
||||
|
||||
// Case: The system goes down.
|
||||
if Shutdown.IsDown() {
|
||||
return
|
||||
}
|
||||
@@ -24,13 +27,17 @@ func FindAndReadFile(filename string) (result []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
// Loop over all files inside the ZIP file:
|
||||
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 the content:
|
||||
contentData, readError := ioutil.ReadAll(fileReader)
|
||||
|
||||
if readError != nil {
|
||||
|
||||
+10
-1
@@ -10,13 +10,16 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Handler to deliver static files.
|
||||
func HandlerStaticFiles(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
// Case: The system goes down.
|
||||
if Shutdown.IsDown() {
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare the path:
|
||||
// Prepare the path by removing the prefix:
|
||||
path := strings.Replace(request.RequestURI, `/staticFiles/`, ``, 1)
|
||||
path = strings.Replace(path, `%20`, ` `, -1)
|
||||
fileType := ``
|
||||
@@ -30,24 +33,30 @@ func HandlerStaticFiles(response http.ResponseWriter, request *http.Request) {
|
||||
fileType = mimeType.MimeType
|
||||
}
|
||||
|
||||
// Case: Was not able to determine the file's type.
|
||||
if fileType == `` {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityCritical, LM.ImpactUnknown, LM.MessageNameNOTFOUND, `The mime type is unknown.`, path)
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
}
|
||||
|
||||
// Read the file's content:
|
||||
contentData := FindAndReadFile(path)
|
||||
|
||||
// Case: Was not able to read the file.
|
||||
if contentData == nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `The desired file was not found.`, path)
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
}
|
||||
|
||||
// Send the meta data and the content to the client:
|
||||
fileLenText := fmt.Sprintf(`%d`, len(contentData))
|
||||
response.Header().Add(`Content-Length`, fileLenText)
|
||||
response.Header().Add(`Content-Type`, fileType)
|
||||
response.Write(contentData)
|
||||
|
||||
// Log all static files accesses?
|
||||
if logStaticFileRequests {
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameBROWSER, `A static file was requested.`, path)
|
||||
}
|
||||
|
||||
+4
-1
@@ -8,15 +8,18 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// The init of the static file package.
|
||||
func init() {
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting now the static files component.`)
|
||||
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the static files component done.`)
|
||||
|
||||
// If the static files are disabled, stop here:
|
||||
if ConfigurationDB.Read(`EnableStaticFiles`) != `true` {
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Static files are disabled.`)
|
||||
return
|
||||
}
|
||||
|
||||
// Case: Static files are enabled.
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Static files are enabled.`)
|
||||
|
||||
// Read the configuration:
|
||||
@@ -27,7 +30,7 @@ func init() {
|
||||
|
||||
logStaticFileRequests = ConfigurationDB.Read(`LogStaticFileRequests`) == `true`
|
||||
|
||||
// Read the static files' data from GridFS:
|
||||
// Read the static files' data from GridFS and keep it in-memory:
|
||||
dbSession, gridFS := CustomerDB.GridFS()
|
||||
defer dbSession.Close()
|
||||
|
||||
|
||||
@@ -5,7 +5,10 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Handler to map the static files to the root. Use it for static web sites.
|
||||
func HandlerMapStaticFiles2Root(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
// Case: The system goes down.
|
||||
if Shutdown.IsDown() {
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
senderName LM.Sender = `System::StaticFiles`
|
||||
startFile4Map2Root string = `index.html`
|
||||
logStaticFileRequests bool = false
|
||||
zipData []byte = nil
|
||||
senderName LM.Sender = `System::StaticFiles` // This is the name for logging event from this package
|
||||
startFile4Map2Root string = `index.html` // The default filename in case of mapping static files to the root
|
||||
logStaticFileRequests bool = false // Logging each access?
|
||||
zipData []byte = nil // The in-memory cache of the data
|
||||
)
|
||||
Reference in new issue
Block a user