Initial commit of Ocean's local development

This commit is contained in:
Thorsten Sommer committed 2014-04-26 11:18:56 +02:00
1 parent 9944a9e1df
commit 86451938ec
116 files changed
+3320

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
package ConfigurationDB
import "labix.org/v2/mgo/bson"
import "github.com/SommerEngineering/Ocean/Log"
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
func checkConfiguration() {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Check now the configuration database.`)
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Done checking the configuration database.`)
CheckSingleConfigurationPresentsAndAddIfMissing(`InternalCommPassword`, `please replace this with e.g. a random GUID, etc.`)
CheckSingleConfigurationPresentsAndAddIfMissing(`CustomerDBHost`, `localhost:27017`)
CheckSingleConfigurationPresentsAndAddIfMissing(`CustomerDBDatabase`, `Ocean`)
CheckSingleConfigurationPresentsAndAddIfMissing(`CustomerDBUsername`, `root`)
CheckSingleConfigurationPresentsAndAddIfMissing(`CustomerDBPassword`, `please replace this with a good password`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDBHost`, `localhost:27017`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDBDatabase`, `Ocean`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDBUsername`, `root`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDBPassword`, `please replace this with a good password`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDBCacheSizeNumberOfEvents`, `50`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDBCacheSizeTime2FlushSeconds`, `6`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogBufferSize`, `500`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDeviceDelayNumberEvents`, `600`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogDeviceDelayTime2FlushSeconds`, `5`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogTimeoutSeconds`, `4`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogStaticFileRequests`, `false`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogUseDatabaseLogging`, `false`)
CheckSingleConfigurationPresentsAndAddIfMissing(`LogUseConsoleLogging`, `true`)
CheckSingleConfigurationPresentsAndAddIfMissing(`NumGenActiveHosts`, `please replace this with the correct hostname of the host which is the master number generator`)
CheckSingleConfigurationPresentsAndAddIfMissing(`NumGenGetHandler`, `http://localhost:80/next/number`)
CheckSingleConfigurationPresentsAndAddIfMissing(`NumGenBufferSize`, `12`)
CheckSingleConfigurationPresentsAndAddIfMissing(`OceanHostnameAndPort`, `:60000`)
CheckSingleConfigurationPresentsAndAddIfMissing(`OceanUtilizeCPUs`, `2`)
CheckSingleConfigurationPresentsAndAddIfMissing(`FilenameWebResources`, `web.zip`)
CheckSingleConfigurationPresentsAndAddIfMissing(`MapStaticFiles2Root`, `false`)
CheckSingleConfigurationPresentsAndAddIfMissing(`MapStaticFiles2RootRootFile`, `index.html`)
CheckSingleConfigurationPresentsAndAddIfMissing(`EnableStaticFiles`, `true`)
CheckSingleConfigurationPresentsAndAddIfMissing(`robots.txt`, `User-agent: *
Disallow:`)
}
/*
Use this function to ensure that the database contains at least a default value for the configuration.
*/
func CheckSingleConfigurationPresentsAndAddIfMissing(name, value string) {
if !checkSingleConfigurationPresents(name) {
addSingleConfiguration(name, value)
}
}
func checkSingleConfigurationPresents(name string) (result bool) {
selection := bson.D{{"Name", name}}
count, _ := collection.Find(selection).Count()
return count > 0
}
func addSingleConfiguration(name, value string) {
entry := ConfigurationDBEntry{}
entry.Name = name
entry.Value = value
collection.Insert(entry)
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Add a missing configuration to the configuration database.`, `Name=`+name, `Value=`+value)
}
+10
View File
@@ -0,0 +1,10 @@
/*
This package provides the configuration database access: Ocean uses MongoDB as database! The configuration is represented as
name value pairs. Both, the name and the value, are strings. If you need numbers for the configuration, you have to convert
these strings in to numbers afterwards. Use the Read() function to read a specific configuration value.
To provide own application configurations, use the function CheckSingleConfigurationPresentsAndAddIfMissing() to ensure, that
at least a default value is present in the database. Collect all these function calls inside a init() function somewhere at your
application.
*/
package ConfigurationDB
+38
View File
@@ -0,0 +1,38 @@
package ConfigurationDB
import "labix.org/v2/mgo"
import "github.com/SommerEngineering/Ocean/Configuration"
import "github.com/SommerEngineering/Ocean/Log"
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
func init() {
config := Configuration.Read()
// Connect to MongoDB:
if newSession, errDial := mgo.Dial(config.ConfigDBHostname); errDial != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+config.ConfigDBHostname, errDial.Error())
return
} else {
session = newSession
}
// Use the correct database:
db = session.DB(config.ConfigDBDatabase)
// Login:
if errLogin := db.Login(config.ConfigDBConfigurationCollectionUsername, config.ConfigDBConfigurationCollectionPassword); errLogin != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelSECURITY, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to login the user `+config.ConfigDBConfigurationCollectionUsername, errLogin.Error())
return
}
// Get the collection:
collection = db.C(config.ConfigDBConfigurationCollection)
// Take care about the index:
collection.EnsureIndexKey(`Name`)
collection.EnsureIndexKey(`Value`)
checkConfiguration()
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `The configuration database is now ready.`)
}
+24
View File
@@ -0,0 +1,24 @@
package ConfigurationDB
import "github.com/SommerEngineering/Ocean/Log"
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
import "labix.org/v2/mgo/bson"
/*
This function reads the current configuration value.
*/
func Read(name string) (value string) {
if name == `` {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `Was not able to read a configuration out of the database.`, `The given name was nil!`)
return
}
result := ConfigurationDBEntry{}
if errFind := collection.Find(bson.D{{"Name", name}}).One(&result); errFind != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `Was not able to read a configuration out of the database.`, `Error while find.`, errFind.Error())
return
}
value = result.Value
return
}
+6
View File
@@ -0,0 +1,6 @@
package ConfigurationDB
type ConfigurationDBEntry struct {
Name string `bson:"Name"`
Value string `bson:"Value"`
}
+19
View File
@@ -0,0 +1,19 @@
package ConfigurationDB
import "github.com/SommerEngineering/Ocean/Log"
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
/*
Do not use this type by your own! It is a Ocean internal type to provide a shutdown function for the configuration database.
*/
type ShutdownFunction struct {
}
/*
If the Ocean server is shutting down, this function is called to close the database.
*/
func (a ShutdownFunction) Shutdown() {
Log.LogShort(senderName, LM.CategoryAPP, LM.LevelWARN, LM.MessageNameSHUTDOWN, `Close now the configuration database connection.`)
db.Logout()
session.Close()
}
+13
View File
@@ -0,0 +1,13 @@
package ConfigurationDB
import "labix.org/v2/mgo"
import "github.com/SommerEngineering/Ocean/Configuration/Meta"
import LM "github.com/SommerEngineering/Ocean/Log/Meta"
var (
session *mgo.Session = nil
db *mgo.Database = nil
collection *mgo.Collection = nil
config Meta.Configuration = Meta.Configuration{}
senderName LM.Sender = `System::ConfigurationDB`
)