From 9f23a823393474beedd5ab0a755ae44d4852af85 Mon Sep 17 00:00:00 2001 From: meowrain Date: Sun, 16 Jun 2024 08:21:27 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0http=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 22 ++++++++--- config.yaml | 5 +++ go.mod | 1 + go.sum | 3 ++ internal/config/config.go | 40 +++++++++++++++---- internal/discovery/shared/shared.go | 2 +- internal/handlers/fileServer.go | 61 +++++++++++++++++++++++++++++ internal/handlers/send.go | 2 - static/css/style.css | 48 +++++++++++++++++++++++ templates/cloudstorage.html | 25 ++++++++++++ {static => templates}/index.html | 1 + 11 files changed, 195 insertions(+), 15 deletions(-) create mode 100644 config.yaml create mode 100644 internal/handlers/fileServer.go create mode 100644 static/css/style.css create mode 100644 templates/cloudstorage.html rename {static => templates}/index.html (97%) diff --git a/cmd/main.go b/cmd/main.go index dbfdef0..77a4e55 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "localsend_cli/internal/config" "localsend_cli/internal/discovery" "localsend_cli/internal/handlers" "localsend_cli/internal/pkg/server" @@ -24,11 +25,22 @@ func main() { // 启动HTTP服务器 httpServer := server.New() - httpServer.HandleFunc("/api/localsend/v2/prepare-upload", handlers.PrepareReceive) - httpServer.HandleFunc("/api/localsend/v2/upload", handlers.ReceiveHandler) - httpServer.HandleFunc("/send", handlers.SendHandler) // 上传处理程序 - httpServer.HandleFunc("/receive", handlers.NormalReceiveHandler) // 下载处理程序 - httpServer.Handle("/", http.FileServer(http.Dir("static"))) + if config.ConfigData.Functions.HttpFileServer { + + //如果启用http文件服务器,启用下面的路由 + httpServer.HandleFunc("/", handlers.IndexFileHandler) + httpServer.HandleFunc("/uploads/", handlers.FileServerHandler) + httpServer.Handle("/static/", http.FileServer(http.Dir("."))) + } + /*发送接收部分*/ + if config.ConfigData.Functions.LocalSendServer { + + httpServer.HandleFunc("/api/localsend/v2/prepare-upload", handlers.PrepareReceive) + httpServer.HandleFunc("/api/localsend/v2/upload", handlers.ReceiveHandler) + httpServer.HandleFunc("/send", handlers.SendHandler) // 上传处理程序 + httpServer.HandleFunc("/receive", handlers.NormalReceiveHandler) // 下载处理程序 + + } go func() { log.Println("Server started at :53317") if err := http.ListenAndServe(":53317", httpServer); err != nil { diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..d2bbe99 --- /dev/null +++ b/config.yaml @@ -0,0 +1,5 @@ +name: "Server" + +functions: + http_file_server: false + local_send_server: true \ No newline at end of file diff --git a/go.mod b/go.mod index 671bd4c..acff6e2 100644 --- a/go.mod +++ b/go.mod @@ -9,4 +9,5 @@ require ( golang.org/x/net v0.26.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.21.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 16a083a..acc5aaf 100644 --- a/go.sum +++ b/go.sum @@ -8,3 +8,6 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/internal/config/config.go b/internal/config/config.go index 4fa5d2a..2bf0094 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,11 +1,37 @@ package config -var Names = []string{ - "Alpha", "Bravo", "Charlie", "Delta", "Echo", - "Foxtrot", "Golf", "Hotel", "India", "Juliett", - "Kilo", "Lima", "Mike", "November", "Oscar", - "Papa", "Quebec", "Romeo", "Sierra", "Tango", - "Uniform", "Victor", "Whiskey", "X-ray", "Yankee", "Zulu", +import ( + "io" + "log" + "os" + + "gopkg.in/yaml.v2" +) + +type Config struct { + NameOfDevice string `yaml:"name"` + Functions struct { + HttpFileServer bool `yaml:"http_file_server"` + LocalSendServer bool `yaml:"local_send_server"` + } `yaml:"functions"` } -var NameOfDevice string = "Server" +var ConfigData Config + +func init() { + configFile, err := os.Open("config.yaml") + if err != nil { + log.Fatalf("Error opening config file: %v", err) + } + defer configFile.Close() + + bytes, err := io.ReadAll(configFile) + if err != nil { + log.Fatalf("Error reading config file: %v", err) + } + + err = yaml.Unmarshal(bytes, &ConfigData) + if err != nil { + log.Fatalf("Error parsing config file: %v", err) + } +} diff --git a/internal/discovery/shared/shared.go b/internal/discovery/shared/shared.go index 7140339..dbd0abf 100644 --- a/internal/discovery/shared/shared.go +++ b/internal/discovery/shared/shared.go @@ -12,7 +12,7 @@ import ( var DiscoveredDevices = make(map[string]BroadcastMessage) var Mu sync.Mutex var Messsage BroadcastMessage = BroadcastMessage{ - Alias: config.NameOfDevice, + Alias: config.ConfigData.NameOfDevice, Version: "2.0", DeviceModel: utils.CheckOSType(), DeviceType: "desktop", diff --git a/internal/handlers/fileServer.go b/internal/handlers/fileServer.go new file mode 100644 index 0000000..f8dae93 --- /dev/null +++ b/internal/handlers/fileServer.go @@ -0,0 +1,61 @@ +package handlers + +import ( + "html/template" + "net/http" + "os" + "path/filepath" + "strings" +) + +const uploadDir = "./uploads" + +func GetFilesFromDir(dir string) ([]os.DirEntry, error) { + entries, err := os.ReadDir(dir) + if err != nil { + return nil, err + } + return entries, nil +} + +func FileServerHandler(w http.ResponseWriter, r *http.Request) { + file := strings.TrimPrefix(r.URL.Path, "/uploads/") + http.ServeFile(w, r, filepath.Join(uploadDir, file)) +} + +func IndexFileHandler(w http.ResponseWriter, r *http.Request) { + dirPath := filepath.Join(uploadDir, strings.TrimPrefix(r.URL.Path, "/uploads/")) + + info, err := os.Stat(dirPath) + if os.IsNotExist(err) { + http.NotFound(w, r) + return + } else if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if info.IsDir() { + files, err := GetFilesFromDir(dirPath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmpl := template.Must(template.ParseFiles("templates/cloudstorage.html")) + data := struct { + Path string + Files []os.DirEntry + }{ + Path: r.URL.Path, + Files: files, + } + + err = tmpl.Execute(w, data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + } else { + http.ServeFile(w, r, dirPath) + } +} diff --git a/internal/handlers/send.go b/internal/handlers/send.go index b07b495..493c7f5 100644 --- a/internal/handlers/send.go +++ b/internal/handlers/send.go @@ -8,8 +8,6 @@ import ( "path/filepath" ) -const uploadDir = "uploads" - // SendHandler 处理文件上传请求 func SendHandler(w http.ResponseWriter, r *http.Request) { // 解析multipart/form-data diff --git a/static/css/style.css b/static/css/style.css new file mode 100644 index 0000000..3a69bb4 --- /dev/null +++ b/static/css/style.css @@ -0,0 +1,48 @@ +body { + font-family: Arial, sans-serif; + background-color: #f4f4f4; + margin: 0; + padding: 0; + display: flex; + justify-content: center; + align-items: center; + min-height: 100vh; +} +.container { + background-color: #fff; + padding: 20px 40px; + border-radius: 10px; + box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); + width: 80%; + max-width: 700px; + box-sizing: border-box; +} +h1 { + color: #333; + text-align: center; + margin-bottom: 30px; + font-size: 2em; +} +ul { + list-style: none; + padding: 0; + margin: 0; +} +li { + margin: 10px 0; + padding: 10px; + border-radius: 5px; + transition: background-color 0.3s, transform 0.3s; +} +li:hover { + background-color: #f1f1f1; + transform: scale(1.02); +} +a { + text-decoration: none; + color: #007BFF; + font-weight: bold; +} +a:hover { + text-decoration: underline; +} \ No newline at end of file diff --git a/templates/cloudstorage.html b/templates/cloudstorage.html new file mode 100644 index 0000000..5bd069d --- /dev/null +++ b/templates/cloudstorage.html @@ -0,0 +1,25 @@ + + + + + + Uploads + + + +
+

Uploads

+ +
+ + diff --git a/static/index.html b/templates/index.html similarity index 97% rename from static/index.html rename to templates/index.html index 61b9d65..d8814a8 100644 --- a/static/index.html +++ b/templates/index.html @@ -16,6 +16,7 @@

Welcome to localsend_cli

Get Started Documentation +