first commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
.env
|
||||
/data
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,5 @@
|
||||
module gin-file
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/joho/godotenv v1.5.1 // indirect
|
||||
@@ -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=
|
||||
@@ -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])
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>File Browser</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Size</th>
|
||||
</tr>
|
||||
{{range .}}
|
||||
<tr>
|
||||
{{if .IsDir}}
|
||||
<td><a href="/{{.Href}}">/{{.Name}}</a></td>
|
||||
{{else}}
|
||||
<td><a href="/{{.Href}}">{{.Name}}</a></td>
|
||||
{{end}}
|
||||
<td>{{.Size}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user