// 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) }
// 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`) }
// 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) }