prep to local avatar cache

This commit is contained in:
Umputun
2018-02-14 03:09:30 -06:00
parent 1a13cd834e
commit 23a52be1de
4 changed files with 19 additions and 15 deletions
+9 -9
View File
@@ -72,14 +72,14 @@ func initProvider(p Params, provider Provider) Provider {
// Routes returns auth routes for given provider
func (p Provider) Routes() chi.Router {
router := chi.NewRouter()
router.Get("/login", p.LoginHandler)
router.Get("/callback", p.AuthHandler)
router.Get("/logout", p.LogoutHandler)
router.Get("/login", p.loginHandler)
router.Get("/callback", p.authHandler)
router.Get("/logout", p.logoutHandler)
return router
}
// LoginHandler - GET /login?from=redirect-back-url
func (p Provider) LoginHandler(w http.ResponseWriter, r *http.Request) {
// loginHandler - GET /login?from=redirect-back-url
func (p Provider) loginHandler(w http.ResponseWriter, r *http.Request) {
// make state (random) and store in session
state := p.randToken()
@@ -106,9 +106,9 @@ func (p Provider) LoginHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
}
// AuthHandler fills user info and redirects to "from" url. This is callback url redirected locally by browser
// authHandler fills user info and redirects to "from" url. This is callback url redirected locally by browser
// GET /callback
func (p Provider) AuthHandler(w http.ResponseWriter, r *http.Request) {
func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) {
session, err := p.Get(r, "remark")
if err != nil {
@@ -178,8 +178,8 @@ func (p Provider) AuthHandler(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, jData)
}
// LogoutHandler - GET /logout
func (p Provider) LogoutHandler(w http.ResponseWriter, r *http.Request) {
// logoutHandler - GET /logout
func (p Provider) logoutHandler(w http.ResponseWriter, r *http.Request) {
session, err := p.Get(r, "remark")
if err != nil {
common.SendErrorJSON(w, r, http.StatusBadRequest, err, "failed to get session")
+4 -5
View File
@@ -24,14 +24,13 @@ func MakeTree(comments []store.Comment, sortType string) *Tree {
topComments := res.filter(comments, "")
res.Nodes = []*Node{}
for _, rc := range topComments {
node := Node{Comment: rc}
for _, rootComment := range topComments {
node := Node{Comment: rootComment}
commentsTree := res.proc(comments, &node, rc.ID)
if commentsTree.Comment.Deleted && len(commentsTree.Replies) == 0 { // skip deleted with no subcomments
commentsTree := res.proc(comments, &node, rootComment.ID)
if rootComment.Deleted && len(commentsTree.Replies) == 0 { // skip deleted with no subcomments
continue
}
res.Nodes = append(res.Nodes, commentsTree)
}
+1
View File
@@ -28,6 +28,7 @@ func TestStore_MakeTree(t *testing.T) {
{ID: "22", ParentID: "2", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC)},
{ID: "4", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC)},
{ID: "3", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC)},
{ID: "5", Deleted: true},
}
res := MakeTree(comments, "time")
+5 -1
View File
@@ -80,6 +80,7 @@ func (s *Server) Run(port int) {
// shortcut, can be any of providers, all logouts do the same - removes cookie
r.Get("/logout", s.AuthProviders[0].LogoutHandler)
}
r.Get("/avatar/{id}", p.avatarHandler)
})
// api routes
@@ -406,6 +407,9 @@ func (s *Server) voteCtrl(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, JSON{"id": comment.ID, "score": comment.Score})
}
func (s *Server) avatarHandler(w http.ResponseWriter, r *http.Request) {
}
// serves static files from /web
func (s *Server) addFileServer(r chi.Router, path string, root http.FileSystem) {
log.Printf("[INFO] run file server for %s", root)
@@ -418,7 +422,7 @@ func (s *Server) addFileServer(r chi.Router, path string, root http.FileSystem)
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// don't show dirs, just serve files
if strings.HasSuffix(r.URL.Path, "/") && len(r.URL.Path) > 1 && r.URL.Path != "/web/" {
if strings.HasSuffix(r.URL.Path, "/") && len(r.URL.Path) > 1 && r.URL.Path != "/"+path+"/" {
http.NotFound(w, r)
return
}