flounder

A simple gemini site builder
git clone git://git.alexwennerberg.com/flounder
Log | Files | Refs | README | LICENSE

commit d50ddbc2504ef28a2f52071a918326a8179805c2
parent a570322bb3d4db15ff3d69556a1754634e7773c3
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon,  3 Oct 2022 18:31:07 -0700

add all users list

Diffstat:
Mdb.go | 11+++++++++--
Mfinger.go | 2+-
Mgemini.go | 2+-
Mhttp.go | 13++++++++++++-
4 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/db.go b/db.go @@ -89,8 +89,15 @@ type User struct { DomainEnabled bool } -func getActiveUserNames() ([]string, error) { - rows, err := DB.Query(`SELECT username from user WHERE active is true order by random() limit 10`) +// kinda hacky +func getActiveUserNames(all bool) ([]string, error) { + var rows *sql.Rows + var err error + if all { + rows, err = DB.Query(`SELECT username from user WHERE active is true`) + } else { + rows, err = DB.Query(`SELECT username from user WHERE active is true order by random() limit 10`) + } if err != nil { return nil, err } diff --git a/finger.go b/finger.go @@ -19,7 +19,7 @@ func runFingerServer() { log.Printf("finger request: %v", q) if q.Username == "" { var hasPlan []string - users, err := getActiveUserNames() + users, err := getActiveUserNames(true) for _, user := range users { plan := path.Join(c.FilesDirectory, user, ".plan") _, err := os.Stat(plan) diff --git a/gemini.go b/gemini.go @@ -58,7 +58,7 @@ func gmiIndex(w gmi.ResponseWriter, r *gmi.Request) { log.Fatal(err) } files, err := getUpdatedFiles(false, "") - users, err := getActiveUserNames() + users, err := getActiveUserNames(false) if err != nil { log.Println(err) w.WriteHeader(gmi.StatusTemporaryFailure, "Server Error") diff --git a/http.go b/http.go @@ -49,6 +49,16 @@ func renderError(w http.ResponseWriter, errorMsg string, statusCode int) { } } +func allUsersHandler(w http.ResponseWriter, r *http.Request) { + allUsers, err := getActiveUserNames(true) + if err != nil { + serverError(w, err) + return + } else { + w.Write([]byte(strings.Join(allUsers, "\n"))) + } +} + func rootHandler(w http.ResponseWriter, r *http.Request) { // serve everything inside static directory if r.URL.Path != "/" { @@ -68,7 +78,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { serverError(w, err) return } - allUsers, err := getActiveUserNames() + allUsers, err := getActiveUserNames(false) if err != nil { serverError(w, err) return @@ -867,6 +877,7 @@ func runHTTPServer() { serveMux.HandleFunc(hostname+"/admin", adminHandler) serveMux.HandleFunc(hostname+"/edit/", editFileHandler) serveMux.HandleFunc(hostname+"/updates/", updatesHandler) + serveMux.HandleFunc(hostname+"/allusers", allUsersHandler) serveMux.HandleFunc(hostname+"/upload", uploadFilesHandler) serveMux.Handle(hostname+"/login", limit(http.HandlerFunc(loginHandler))) serveMux.Handle(hostname+"/register", limit(http.HandlerFunc(registerHandler)))