func handleShared(w http.ResponseWriter, r *http.Request) { t0 := time.Now() var sid string u.PathVars(r, "/share/", &sid) user, err := auth.GetUserByShared(sid) if err != nil { glog.Errorf("handleShared - auth.GetUserByShared(%s): %s", sid, err) return } cml := make(map[string][]game.Game) gl := game.UserGames(user) sort.Sort(game.GameName(gl)) for _, g := range gl { cml[g.ConsoleName] = append(cml[g.ConsoleName], g) } cl, err := game.GetConsoles(user) var sm []game.ConsoleMeta for _, c := range cl { if len(cml[c.Name]) > 0 { var cm game.ConsoleMeta cm.Console = c cm.Games = cml[c.Name] sm = append(sm, cm) } } sort.Sort(game.ConsoleMetaName(sm)) sharedtmpl.ExecuteTemplate(w, "shared", sm) fmt.Printf("handleShared %v\n", time.Now().Sub(t0)) }
func handleCategory(w http.ResponseWriter, r *http.Request) { t0 := time.Now() var loggedin bool loggedin, userName = auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } var id string var todo string var val string u.PathVars(r, "/category/", &id, &todo, &val) switch todo { case "new": var c feed.Category c.Name = val c.Insert(userName) fmt.Fprintf(w, "Added") case "name": c := feed.GetCat(id) c.Name = val c.Save() fmt.Fprintf(w, id+"Renamed: "+val) case "desc": c := feed.GetCat(id) c.Description = val c.Save() fmt.Fprintf(w, "Desc: "+val) case "delete": c := feed.GetCat(id) c.Delete() fmt.Fprintf(w, "Deleted") case "update": c := feed.GetCat(id) c.Update() fmt.Fprintf(w, "Updated") case "unread": c := feed.GetCat(id) print("in unread\n") fmt.Fprintf(w, strconv.Itoa(c.Unread())) case "exclude": c := feed.GetCat(id) c.Exclude = val c.Save() fmt.Fprintf(w, "Exclude:"+c.Exclude) case "print": c := feed.GetCat(id) if err := categoryPrintHtml.Execute(w, c); err != nil { glog.Errorf("categoryPrintHtml.Execute: %s", err) } case "clearcache": c := feed.GetCat(id) c.ClearCache() case "deleteexcludes": c := feed.GetCat(id) c.DeleteExcludes() } fmt.Printf("handleCategory %v\n", time.Now().Sub(t0)) }
//print the list of entries for the selected category, feed, or marked func handleEntries(w http.ResponseWriter, r *http.Request) { t0 := time.Now() var loggedin bool var feedOrCat, id, mode, curID, modifier string loggedin, userName = auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } // format is /entries/{feed|category|marked}/<id>/{read|unread|marked|next|previous}[/{feed_id|cat_id}] u.PathVars(r, "/entries/", &feedOrCat, &id, &mode, &curID, &modifier) var el []feed.Entry switch feedOrCat { case "feed": f, err := feed.GetFeed(u.Toint(id)) if err != nil { glog.Errorf("feed.GetFeed(%s): %s", id, err) } switch mode { case "read": el = f.ReadEntries() case "marked": el = f.MarkedEntries() case "all": el = f.AllEntries() case "search": el = f.SearchTitles(curID, modifier) default: el = f.UnreadEntries() } case "category": c := feed.GetCat(id) switch mode { case "read": el = c.ReadEntries() case "marked": el = c.MarkedEntries() case "all": el = c.AllEntries() case "search": el = c.SearchTitles(curID, modifier) default: el = c.UnreadEntries() } case "marked": el = feed.AllMarkedEntries(userName) } if len(el) == 0 { fmt.Fprintf(w, "No entries found") } if err := entriesListTmpl.Execute(w, &el); err != nil { glog.Errorf("entriesListTmpl.Execute: %s", err) } t1 := time.Now() fmt.Printf("handleEntries %v\n", t1.Sub(t0)) }
func handleStats(w http.ResponseWriter, r *http.Request) { var t0 = time.Now() var todo string u.PathVars(r, "/stats/", &todo) var c string switch todo { case "entries": c, _ = feed.GetEntriesCount() } fmt.Fprintf(w, c) fmt.Printf("handleStats %v\n", time.Now().Sub(t0)) }
func handleLogin(w http.ResponseWriter, r *http.Request) { t0 := time.Now() fmt.Printf("HandleLogin()") var lt string u.PathVars(r, "/login/", <) fmt.Printf("lt: %s", lt) err := auth.LoginToken(w, r, lt) if err != nil { glog.Infof("%s", err) } fmt.Printf("handleLogin %v\n", time.Now().Sub(t0)) }
//print the list of categories (possibly with feeds in that cat), then the uncategorized feeds func handleCategoryList(w http.ResponseWriter, r *http.Request) { t0 := time.Now() loggedin, userName := auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } var currentCat string type CategoryList struct { SelectedCat int CategoryList []feed.Category SelectedFeeds []feed.Feed OrphanFeeds []feed.Feed } u.PathVars(r, "/categoryList/", ¤tCat) fmt.Fprintf(w, "<ul class='feedList' id='feedList'>\n") allthecats := feed.GetCategories(userName) for _, cat := range allthecats { //print the feeds under the currently selected category if strconv.Itoa(cat.ID) == currentCat { if err := categoryHtmlS.Execute(w, cat); err != nil { glog.Errorf("categoryHtmlS.Execute: %s", err) } fmt.Fprintf(w, "<br>\n") catFeeds := cat.Feeds() for j := range catFeeds { if err := feedHtmlSpaced.Execute(w, catFeeds[j]); err != nil { glog.Errorf("feedHtmlSpaced.Execute: %s", err) } } } else { if err := categoryHtml.Execute(w, cat); err != nil { glog.Errorf("categoryHtml.Execute: %s", err) } fmt.Fprintf(w, "<br>\n") } } fmt.Fprintf(w, "<hr>") allFeeds := feed.GetFeedsWithoutCats(userName) for i := range allFeeds { if err := feedHtml.Execute(w, allFeeds[i]); err != nil { glog.Errorf("feedHtml.Execute: %s", err) } } t1 := time.Now() fmt.Printf("handleCategoryList %v\n", t1.Sub(t0)) }
func handleSelectMenu(w http.ResponseWriter, r *http.Request) { t0 := time.Now() var loggedin bool loggedin, userName = auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } var id string u.PathVars(r, "/menu/select/", &id) f, err := feed.GetFeed(u.Toint(id)) if err != nil { glog.Errorf("feed.GetFeed(%s): %s", id, err) } setSelects(&f) if err := menuDropHtml.Execute(w, f); err != nil { glog.Errorf("menuDropHtml.Execute: %s", err) } fmt.Printf("handleSelectMenu %v\n", time.Now().Sub(t0)) }
func handleEntry(w http.ResponseWriter, r *http.Request) { t0 := time.Now() var loggedin bool loggedin, userName = auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } var id string u.PathVars(r, "/entry/", &id) e := feed.GetEntry(id, userName) f, err := feed.GetFeed(e.FeedID) if err != nil { glog.Errorf("feed.GetFeed(%s): %s", e.FeedID, err) } e.FeedName = f.Title switch e.ViewMode() { case "link": e.Link = html.UnescapeString(e.Link) if err := entryLinkHtml.Execute(w, &e); err != nil { glog.Errorf("entryLinkHtml: %s", err) } case "proxy": e.Content, err = e.ProxyLink() if err := entryHtml.Execute(w, e); err != nil { glog.Errorf("entryHtml.Execute: %s", err) } default: if err := entryHtml.Execute(w, &e); err != nil { glog.Errorf("entryHtml.Execute: %s", err) } } print("About to markentry\n") if err := e.MarkRead(); err != nil { glog.Errorf("e.MarkRead(): %s", err) } fmt.Printf("handleEntry %v\n", time.Now().Sub(t0)) }
func handleMenu(w http.ResponseWriter, r *http.Request) { t0 := time.Now() var loggedin bool loggedin, userName = auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } var feedOrCat string var id string var mode string var curID string var modifier string u.PathVars(r, "/menu/", &feedOrCat, &id, &mode, &curID, &modifier) switch feedOrCat { case "category": cat := feed.GetCat(id) cat.SearchSelect = getSearchSelect(modifier) cat.Search = curID if err := catMenuHtml.Execute(w, cat); err != nil { glog.Errorf("catMenuHtml: %s", err) } case "feed": f, err := feed.GetFeed(u.Toint(id)) if err != nil { glog.Errorf("feed.GetFeed(%s): %s", id, err) } f.SearchSelect = getSearchSelect(modifier) f.Search = curID setSelects(&f) if err := feedMenuHtml.Execute(w, f); err != nil { glog.Errorf("feedMenuHtml.Execute: %s", err) } case "marked": fmt.Fprintf(w, " ") } fmt.Printf("handleMenu %v\n", time.Now().Sub(t0)) }
func handleMarkEntry(w http.ResponseWriter, r *http.Request) { t0 := time.Now() var loggedin bool loggedin, userName = auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } var feedOrCat string var fcid string var retstr string var id string var tomark string u.PathVars(r, "/entry/mark/", &feedOrCat, &fcid, &id, &tomark) fmt.Printf("feedOrCat: %s, fcid: %s, id: %s, tomark: %s", feedOrCat, fcid, id, tomark) b := strings.Split(id, ",") // one thing can be marked whatever, but a list can only be marked read if len(b) == 1 { e := feed.GetEntry(id, userName) switch tomark { case "read": if err := e.MarkRead(); err != nil { glog.Errorf("e.MarkRead(): %s", err) } case "unread": if err := e.MarkUnread(); err != nil { glog.Errorf("e.MarkUnread(): %s", err) } case "marked": if err := e.Mark(); err != nil { glog.Errorf("e.Mark(): %s", err) } retstr = "<img src='static/mark_set.png' alt='Set mark' onclick='javascript:toggleMark(" + id + ");'>\n" case "unmarked": if err := e.UnMark(); err != nil { glog.Errorf("e.UnMark(): %s", err) } retstr = "<img src='static/mark_unset.png' alt='Set mark' onclick='javascript:toggleMark(" + id + ");'>\n" case "togglemarked": rsv, err := e.ToggleMark() if err != nil { glog.Errorf("e.ToggleMarked(): %s", err) } retstr = "<img src='static/mark_" + rsv + ".png' alt='Set mark' onclick='javascript:toggleMark(" + id + ");'>\n" } } else { switch feedOrCat { case "feed": f, err := feed.GetFeed(u.Toint(fcid)) if err != nil { glog.Errorf("getFeed(%s): %s", fcid, err) } err = f.MarkEntriesRead(b) if err != nil { glog.Errorf("f.MarkEntriesread(): %s", err) } case "category": c := feed.GetCat(fcid) err := c.MarkEntriesRead(b) if err != nil { glog.Errorf("c.MarkEntriesRead(): %s", err) } } } fmt.Fprintf(w, retstr) t1 := time.Now() fmt.Printf("handleMarkEntry %v\n", t1.Sub(t0)) }
func handleFeed(w http.ResponseWriter, r *http.Request) { var t0 = time.Now() var loggedin bool loggedin, userName = auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } var id string var todo string var val string u.PathVars(r, "/feed/", &id, &todo, &val) f, err := feed.GetFeed(u.Toint(id)) if err != nil { glog.Errorf("feed.GetFeed(%s): %s", id, err) } if f.UserName != userName { fmt.Fprintf(w, "Auth err") return } switch todo { case "name": f.Title = val f.Save() fmt.Fprintf(w, "Name: "+val) case "link": url := r.FormValue("url") f.Url = url f.Save() fmt.Fprintf(w, f.Url) case "expirey": f.Expirey = val f.Save() fmt.Fprintf(w, "Expirey: "+val) case "autoscroll": f.AutoscrollPX = u.Toint(val) f.Save() fmt.Fprintf(w, "Autoscroll: "+val) case "exclude": f.Exclude = html.EscapeString(val) f.Save() fmt.Fprintf(w, "Exclude saved") case "excludedata": f.ExcludeData = html.EscapeString(val) f.Save() fmt.Fprintf(w, "Exclude Data Saved") case "category": f.CategoryID = u.Toint(val) f.Save() fmt.Fprintf(w, "Category: "+f.Category().Name) case "view_mode": f.ViewMode = val f.Save() fmt.Fprintf(w, "View Mode: "+val) case "delete": f.Delete() fmt.Fprintf(w, "Deleted") case "update": err = f.Update() if err != nil { glog.Errorf("f.Update() (url: %s): %s", f.Url, err) fmt.Fprintf(w, "Error updating") } else { fmt.Fprintf(w, "Updated") } case "unread": fmt.Fprintf(w, strconv.Itoa(f.Unread())) case "deleteexcludes": f.DeleteExcludes() fmt.Fprintf(w, "Deleted Excludes") case "clearcache": f.ClearCache() fmt.Fprintf(w, "Cleared Cache") } fmt.Printf("handleFeed /feed/%s/%s/%s %v\n", id, todo, val, time.Now().Sub(t0)) return }
func handleEdit(w http.ResponseWriter, r *http.Request) { t0 := time.Now() loggedin, user := auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } if !user.Admin { glog.Errorf("user %s attempting to add, not an admin", user) return } var cg string u.PathVars(r, "/edit/", &cg) switch cg { case "console": c, err := game.GetConsole(r.FormValue("name"), user) if err != nil { glog.Errorf("GetConsole(%s,user): %s", r.PostFormValue("name"), err) } type Meta struct { User auth.User Message template.HTML Console game.Console CheapestGame game.Game Search string } var meta Meta meta.User = user meta.Console = c wl, err := game.UserWantedGames(user) if err != nil { glog.Errorf("game.UserWantedGames(user): %s", err) return } meta.CheapestGame = game.Filter(wl).Cheapest() switch r.PostFormValue("action") { case "submit": c.Name = r.PostFormValue("name") c.Manufacturer = r.PostFormValue("manufacturer") c.Year = u.Toint(r.PostFormValue("year")) c.Picture = r.PostFormValue("picture") if err := c.Save(); err != nil { glog.Errorf("Saving console (%s): %s", c, err) meta.Message = template.HTML(err.Error()) } else { meta.Message = template.HTML("Saved") } meta.Console = c if err := tmpl.ExecuteTemplate(w, "edit_console", meta); err != nil { glog.Errorf("ExecuteTemplate(w,edit_console,meta): %s", err) } case "delete": if err := tmpl.ExecuteTemplate(w, "really_delete_console", meta); err != nil { glog.Errorf("ExecuteTemplate(w,really_delete_console,meta): %s", err) return } case "reallydelete": err := c.Delete() if err != nil { glog.Errorf("c.Delete(): %s", err) } http.Redirect(w, r, "/", http.StatusFound) default: if err := tmpl.ExecuteTemplate(w, "edit_console", meta); err != nil { glog.Errorf("ExecuteTemplate(w,edit_console,meta): %s", err) } } case "game": id := u.Toint(r.FormValue("id")) g, err := game.GetGame(id, user) if err != nil { glog.Errorf("game.GetGame(%v,user): %s", id, err) return } type Meta struct { User auth.User CheapestGame game.Game Game game.Game Message template.HTML Search string } var meta Meta meta.User = user meta.Game = g wl, err := game.UserWantedGames(user) if err != nil { glog.Errorf("game.UserWantedGames(user): %s", err) return } meta.CheapestGame = game.Filter(wl).Cheapest() switch r.PostFormValue("action") { case "submit": g.Name = r.PostFormValue("name") g.Publisher = r.PostFormValue("publisher") g.Year = u.Toint(r.PostFormValue("year")) g.ConsoleName = r.PostFormValue("console") if err := g.Save(); err != nil { glog.Errorf("Saving game (%s): %s", g, err) meta.Message = "Error saving" } else { fmt.Printf("Saved game: %s", g) meta.Message = template.HTML("Saved: <a href='/game/" + u.Tostr(g.ID) + "'>Game</a>") } meta.Game = g case "delete": if err := tmpl.ExecuteTemplate(w, "really_delete_game", meta); err != nil { glog.Errorf("ExecuteTemplate(w,really_delete_game,meta): %s", err) return } case "reallydelete": err := g.Delete() if err != nil { glog.Errorf("g.Delete(): %s", err) } http.Redirect(w, r, "/", http.StatusFound) default: meta.Message = template.HTML("in default") } if err := tmpl.ExecuteTemplate(w, "edit_game", meta); err != nil { glog.Errorf("ExecuteTemplate(w,edit_game, meta): %s", err) return } } fmt.Printf("handleEdit %v\n", time.Now().Sub(t0)) }
func handleAdd(w http.ResponseWriter, r *http.Request) { t0 := time.Now() loggedin, user := auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } if !user.Admin { glog.Errorf("user %s attempting to add, not an admin", user) return } var cg string u.PathVars(r, "/add/", &cg) type Meta struct { User auth.User Message template.HTML Consoles []game.Console Search string } var meta Meta meta.User = user cl, err := game.GetConsoles(user) meta.Consoles = cl sort.Sort(game.ConsoleName(cl)) if err != nil { glog.Errorf("game.GetConsoles(%s): %s", user, err) } switch cg { case "console": var c game.Console c.Name = r.PostFormValue("name") c.Manufacturer = r.PostFormValue("manufacturer") c.Year = u.Toint(r.PostFormValue("year")) c.User = user c.Picture = r.PostFormValue("picture") err := c.Save() if err != nil { glog.Errorf("Saving console (%s): %s", c, err) meta.Message = template.HTML(err.Error()) } else { meta.Message = template.HTML("Saved") } case "game": var g game.Game g.Name = r.PostFormValue("name") g.Publisher = r.PostFormValue("publisher") g.Year = u.Toint(r.PostFormValue("year")) g.ConsoleName = r.PostFormValue("console") g.User = user g, err := game.InsertGame(g) if err != nil { glog.Errorf("Saving game (%s): %s", g, err) meta.Message = "Error saving" } else { meta.Message = template.HTML("Saved: <a href='/game/" + u.Tostr(g.ID) + "'>Game</a>") } } if err := tmpl.ExecuteTemplate(w, "add", meta); err != nil { glog.Errorf("ExecuteTemplate(w,add,meta): %s", err) } fmt.Printf("handleAdd %v\n", time.Now().Sub(t0)) }
func handleConsole(w http.ResponseWriter, r *http.Request) { t0 := time.Now() //console/<console name> loggedin, user := auth.LoggedIn(w, r) if !loggedin { http.Redirect(w, r, "/", http.StatusFound) return } type Meta struct { User auth.User Consoles []game.Console CheapestGame game.Game Url string Has bool Box bool Manual bool Search string } var m Meta m.User = user wl, err := game.UserWantedGames(user) if err != nil { glog.Errorf("game.UserWantedGames(user): %s", err) return } m.CheapestGame = game.Filter(wl).Cheapest() if err := tmpl.ExecuteTemplate(w, "main_html", m); err != nil { glog.Errorf("Execute main_html: %s", err) return } var cname string u.PathVars(r, "/console/", &cname) m.Url = "/console/" + cname + "?" c, err := game.GetConsole(cname, user) fmt.Printf("console=%s", c) if err != nil { glog.Errorf("game.GetConsole") } var mycl []game.Console mycl = append(mycl, c) if err := tmpl.ExecuteTemplate(w, "consoles_list", mycl); err != nil { glog.Errorf("ExecuteTemplate(console_entry): %s", err) return } if err != nil { glog.Errorf("ConsoleOnlyEntryHTML.Execute(w,myc): %s", err) } cg, err := c.Games() if err != nil { glog.Errorf("c.Games(): %s", err) return } switch r.FormValue("sort") { default: sort.Sort(game.GameName(cg)) } if r.FormValue("has") == "true" { m.Has = true } if r.FormValue("box") == "true" { m.Box = true } if r.FormValue("manual") == "true" { m.Manual = true } cg = game.Filter(cg).Request(r) if err := tmpl.ExecuteTemplate(w, "filter", m); err != nil { glog.Errorf("tmpl.ExecuteTemplate(w,filter,meta): %s", err) } if err := tmpl.ExecuteTemplate(w, "games_list", cg); err != nil { glog.Errorf("tmpl.ExecuteTemplate(w, games_list, cg): %s", err) return } fmt.Printf("user: %s %v", user.Email, user) fmt.Printf("handleConsole %v\n", time.Now().Sub(t0)) }