-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (103 loc) · 2.72 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"embed"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
)
//go:embed FileBox/dist/*
var staticFiles embed.FS
type FileInfo struct {
Type string `json:"type"`
Name string `json:"name"`
ModifiedDate time.Time `json:"modifiedDate"`
Size *int64 `json:"size,omitempty"`
}
type DirectoryResponse struct {
CurrentPath string `json:"currentPath"`
Files []FileInfo `json:"files"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
func main() {
app := fiber.New()
app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(staticFiles),
PathPrefix: "FileBox/dist",
}))
app.Get("/healthcheck", func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
})
app.Get("/ws", websocket.New(func(c *websocket.Conn) {
if err := serveDirectory(c, "."); err != nil {
log.Printf("Error serving initial directory: %v", err)
c.WriteJSON(ErrorResponse{Error: "Failed to serve initial directory"})
return
}
for {
_, msg, err := c.ReadMessage()
if err != nil {
log.Printf("Error reading message: %v", err)
c.WriteJSON(ErrorResponse{Error: "Failed to read message"})
break
}
log.Printf("Received message: %s", msg)
dirPath := string(msg)
if err := serveDirectory(c, dirPath); err != nil {
log.Printf("Error serving directory: %v", err)
c.WriteJSON(ErrorResponse{Error: fmt.Sprintf("Failed to serve directory: %v", err)})
}
}
}))
log.Printf("Server starting on :3000")
if err := app.Listen(":3000"); err != nil {
log.Fatalf("Error starting server: %v", err)
}
}
func serveDirectory(c *websocket.Conn, dirPath string) error {
absPath, err := filepath.Abs(dirPath)
if err != nil {
return fmt.Errorf("failed to get absolute path: %w", err)
}
files, err := os.ReadDir(absPath)
if err != nil {
return fmt.Errorf("failed to read directory '%s': %w", absPath, err)
}
var fileInfos []FileInfo
for _, file := range files {
fileType := "file"
var size *int64
info, err := file.Info()
if err != nil {
log.Printf("Error getting info for file '%s': %v", file.Name(), err)
continue
}
if file.IsDir() {
fileType = "directory"
} else {
fileSize := info.Size()
size = &fileSize
}
fileInfos = append(fileInfos, FileInfo{
Type: fileType,
Name: file.Name(),
ModifiedDate: info.ModTime(),
Size: size,
})
}
response := DirectoryResponse{
CurrentPath: absPath,
Files: fileInfos,
}
if err := c.WriteJSON(response); err != nil {
return fmt.Errorf("failed to send directory information: %w", err)
}
return nil
}