mirror of
https://github.com/riwiwa/muzi.git
synced 2026-02-28 11:56:57 -08:00
redirect to profile on root, login if logged out, create account if no accounts. move pfp location
This commit is contained in:
2
db/db.go
2
db/db.go
@@ -95,7 +95,7 @@ func CreateUsersTable() error {
|
|||||||
username TEXT NOT NULL UNIQUE,
|
username TEXT NOT NULL UNIQUE,
|
||||||
password TEXT NOT NULL,
|
password TEXT NOT NULL,
|
||||||
bio TEXT DEFAULT 'This profile has no bio.',
|
bio TEXT DEFAULT 'This profile has no bio.',
|
||||||
pfp TEXT DEFAULT '/files/assets/default.png',
|
pfp TEXT DEFAULT '/files/assets/pfps/default.png',
|
||||||
allow_duplicate_edits BOOLEAN DEFAULT FALSE,
|
allow_duplicate_edits BOOLEAN DEFAULT FALSE,
|
||||||
pk SERIAL PRIMARY KEY
|
pk SERIAL PRIMARY KEY
|
||||||
);`)
|
);`)
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 7.7 KiB After Width: | Height: | Size: 7.7 KiB |
28
web/web.go
28
web/web.go
@@ -681,11 +681,39 @@ func importSpotifyProgressHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasUsers(ctx context.Context) bool {
|
||||||
|
var count int
|
||||||
|
err := db.Pool.QueryRow(ctx, "SELECT COUNT(*) FROM users;").Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error checking for users: %v\n", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return count > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func rootHandler() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !hasUsers(r.Context()) {
|
||||||
|
http.Redirect(w, r, "/createaccount", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
username := getLoggedInUsername(r)
|
||||||
|
if username == "" {
|
||||||
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/profile/"+username, http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
addr := ":1234"
|
addr := ":1234"
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Handle("/files/*", http.StripPrefix("/files", http.FileServer(http.Dir("./static"))))
|
r.Handle("/files/*", http.StripPrefix("/files", http.FileServer(http.Dir("./static"))))
|
||||||
|
r.Get("/", rootHandler())
|
||||||
r.Get("/login", loginPageHandler())
|
r.Get("/login", loginPageHandler())
|
||||||
r.Get("/createaccount", createAccountPageHandler())
|
r.Get("/createaccount", createAccountPageHandler())
|
||||||
r.Get("/profile/{username}", profilePageHandler())
|
r.Get("/profile/{username}", profilePageHandler())
|
||||||
|
|||||||
Reference in New Issue
Block a user