Add random background images for the login page

This commit is contained in:
Cameron Reed 2024-10-01 11:24:40 -06:00
parent c9fe62e7fd
commit 036d3ade92
3 changed files with 32 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
test.db
user_dbs/
todo-web
static/img/login/*

View File

@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"net/http"
"path"
"github.com/Cameron-Reed1/todo-web/api"
"github.com/Cameron-Reed1/todo-web/db"
@ -56,6 +57,7 @@ func addFrontendEndpoints(mux *http.ServeMux, static_path string) {
mux.HandleFunc("/upcoming", pages.UpcomingFragment)
mux.HandleFunc("/login", pages.Login)
mux.HandleFunc("/create-account", pages.CreateAccount)
// mux.HandleFunc("/account", pages.AccountPage)
mux.HandleFunc("POST /logout", pages.Logout)
mux.HandleFunc("DELETE /delete/{id}", pages.DeleteItem)
mux.HandleFunc("PATCH /set/{id}", pages.SetItemCompleted)
@ -65,6 +67,8 @@ func addFrontendEndpoints(mux *http.ServeMux, static_path string) {
fileServer := http.FileServer(http.Dir(static_path))
mux.Handle("/css/", fileServer)
mux.Handle("/js/", fileServer)
mux.Handle("/img/", fileServer)
mux.HandleFunc("/img/login.jpg", func(w http.ResponseWriter, r *http.Request) { pages.RandomImage(w, r, path.Join(static_path, "img/login/")) })
}
func addBackendEndpoints(mux *http.ServeMux) {

27
pages/randImage.go Normal file
View File

@ -0,0 +1,27 @@
package pages
import (
"math/rand/v2"
"net/http"
"os"
"path"
)
func RandomImage(w http.ResponseWriter, r *http.Request, basePath string) {
file, err := os.Open(basePath)
if err != nil {
return
}
defer file.Close()
files, err := file.Readdir(-1)
if err != nil {
return
}
idx := rand.UintN(uint(len(files)))
w.Header().Add("Cache-Control", "max-age=300")
http.ServeFile(w, r, path.Join(basePath, files[idx].Name()))
}