From 5aa3c8425440bcab56db263ad5a783915a909115 Mon Sep 17 00:00:00 2001 From: xc Date: Wed, 20 Aug 2025 14:15:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aufs/core/dtmem.go | 44 ++++++++++++++++++++ aufs/main.go | 2 + main.go | 101 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 aufs/core/dtmem.go create mode 100644 main.go diff --git a/aufs/core/dtmem.go b/aufs/core/dtmem.go new file mode 100644 index 0000000..5232862 --- /dev/null +++ b/aufs/core/dtmem.go @@ -0,0 +1,44 @@ +package core + +import ( + "encoding/json" + "fmt" + "net/http" + "runtime" +) + +type Dtm struct { + TotalAlloc string `json:"total_alloc"` + Alloc string `json:"alloc"` +} + +// 获取内存信息 +func Dtmem(w http.ResponseWriter, r *http.Request) { + var m runtime.MemStats + runtime.ReadMemStats(&m) + + dtm := Dtm{ + TotalAlloc: fmt.Sprintf("%d", m.TotalAlloc), + + Alloc: fmt.Sprintf("%v MB", m.Alloc/1024), + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(dtm) + + // + // jsonobj, err := json.Marshal(m) + // if err != nil { + // fmt.Fprintf(w, "json marshal faild %v", err) + // return + // } + + // fmt.Fprintf(w, "%s", jsonobj) + + // usage:=m.TotalAlloc/1024/1024 + // free:=m.Free/1024/1024 + + // // + // fmt.Fprintf("总分配内存: %v MB\n", m.TotalAlloc/1024/1024) + // fmt.Fprintf("已分配内存: %v MB\n", m.Alloc/1024/1024) +} diff --git a/aufs/main.go b/aufs/main.go index 6ce5f16..e0538fa 100644 --- a/aufs/main.go +++ b/aufs/main.go @@ -28,6 +28,8 @@ func startWeb() { // http.HandleFunc("/rc", core.ReceiveHandler) // 发送zip文件 // http.HandleFunc("/sendZip", core.SendZip) + // 内存信息 + http.HandleFunc("/dtmem", core.Dtmem) fmt.Printf("Starting server at port 9099\n") // 启动HTTP服务器并监听端口 80,如果出现错误,则打印错误信息并退出 diff --git a/main.go b/main.go new file mode 100644 index 0000000..a99d571 --- /dev/null +++ b/main.go @@ -0,0 +1,101 @@ +package main + +import ( + "fmt" + "fssc/config" + "net/http" + "os" + "path/filepath" + + "fssc/internal/discovery" + "fssc/internal/handler" + "fssc/web" + + "github.com/urfave/cli/v2" +) + +var ( + port string +) + +// 初始化配置 +func initConfig(c *cli.Context) error { + return config.G.SetConf(port) +} + +// 发送端 +func sendClient() error { + // 发送者角色 + go discovery.Send(discovery.Sender) + //udp 监听指定路径下的文件 + go discovery.Listen(discovery.Receiver, config.G.FilePath) + + url := fmt.Sprintf("http://%s:%s", config.G.LocalIP, config.G.Port) + fmt.Printf("Server address: %s\n", url) + + http.HandleFunc("/", handler.SendHandler) + // ico + http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "image/x-icon") + http.ServeFile(w, r, "./favicon.ico") + }) + // 加载静态资源 + http.Handle("/static/", http.StripPrefix("/static/", + http.FileServer(http.FS(web.StaticFs)))) + + // 文件更新包 + http.HandleFunc("/up", handler.UpServer) + // 传送单文件 + http.HandleFunc("/sup", handler.Supfile) + // udp 传送文件 + http.HandleFunc("/sendZip", handler.SendZip) + // 监视目标的fs json + http.HandleFunc("/nf", handler.NfTest) + // web console + http.HandleFunc("/console", handler.WebConsole) + + fmt.Println("send file to server...") + + return http.ListenAndServe(config.G.WildcardAddress, nil) +} + +// 入口函数 +func main() { + app := &cli.App{ + Name: "fssc", + Usage: "file transfer tool", + UsageText: "fssc [global options] [filename|foldername]", + Description: "fssc is a simple command-line tool send file/folder", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "port", + Value: "9099", + Usage: "server port", + Aliases: []string{"p"}, + Destination: &port, + }, + &cli.BoolFlag{ + Name: "version", + Aliases: []string{"v"}, + Usage: "print the version", + }, + }, + Action: func(c *cli.Context) error { + + if c.NArg() > 0 { + currentPath := filepath.ToSlash(c.Args().Get(0)) + config.G.FilePath = currentPath + return sendClient() + } else { + fmt.Println("no input args") + return nil + } + //return receiveClient() + }, + Before: initConfig, + } + err := app.Run(os.Args) + if err != nil { + fmt.Println(err.Error()) + } +}