Compare commits
No commits in common. "d37cacf2b7daa9fc93e7f3f0c686d015fb8078da" and "bf0bc3ceb898583c1482d6fd414f1cb55366ae2c" have entirely different histories.
d37cacf2b7
...
bf0bc3ceb8
@ -40,9 +40,7 @@ func main() {
|
|||||||
http.HandleFunc("/upload", internal.AuthMiddleware(internal.UploadHandler))
|
http.HandleFunc("/upload", internal.AuthMiddleware(internal.UploadHandler))
|
||||||
http.HandleFunc("/health", internal.HealthHandler)
|
http.HandleFunc("/health", internal.HealthHandler)
|
||||||
|
|
||||||
http.HandleFunc("/uploads/", internal.AuthClientMiddleware(
|
http.HandleFunc("/uploads/", internal.AuthClientMiddleware(internal.HandleFileServer("/uploads", "/uploads/")))
|
||||||
internal.HandlerToHandlerFunc(http.StripPrefix("/uploads/", http.FileServer(http.Dir("./uploads")))),
|
|
||||||
))
|
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,10 +24,7 @@ func InitLog() {
|
|||||||
var logFilePath string
|
var logFilePath string
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
logFilePath = filepath.Join("C:\\ProgramData\\CCTV", logFileName)
|
logFilePath = filepath.Join("C:\\ProgramData\\CCTV", logFileName)
|
||||||
} else if runtime.GOOS == "darwin" {
|
|
||||||
logFilePath = filepath.Join("/Library/Logs", logFileName)
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
logFilePath = filepath.Join("/var/log", logFileName)
|
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) {
|
func DashboardHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// get the newest picture name
|
// get the newest picture name
|
||||||
pictures, err := os.ReadDir("uploads")
|
pictures, err := os.ReadDir(UploadDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Error reading directory", http.StatusInternalServerError)
|
http.Error(w, "Error reading directory", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -278,23 +278,15 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandlerToHandlerFunc(h http.Handler) http.HandlerFunc {
|
func HandleFileServer(dir, prefix string) http.HandlerFunc {
|
||||||
|
fs := http.FileServer(http.Dir(dir))
|
||||||
|
realHandler := http.StripPrefix(prefix, fs).ServeHTTP
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
h.ServeHTTP(w, r)
|
LogFileServerRequest(getClientIP(r), r.URL.Path)
|
||||||
|
realHandler(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) {
|
func HealthHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 273 KiB |
@ -4,8 +4,7 @@
|
|||||||
<title>CCTV</title>
|
<title>CCTV</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Latest Picture</h1>
|
<h1>Latest Picture</h1>
|
||||||
<img src="/uploads/Cat03.jpg" alt="Latest Picture">
|
<img src="{{.picture}}" alt="Latest Picture">
|
||||||
<img src="{{.picture}}" alt="Latest Picture">
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in New Issue
Block a user