package main import ( "aufs/config" "aufs/core" "aufs/crypto" "fmt" "log" "mime" "net/http" "os" "os/signal" "syscall" ) // 启动web服务 func startWeb() { // 手动注册SVG的MIME类型(防止系统默认配置缺失) mime.AddExtensionType(".svg", "image/svg+xml") mime.AddExtensionType(".svgz", "image/svg+xml") // 压缩的SVG //创建一个文件服务器,会去www目录下找index.html fileServer := http.FileServer(http.Dir("./www")) // 将 "/" 路径映射到文件服务器 http.Handle("/", fileServer) //扫描指定的目录 http.HandleFunc("/sc", core.SerInfo) // 数据库基础信息 http.HandleFunc("/bs", core.BfsInfo) // 硬件信息 http.HandleFunc("/hd", core.Hdinfo) // sse 推送 http.HandleFunc("/sse", core.SseHandler) // 文件接收 // http.HandleFunc("/rc", core.ReceiveHandler) // 发送zip文件 http.HandleFunc("/sendZip", core.SendZip) // 系统监控 http.HandleFunc("/sysinfo", core.SysMonitor) //保存监听的服务器 http.HandleFunc("/scdb", core.Settdb) // 可使用的服务器 http.HandleFunc("/sclist", core.Serlist) // 服务器详情 http.HandleFunc("/scdetail", core.Scdetail) // 编辑 http.HandleFunc("/scedit", core.Scedit) // 文件列表 http.HandleFunc("/flist", core.Flist) // 内存信息 http.HandleFunc("/dtmem", core.Dtmem) // 启动HTTP服务器并监听端口 80,如果出现错误,则打印错误信息并退出 fmt.Printf("Starting server at port:%v\n", config.G.Port) sport := fmt.Sprintf(":%v", config.G.Port) if err := http.ListenAndServe(sport, nil); err != nil { log.Fatal(err) } } func main() { // 执行的时候,没有附带参数默认为客户端 // 获取ip地址 lcip, err := config.GetLocalIP() // 错误异常不为空,就输出 if err != nil { fmt.Printf("get server ip faild %v", err) } // 判断输入 args := os.Args if args == nil || len(args) < 2 { // 编程客户端 fmt.Printf("run as client\n") // 启动web startWeb() // 提示 os.Exit(1) } config.G.LocalIP = lcip // 监控目录 flag := args[1] if flag == "-t" { wkdir := args[2] // 赋值给全局 config.G.FilePath = wkdir } else { // curdir, err := os.Getwd() if err != nil { fmt.Printf("initlizer faild %v", err) } // current work directory config.G.FilePath = curdir } // 使用帮助 if flag == "-h" { fmt.Printf("Usage: ./fss -t /home/\n") os.Exit(1) } // 初始化加密证书 if flag == "-mc" { bits := 2048 password := "xc1123" privateKeyPath := "private.pem" publicKeyPath := "public.pem" err := crypto.GenerateAndSaveKeysWithPassword(bits, password, privateKeyPath, publicKeyPath) if err != nil { fmt.Printf("错误: %v\n", err) return } fmt.Println("密钥生成完成!") } // args 长度大于4 才有意思 if len(args) > 4 { config.G.Port = args[4] // 监控端口 tpflag := args[3] if tpflag == "-p" { config.G.Port = args[4] } } else { config.G.Port = "9099" } // web service startWeb() // 监听关闭的信号 ch := make(chan os.Signal, 2) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) sign, ok := <-ch if ok { msg := "OS Signal received: " + sign.String() log.Println(msg) os.Exit(0) } // // log.Println("exit AUFS application") }