go_dreamfactory/cmd/upgrade/main.go
2022-08-12 18:53:45 +08:00

168 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"embed"
"flag"
"fmt"
"go_dreamfactory/cmd/upgrade/tools"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
// func main() {
// http.HandleFunc("/download", FileDownload)
// http.HandleFunc("/upload", FileUpload)
// log.Print("http server start")
// if err := http.ListenAndServe(":8910", nil); err != nil {
// log.Fatal(err)
// }
// }
func FileUpload(w http.ResponseWriter, r *http.Request) {
//获取文件流,第三个返回值是错误对象
file, header, _ := r.FormFile("file")
//读取文件流为[]byte
b, _ := ioutil.ReadAll(file)
//把文件保存到指定位置
ioutil.WriteFile("/opt/upgrade", b, 0777)
//输出上传时文件名
fmt.Println("上传文件名:", header.Filename)
}
func FileDownload(w http.ResponseWriter, r *http.Request) {
filename := "E:\\projects\\workspace\\go_dreamfactory\\cmd\\v2\\RobotGUI.exe"
file, _ := os.Open(filename)
defer file.Close()
fileHeader := make([]byte, 512)
file.Read(fileHeader)
fileStat, _ := file.Stat()
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
w.Header().Set("Content-Type", http.DetectContentType(fileHeader))
w.Header().Set("Content-Length", strconv.FormatInt(fileStat.Size(), 10))
file.Seek(0, 0)
io.Copy(w, file)
return
}
//go:embed views/*
var f embed.FS
var version = "0.0.1"
func main() {
host := flag.String("host", "10.0.0.9", "Host")
port := flag.String("port", "8080", "Port")
uploadsDir := flag.String("uploadsDir", "./uploads", "上传文件存储地址")
wwwDir := flag.String("wwwDir", "./www", "www服务地址即解压地址")
flag.Parse()
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.Static("/prd", "./www")
tmpl := template.Must(template.New("").ParseFS(f, "views/*"))
r.SetHTMLTemplate(tmpl)
r.GET("/version", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"version": version,
})
})
r.GET("/", func(c *gin.Context) {
fmt.Println(tmpl.DefinedTemplates())
c.HTML(http.StatusOK, "index.html", nil)
})
r.POST("/upload", func(c *gin.Context) {
f, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}
//从上传的文件名获取到版本号
s := strings.SplitN(f.Filename, "-", 2)
if len(s) != 2 {
c.JSON(http.StatusBadRequest, gin.H{
"error": "上传的文件名中没有包含版本号 eg. update.zip-0.0.1",
})
return
}
version = s[1] //设置版本号
dst := path.Join(*uploadsDir, f.Filename)
err = c.SaveUploadedFile(f, dst)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
_, err = tools.Unzip(dst, *wwwDir)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
c.JSON(http.StatusOK, gin.H{
"msg": "upload success",
"url": strings.Join([]string{"http://" + *host + ":" + *port, "/prd/", strings.Split(f.Filename, ".")[0], "/index.html"}, ""),
})
})
r.GET("/dirs", func(c *gin.Context) {
dir, err := ioutil.ReadDir(*wwwDir)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
type Item struct {
Name string `json:"name"`
Url string `json:"url"`
DateTime string `json:"dateTime"`
}
var dirs []Item
for _, f := range dir {
// if f.IsDir() && len(f.Name()) > 0 && f.Name() != "__MACOSX" {
dirs = append(dirs, Item{
Name: f.Name(),
Url: strings.Join([]string{"http://" + *host + ":" + *port, "/prd/", f.Name()}, ""),
DateTime: f.ModTime().Format(time.RFC3339),
})
// }
}
c.JSON(http.StatusOK, gin.H{
"dirs": dirs,
})
})
err := r.Run("0.0.0.0:" + *port)
if err != nil {
log.Fatal(err)
}
}