Compare commits
2 Commits
bf0bc3ceb8
...
d37cacf2b7
| Author | SHA1 | Date | |
|---|---|---|---|
| d37cacf2b7 | |||
| 5ab286bad9 |
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
@ -278,15 +278,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)
|
||||
}
|
||||
|
||||
BIN
uploads/Cat03.jpg
Normal file
BIN
uploads/Cat03.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 273 KiB |
@ -5,6 +5,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Latest Picture</h1>
|
||||
<img src="/uploads/Cat03.jpg" alt="Latest Picture">
|
||||
<img src="{{.picture}}" alt="Latest Picture">
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue
Block a user