func renderError(w http.ResponseWriter, err error) { renderTemplate(w, ERROR_TEMPLATE, errorView{ err.Error(), err.Error(), util.IsDev(), }) }
func renderParseError(w http.ResponseWriter, err error) bool { if err != nil { renderTemplate(w, ERROR_TEMPLATE, errorView{ "Could not parse integer", err.Error(), util.IsDev(), }) return true } return false }
func main() { fmt.Printf("Starting on port %s...\n", *port) if util.IsDev() { fmt.Println("Started in DEVELOPMENT mode.") } fmt.Println("Press Ctrl-C to exit!") fmt.Println() initialize() // start main loop for the application go mainLoop() http.Handle("/public/", http.FileServer(http.Dir("."))) log.Fatal(http.ListenAndServe(*port, nil)) }
// Writes the tempalte to the response. If we are running in a development mode, // it re-reads the template from the file every time. Otherwise it uses the // cached version. func renderTemplate(w http.ResponseWriter, tmplName string, data interface{}) { if util.IsDev() { tmpl := template.New(getFullTempalteName(tmplName)) tmpl.Funcs(utilFuncMap()) //tmpl, err := template.ParseFiles("template/" + getFullTempalteName(tmplName)) tmpl, err := tmpl.ParseFiles("template/" + getFullTempalteName(tmplName)) if err != nil { fmt.Fprintf(w, "Error in template:\n%s", err.Error()) } else { tmpl.Execute(w, data) } return } templates.ExecuteTemplate(w, getFullTempalteName(tmplName), data) }
// join handler func joinHandler(rw http.ResponseWriter, req *http.Request) { if !canJoin(req) { renderError(rw, util.Errorf("You are already playing! You cannot join twice.")) return } player := engine.NewPlayer(req.FormValue("username")) player.IsBot = covertCheckboxValueToBool(req.FormValue("is_robot")) err := player.Join(currentGame) if err != nil { util.LogWarn("Player could not join. Cause: %s", err.Error()) renderTemplate(rw, ERROR_TEMPLATE, errorView{ // TODO: move to properties file "Játékos nem tudott csatlakozni.", err.Error(), util.IsDev(), }) } else { http.SetCookie(rw, &http.Cookie{Name: PLAYER_ID_COOKIE, Value: player.Id, HttpOnly: true}) http.Redirect(rw, req, "/shoot", http.StatusFound) } }