// Init my own customer database e.g. to ensure indexes.
func initCustomerDB() {
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameINIT, `Start init of customer database.`)
	defer Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameINIT, `Done init of customer database.`)

	// Get the database:
	dbSession, db := CustomerDB.DB()
	defer dbSession.Close()

	// Any issues with the database?
	if db == nil {
		Log.LogFull(senderName, LM.CategoryAPP, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)
		return
	}

	//
	// Get all collections:
	//

	// The collection for the answers:
	collAnswers := db.C(`Answers`)

	//
	// Ensure the indexes:
	//

	// Index for the text column:
	collAnswers.EnsureIndexKey(`Answer`)
}
// Register all of my own configuration parameters.
func registerAllAppConfigurationParameters() {

	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameSTARTUP, `Register now all app configuration parameters.`)
	defer Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameSTARTUP, `Register now all app configuration parameters done.`)

	// Add the parameter MyMotto with the default value "Keep calm :)".
	// You can change the value by using the admin's web interface on http://127.0.0.1:60000/
	ConfigurationDB.CheckSingleConfigurationPresentsAndAddIfMissing(`MyMotto`, `Keep calm :)`)
}
// Register all HTTP handlers for my app.
func registerHandlers() {
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameSTARTUP, `Register now all app handlers.`)
	defer Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameSTARTUP, `Register now all app handlers done.`)

	// Public Handlers: Public reachable
	Handlers.AddPublicHandler(`/`, Website.HandlerHome)
	Handlers.AddPublicHandler(`/version`, Website.HandlerVersion)
	Handlers.AddPublicHandler(`/question`, Website.HandlerShowQuestion)
	Handlers.AddPublicHandler(`/store`, Website.HandlerStore2Database)
	Handlers.AddPublicHandler(`/answers`, Website.HandlerAnswers)

	// Admin Handlers: Reachable only on the local device or by using e.g. SSH tunnels
	Handlers.AddAdminHandler(`/version`, Website.HandlerVersion)
	Handlers.AddAdminHandler(`/test`, Website.HandlerTEST)
}
// This HTTP handler provides a question by using a HTML template.
func HandlerShowQuestion(response http.ResponseWriter, request *http.Request) {
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameREQUEST, `Someone has requested the question handler.`, request.RemoteAddr)

	// Read the language of the client for I18N (see https://en.wikipedia.org/wiki/Internationalization_and_localization):
	lang := Tools.GetRequestLanguage(request)[0]

	// This is the data object for the HTML template:
	templateData := WebQuestion{}

	// I18N example: Uses the client the German language?
	if strings.Contains(lang.Language, `de`) {
		// Case: German
		templateData.IntroText = `Meine heutige Frage ist`
		templateData.Question = `Wie geht es dir?`
		templateData.PlaceholderText = `Deine Antwort:`
	} else {
		// Case: Default=English
		templateData.IntroText = `My today's question is`
		templateData.Question = `How are you?`
		templateData.PlaceholderText = `Your answer:`
	}

	// Let the client know, that we understand his language:
	Tools.SendChosenLanguage(response, lang)

	// Try to execute the template with the data.
	// The result gets sended to the client i.e. browser:
	Templates.ProcessHTML(`Question`, response, templateData)
}
// This HTTP handler stores the answers to the database.
func HandlerStore2Database(response http.ResponseWriter, request *http.Request) {
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameREQUEST, `Someone has requested the store handler.`, request.RemoteAddr)

	// Read the send answer from the client:
	answer := request.FormValue(`answer`)

	// Get the customer database:
	dbSession, db := CustomerDB.DB()

	// After we are done, close the connection:
	defer dbSession.Close()

	// Any issues?
	if db == nil {
		Log.LogFull(senderName, LM.CategoryAPP, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)
		return
	}

	// The collection for the answers:
	collAnswers := db.C(`Answers`)

	// Create the data object:
	answerDB := DBAnswers{}
	answerDB.Answer = answer

	// Insert the object in the database:
	collAnswers.Insert(answerDB)

	// Redirect the client to the answers page:
	defer http.Redirect(response, request, "/answers", 302)
}
// This HTTP handler prints the current motto and logs the request.
func HandlerHome(response http.ResponseWriter, request *http.Request) {
	// Log the request:
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameREQUEST, `Someone has requested the home.`, request.RemoteAddr)

	// Get the current configuration value for your own configuration parameter:
	// (You can change the value by using the admin's web interface on http://127.0.0.1:60000/)
	currentMotto := ConfigurationDB.Read(`MyMotto`)

	// Write the website:
	fmt.Fprintf(response, "Welcome to my website. The current motto i.e. slogan is: '%s'.\n", currentMotto)
}
// This HTTP handler provides all answers by using a HTML template.
func HandlerAnswers(response http.ResponseWriter, request *http.Request) {
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameREQUEST, `Someone has requested the answers.`, request.RemoteAddr)

	// Read the language of the client for I18N (see https://en.wikipedia.org/wiki/Internationalization_and_localization):
	lang := Tools.GetRequestLanguage(request)[0]

	// This is the data object for the HTML template:
	templateData := WebAnswers{}

	// Get the customer database:
	dbSession, db := CustomerDB.DB()

	// After we are done, close the connection:
	defer dbSession.Close()

	// Any issues?
	if db == nil {
		Log.LogFull(senderName, LM.CategoryAPP, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)
		return
	}

	// The collection for the answers:
	collAnswers := db.C(`Answers`)

	// Select the necessary documents in the database.
	// In this case, we want all documents!
	//
	// Syntax for database queries, see http://gopkg.in/mgo.v2
	//
	selection := bson.D{}

	if errFind := collAnswers.Find(selection).All(&templateData.Answers); errFind != nil {
		Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `Was not able to read all answers out of the database.`, `Error while find.`, errFind.Error())
	}

	// I18N example: Uses the client the German language?
	if strings.Contains(lang.Language, `de`) {
		// Case: German
		templateData.IntroText = `Hier siehst du alle Antworten.`
	} else {
		// Case: Default=English
		templateData.IntroText = `Here you see all answers.`
	}

	// Let the client know, that we understand his language:
	Tools.SendChosenLanguage(response, lang)

	// Try to execute the template with the data.
	// The result gets sended to the client i.e. browser:
	Templates.ProcessHTML(`Answers`, response, templateData)
}
Exemple #8
0
// This is the entry point of any Go application.
func main() {
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameSTARTUP, `MyApp is starting.`)

	// Init the system's handlers first:
	System.InitHandlers()

	// Init all my own app handlers:
	registerHandlers()

	// Register all my own configuration parameters to the configuration database:
	registerAllAppConfigurationParameters()

	// Init my customer database:
	initCustomerDB()

	// Start the server and wait until the end:
	System.StartAndBlockForever()
}
// This HTTP handler prints the current versions and logs the request.
func HandlerVersion(response http.ResponseWriter, request *http.Request) {
	Log.LogShort(senderName, LM.CategoryAPP, LM.LevelINFO, LM.MessageNameREQUEST, `Someone has requested the version.`, request.RemoteAddr)
	fmt.Fprintf(response, "%s\n", version)
}