small pixel drawing of a pufferfish vore

implement userHandler
Jes Olson j3s@c3f.net
Sat, 18 Mar 2023 19:54:06 -0700
commit

e50e3d236b6c5088d782c97ade1f169810e94ebd

parent

43b570b7cc35424e98c7401117f12d13254a066f

4 files changed, 76 insertions(+), 10 deletions(-)

jump to
M main.gomain.go

@@ -3,21 +3,27 @@

import ( "log" "net/http" + "strings" ) func main() { s := New() mux := http.NewServeMux() + // since "/" is a wildcard, this anonymous function acts + // as a router for patterns that can't be registered at + // start time. e.g. /j3s or /j3s/feeds mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { s.indexHandler(w, r) return } - if r.URL.Path == "/" { - s.indexHandler(w, r) + // handles /<username> + if s.db.UserExists(strings.TrimPrefix(r.URL.Path, "/")) { + s.userHandler(w, r) return } + http.NotFound(w, r) }) mux.HandleFunc("/login", s.loginHandler) mux.HandleFunc("/logout", s.logoutHandler)
M readmereadme

@@ -31,19 +31,22 @@ add new user & hashed password to database

register failure: redirect to /login TODO: validate user input [ ] GET /{username} - display pretty feeds for a given user - there is a button to view the raw feed list (plaintext) - if logged in: there is an edit button - [ ] GET /{username}/feeds - put your feeds here, one per line: + display a users feed items by date, maybe in a table? + -> there is a button to view the feed an article came from + [ ] GET /feeds + pretty-print your feeds <text box with pre-populated list of feeds from {username}> display a text box pre-populated with the list of feeds that make up {username} if you don't know what feeds to use, check out /discover for ideas button: validate if validated, display locked list of feeds with button: submit - [ ] POST /{username}/feeds - subscribe to all of the feeds in your textbox + [ ] POST /feeds + range over submitted feeds & validate them + success: redir to /feeds/validate + fail: user input err or whatever it's called + + extra: tool for looking up feed from website background: fetch feeds every ~90m
M site.gosite.go

@@ -3,6 +3,7 @@

import ( "fmt" "net/http" + "strings" "git.j3s.sh/feeds.gay/auth" "git.j3s.sh/feeds.gay/sqlite"

@@ -123,6 +124,37 @@ return

} http.Redirect(w, r, "/", http.StatusSeeOther) } +} + +func (s *Site) userHandler(w http.ResponseWriter, r *http.Request) { + if !methodAllowed(w, r, "GET") { + return + } + username := strings.TrimPrefix(r.URL.Path, "/") + + fmt.Fprintf(w, `<!DOCTYPE html> + <p>%s's feeds:`, username) + // feeds := s.db.GetFeeds(username) + // for _, feed := range feeds { + // fmt.Fprintf(w, "<p>%s", feed) + // } + + // print the list of feeds + + // if s.loggedIn(r) { + // // TODO: render user template + // } else { + // // TODO: render user template + // } + // fmt.Fprintf(w, `<!DOCTYPE html> + // + // <title>%s</title> + // <p> { %s <a href=/logout>logout</a> }`, s.title, s.username(r)) + // + // fmt.Fprintf(w, `<!DOCTYPE html> + // + // <title>%s</title> + // <a href="/login">login</a>`, s.title) } // username fetches a client's username based
M sqlite/sql.gosqlite/sql.go

@@ -60,7 +60,7 @@

return &wrapper } -// TODO: evaluate error cases +// TODO: think more about errors func (s *DB) GetUsernameBySessionToken(token string) string { var username string

@@ -85,6 +85,7 @@ panic(err)

} return password } + func (s *DB) SetSessionToken(username string, token string) { _, err := s.sql.Exec("UPDATE user SET session_token=? WHERE username=?", token, username) if err != nil {

@@ -98,3 +99,27 @@ if err != nil {

panic(err) } } + +func (s *DB) UserExists(username string) bool { + var result string + err := s.sql.QueryRow("SELECT username FROM user WHERE username=?", username).Scan(&result) + if err == sql.ErrNoRows { + return false + } + if err != nil { + panic(err) + } + return true +} + +func (s *DB) GetFeeds(username string) bool { + var result string + err := s.sql.QueryRow("SELECT username FROM user WHERE username=?", username).Scan(&result) + if err == sql.ErrNoRows { + return false + } + if err != nil { + panic(err) + } + return true +}