Initial commit of Ocean's local development
This commit is contained in:
1 parent
9944a9e1df
commit
86451938ec
116 files changed
+3320
No files matched your search
@@ -0,0 +1,47 @@
|
||||
package StaticFiles
|
||||
|
||||
import "io/ioutil"
|
||||
import "bytes"
|
||||
import "archive/zip"
|
||||
import "github.com/SommerEngineering/Ocean/Shutdown"
|
||||
import "github.com/SommerEngineering/Ocean/Log"
|
||||
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
func FindAndReadFile(filename string) (result []byte) {
|
||||
if Shutdown.IsDown() {
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare the path:
|
||||
path := filename
|
||||
|
||||
// Read the content from the ZIP file:
|
||||
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
|
||||
}
|
||||
|
||||
for _, file := range reader.File {
|
||||
if file.Name == path {
|
||||
|
||||
fileReader, openError := file.Open()
|
||||
defer fileReader.Close()
|
||||
|
||||
if openError == nil {
|
||||
contentData, readError := ioutil.ReadAll(fileReader)
|
||||
|
||||
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 file.`, readError.Error(), path)
|
||||
return
|
||||
}
|
||||
|
||||
result = contentData
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameNOTFOUND, `The desired file is not part of the ZIP file.`, `Do you use an old version?`, path)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package StaticFiles
|
||||
|
||||
import "fmt"
|
||||
import "strings"
|
||||
import "net/http"
|
||||
import "github.com/SommerEngineering/Ocean/Shutdown"
|
||||
import "github.com/SommerEngineering/Ocean/MimeTypes"
|
||||
import "github.com/SommerEngineering/Ocean/Log"
|
||||
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
func HandlerStaticFiles(response http.ResponseWriter, request *http.Request) {
|
||||
if Shutdown.IsDown() {
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare the path:
|
||||
path := strings.Replace(request.RequestURI, `/staticFiles/`, ``, 1)
|
||||
path = strings.Replace(path, `%20`, ` `, -1)
|
||||
fileType := ``
|
||||
|
||||
// Determine the MIME type:
|
||||
if mimeType, errMime := MimeTypes.DetectType(path); errMime != nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityMiddle, LM.ImpactMiddle, LM.MessageNameNOTFOUND, `Was not able to detect the MIME type of the font.`, errMime.Error(), path)
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
} else {
|
||||
fileType = mimeType.MimeType
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
contentData := FindAndReadFile(path)
|
||||
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
|
||||
}
|
||||
|
||||
fileLenText := fmt.Sprintf(`%d`, len(contentData))
|
||||
response.Header().Add(`Content-Length`, fileLenText)
|
||||
response.Header().Add(`Content-Type`, fileType)
|
||||
response.Write(contentData)
|
||||
|
||||
if logStaticFileRequests {
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameBROWSER, `A static file was requested.`, path)
|
||||
}
|
||||
return
|
||||
|
||||
http.NotFound(response, request)
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameNOTFOUND, `The desired file is not part of the ZIP file.`, `Do you use an old version?`, path)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package StaticFiles
|
||||
|
||||
import "io/ioutil"
|
||||
import "github.com/SommerEngineering/Ocean/CustomerDB"
|
||||
import "github.com/SommerEngineering/Ocean/ConfigurationDB"
|
||||
import "github.com/SommerEngineering/Ocean/Log"
|
||||
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
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 ConfigurationDB.Read(`EnableStaticFiles`) != `true` {
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Static files are disabled.`)
|
||||
return
|
||||
}
|
||||
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Static files are enabled.`)
|
||||
|
||||
// Read the configuration:
|
||||
if ConfigurationDB.Read(`MapStaticFiles2Root`) == `true` {
|
||||
startFile4Map2Root = ConfigurationDB.Read(`MapStaticFiles2RootRootFile`)
|
||||
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `The desired root document was set.`, `rootDocument=`+startFile4Map2Root)
|
||||
}
|
||||
|
||||
logStaticFileRequests = ConfigurationDB.Read(`LogStaticFileRequests`) == `true`
|
||||
|
||||
// Read the static files' data from GridFS:
|
||||
gridFS := CustomerDB.GridFS()
|
||||
if gridFile, errGridFile := gridFS.Open(`staticFiles.zip`); errGridFile != nil {
|
||||
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the static files out of the GridFS!`, errGridFile.Error())
|
||||
return
|
||||
} else {
|
||||
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 static files.`, ioError.Error())
|
||||
return
|
||||
} else {
|
||||
zipData = data
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package StaticFiles
|
||||
|
||||
import "net/http"
|
||||
import "github.com/SommerEngineering/Ocean/Shutdown"
|
||||
|
||||
func HandlerMapStaticFiles2Root(response http.ResponseWriter, request *http.Request) {
|
||||
if Shutdown.IsDown() {
|
||||
http.NotFound(response, request)
|
||||
return
|
||||
}
|
||||
|
||||
if request.RequestURI == `/` {
|
||||
request.RequestURI = `/staticFiles/` + startFile4Map2Root
|
||||
} else {
|
||||
request.RequestURI = `/staticFiles` + request.RequestURI
|
||||
}
|
||||
|
||||
HandlerStaticFiles(response, request)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package StaticFiles
|
||||
|
||||
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
|
||||
|
||||
var (
|
||||
senderName LM.Sender = `System::StaticFiles`
|
||||
startFile4Map2Root string = `index.html`
|
||||
logStaticFileRequests bool = false
|
||||
zipData []byte = nil
|
||||
)
|
||||
Reference in new issue
Block a user