From b58d6eed2447e851d0e5040986fb446990e0e607 Mon Sep 17 00:00:00 2001 From: woon Date: Sat, 11 Mar 2023 15:40:46 +0800 Subject: [PATCH] first commit --- .env copy | 2 + .gitignore | 2 + app.service.example | 14 ++++++ build.sh | 35 +++++++++++++ go.mod | 5 ++ go.sum | 2 + main.go | 114 +++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 24 +++++++++ 8 files changed, 198 insertions(+) create mode 100644 .env copy create mode 100644 .gitignore create mode 100644 app.service.example create mode 100644 build.sh create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 templates/index.html diff --git a/.env copy b/.env copy new file mode 100644 index 0000000..b1e806c --- /dev/null +++ b/.env copy @@ -0,0 +1,2 @@ +FILE_PATH=./data +PORT=1112 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c06317a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +/data \ No newline at end of file diff --git a/app.service.example b/app.service.example new file mode 100644 index 0000000..7e9c96b --- /dev/null +++ b/app.service.example @@ -0,0 +1,14 @@ +[Unit] +Description=go Application +After=network.target + +[Service] +User=appruner +WorkingDirectory=APP_PATH +ExecStart=APP_PATH/app +StandardOutput=file:APP_PATH/app.out +StandardError=file:APP_PATH/app.err +Restart=always + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..c6f6d96 --- /dev/null +++ b/build.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# 判断 app 用户是否存在 +if id "appruner" >/dev/null 2>&1; then + echo "User appruner exists." +else + # 创建 app 用户 + echo "Creating user appruner..." + useradd -m -s /bin/bash appruner +fi + + +# 获取当前目录的绝对路径 +app_path=$(cd "$(dirname "$0")"; pwd) +app_name=$(basename "${app_path}") + +# 执行 git pull 命令 +cd ${app_path} && git pull + +# 执行 go build 命令 +cd ${app_path} && go build -o app + +cp app.service.example ${app_name}.service + +# 替换 app.service 文件中的 APP_PATH +sed -i "s|APP_PATH|${app_path}|g" ${app_name}.service + +mv ${app_name}.service /etc/systemd/system/${app_name}.service + +# 重新加载 systemd 配置文件 +systemctl daemon-reload + +# 重新启动应用程序服务 +systemctl stop ${app_name}.service +systemctl start ${app_name}.service \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..493eba6 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module gin-file + +go 1.20 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..04c0fcb --- /dev/null +++ b/main.go @@ -0,0 +1,114 @@ +package main + +import ( + "fmt" + "html/template" + "net/http" + "os" + "path/filepath" + "strings" + + "github.com/joho/godotenv" +) + +// 定义静态文件根目录 +var rootPath string + +// FileInfo 包含文件的元数据信息 +type FileInfo struct { + Name string + Size string + IsDir bool + Href string +} + +func main() { + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading .env file") + return + } + rootPath = os.Getenv("FILE_PATH") + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + dir := r.URL.Path[1:] + + dir = rootPath + "/" + dir + + dir = strings.Replace(dir, "\\", "/", -1) + + fileInfo, err := os.Stat(dir) + + if err != nil { + //fmt.Println("File does not exist") + } else if fileInfo.Mode().IsRegular() { + file, _ := os.Open(dir) + defer file.Close() + http.ServeContent(w, r, fileInfo.Name(), fileInfo.ModTime(), file) + } else { + fileInfos, err := listDir(dir) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmpl, err := template.ParseFiles("templates/index.html") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + tmpl.Execute(w, fileInfos) + } + + }) + + http.ListenAndServe(":"+os.Getenv("PORT"), nil) +} + +func listDir(dir string) ([]FileInfo, error) { + file, err := os.Open(dir) + if err != nil { + return nil, err + } + defer file.Close() + + names, err := file.Readdirnames(-1) + if err != nil { + return nil, err + } + + var fileInfos []FileInfo + for _, name := range names { + fileInfo, err := os.Stat(filepath.Join(dir, name)) + if err != nil { + return nil, err + } + + Href := filepath.Join(dir, name) + Href = strings.Replace(Href, filepath.Join(rootPath), "", -1) + + size := fileInfo.Size() + formattedSize := formatFileSize(size) + + fileInfos = append(fileInfos, FileInfo{ + Name: name, + Size: formattedSize, + IsDir: fileInfo.IsDir(), + Href: Href, + }) + } + return fileInfos, nil +} + +func formatFileSize(size int64) string { + units := []string{"B", "KB", "MB", "GB"} + var unitIndex int + var fileSize float64 + for size > 1024 && unitIndex < len(units)-1 { + fileSize = float64(size) / 1024 + size = size / 1024 + unitIndex++ + } + fileSize = float64(size) + return fmt.Sprintf("%.2f %s", fileSize, units[unitIndex]) +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..a47b044 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,24 @@ + + + File Browser + + + + + + + + + {{range .}} + + {{if .IsDir}} + + {{else}} + + {{end}} + + + {{end}} +
NameSize
/{{.Name}}{{.Name}}{{.Size}}
+ + \ No newline at end of file