diff --git a/.gitignore b/.gitignore index e137b98..fdc0cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ test.db user_dbs/ todo-web +static/img/login/* diff --git a/main.go b/main.go index 43fce39..59f9353 100644 --- a/main.go +++ b/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) { diff --git a/pages/randImage.go b/pages/randImage.go new file mode 100644 index 0000000..7dc8f5e --- /dev/null +++ b/pages/randImage.go @@ -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())) +}