Add random background images for the login page
This commit is contained in:
parent
c9fe62e7fd
commit
036d3ade92
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
test.db
|
||||
user_dbs/
|
||||
todo-web
|
||||
static/img/login/*
|
||||
|
4
main.go
4
main.go
@ -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
27
pages/randImage.go
Normal 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()))
|
||||
}
|
Loading…
Reference in New Issue
Block a user