// adminHandler function serves content. func adminHandler(w http.ResponseWriter, r *http.Request) { sess, _ := mod_sessions.GetSession(r, "sf") tpl, err := gwp_template.Load(M.ModCtx.Ctx, "admin.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } mydata := Content{ExampleData: sess.ID} buff := new(bytes.Buffer) tpl.Execute(buff, mydata) w.Write(buff.Bytes()) }
// indexPage() is a handler which will load some template and send the result back to the client func indexPage(writer http.ResponseWriter, req *http.Request) { tpl, err := gwp_template.Load(ctx, "index.html") if err != nil { http.Error(writer, err.Error(), http.StatusInternalServerError) return } var displayContent bool // if session parameter "session_id" is present, we already have session set and we can show the content // otherwise, we will display login form sess, ok := mod_sessions.CheckSession(req, writer, "session_id") if ok { displayContent = true } else { displayContent = false } var s_id string if sid, ok := sess.Values["session_id"]; ok { s_id = sid.(string) } else { s_id = sess.ID } errmsg := req.FormValue("error") var msg string if errmsg == "login" { msg = "Invalid login" } mydata := Example{ID: s_id, Name: "Joe", LoggedIn: displayContent, ErrorMsg: msg} buff := new(bytes.Buffer) tpl.Execute(buff, mydata) writer.Write(buff.Bytes()) }