-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
73 lines (64 loc) · 1.74 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
const (
PathList = "/files"
PathDownload = "/download/"
QueryParamKey = "secret"
)
func runHttpServer(port int) {
fn := "runHttpServer"
http.HandleFunc(PathList, secretFilterHandler(fileListHandler))
http.HandleFunc(PathDownload, secretFilterHandler(fileDownloadHandler))
fmt.Printf("%s is listening on port %d\n", fn, port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
fmt.Println("Error starting file server:", err)
}
}
func secretFilterHandler(next http.HandlerFunc) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
getSecret := r.URL.Query().Get(QueryParamKey)
if getSecret != secret {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next(w, r)
}
}
func fileListHandler(w http.ResponseWriter, r *http.Request) {
files, err := os.ReadDir(path)
if err != nil {
http.Error(w, "Unable to list files", http.StatusInternalServerError)
return
}
result := make([]string, 0)
w.Header().Set("Content-Type", "application/json")
for _, f := range files {
if !f.IsDir() {
result = append(result, f.Name())
}
}
marshal, err := json.Marshal(map[string]interface{}{
"data": result,
})
if err != nil {
http.Error(w, "Unable to list files", http.StatusInternalServerError)
return
}
_, _ = w.Write(marshal)
}
func fileDownloadHandler(w http.ResponseWriter, r *http.Request) {
fileName := strings.TrimPrefix(r.URL.Path, PathDownload)
filePath := filepath.Join(path, fileName)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
http.Error(w, "File not found", http.StatusNotFound)
return
}
http.ServeFile(w, r, filePath)
}