// 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)
}
Ejemplo n.º 2
0
// 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)
}