diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..a4feeda Binary files /dev/null and b/.DS_Store differ diff --git a/cmd/main.go b/cmd/main.go index 3b5ba58..928f674 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -40,7 +40,9 @@ func main() { http.HandleFunc("/upload", internal.AuthMiddleware(internal.UploadHandler)) http.HandleFunc("/health", internal.HealthHandler) - http.HandleFunc("/uploads/", internal.AuthClientMiddleware(internal.HandleFileServer("/uploads", "/uploads/"))) + http.HandleFunc("/uploads/", internal.AuthClientMiddleware( + internal.HandlerToHandlerFunc(http.StripPrefix("/uploads/", http.FileServer(http.Dir("./uploads")))), + )) http.ListenAndServe(":8080", nil) } diff --git a/internal/log.go b/internal/log.go index 041463c..7af0ba3 100644 --- a/internal/log.go +++ b/internal/log.go @@ -24,7 +24,10 @@ func InitLog() { var logFilePath string if runtime.GOOS == "windows" { logFilePath = filepath.Join("C:\\ProgramData\\CCTV", logFileName) + } else if runtime.GOOS == "darwin" { + logFilePath = filepath.Join("/Library/Logs", logFileName) } else { + logFilePath = filepath.Join("/var/log", logFileName) } diff --git a/internal/server.go b/internal/server.go index a09c93e..3191d70 100644 --- a/internal/server.go +++ b/internal/server.go @@ -155,7 +155,7 @@ func AuthClientMiddleware(next http.HandlerFunc) http.HandlerFunc { func DashboardHandler(w http.ResponseWriter, r *http.Request) { // get the newest picture name - pictures, err := os.ReadDir(UploadDir) + pictures, err := os.ReadDir("uploads") if err != nil { http.Error(w, "Error reading directory", http.StatusInternalServerError) return @@ -277,15 +277,23 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { } -func HandleFileServer(dir, prefix string) http.HandlerFunc { - fs := http.FileServer(http.Dir(dir)) - realHandler := http.StripPrefix(prefix, fs).ServeHTTP +func HandlerToHandlerFunc(h http.Handler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - LogFileServerRequest(getClientIP(r), r.URL.Path) - realHandler(w, r) + h.ServeHTTP(w, r) } } +func FileServerHandler(w http.ResponseWriter, r *http.Request) { + filepath := filepath.Join("uploads", r.URL.Path) + + if _, err := os.Stat(filepath); err != nil { + http.Error(w, "File not found", http.StatusNotFound) + return + } + + http.ServeFile(w, r, filepath) +} + func HealthHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } diff --git a/uploads/Cat03.jpg b/uploads/Cat03.jpg new file mode 100644 index 0000000..9b95fef Binary files /dev/null and b/uploads/Cat03.jpg differ diff --git a/views/dash.html b/views/dash.html index 7eeebe1..afd077d 100644 --- a/views/dash.html +++ b/views/dash.html @@ -4,7 +4,8 @@
+