feat: 添加web
This commit is contained in:
1 parent
2605f9bdb8
commit
71669aa380
3 files changed
+37
-56
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
web/*
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "net/http"
|
"embed"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
// "path/filepath"
|
"strings"
|
||||||
// "io/ioutil"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
@@ -12,13 +14,12 @@ import (
|
|||||||
"miao-directory/fsOperateEventCenter"
|
"miao-directory/fsOperateEventCenter"
|
||||||
)
|
)
|
||||||
|
|
||||||
var staticPath = "./"
|
//go:embed web/*
|
||||||
|
var webFS embed.FS
|
||||||
|
|
||||||
var port = ":17705"
|
var port = ":17705"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if _, err := os.Stat(staticPath); os.IsNotExist(err) {
|
|
||||||
os.Mkdir(staticPath, os.ModePerm)
|
|
||||||
}
|
|
||||||
fsOperateEventCenter := fsOperateEventCenter.NewFsOperateEventCenter()
|
fsOperateEventCenter := fsOperateEventCenter.NewFsOperateEventCenter()
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
@@ -33,7 +34,20 @@ func main() {
|
|||||||
router.POST("/rename/*path", api.RenameHandler(fsOperateEventCenter))
|
router.POST("/rename/*path", api.RenameHandler(fsOperateEventCenter))
|
||||||
router.POST("/delete/*path", api.DeleteHandler(fsOperateEventCenter))
|
router.POST("/delete/*path", api.DeleteHandler(fsOperateEventCenter))
|
||||||
|
|
||||||
router.Run(port)
|
// Redirect from '/' to '/web/'
|
||||||
|
router.GET("/", func(c *gin.Context) {
|
||||||
|
c.Redirect(http.StatusMovedPermanently, "/web/")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Serve static files from the embedded filesystem
|
||||||
|
staticFS := http.FS(webFS)
|
||||||
|
router.NoRoute(func(c *gin.Context) {
|
||||||
|
http.FileServer(staticFS).ServeHTTP(c.Writer, c.Request)
|
||||||
|
})
|
||||||
|
|
||||||
|
address := "[::]" + port
|
||||||
|
printListeningAddresses(port)
|
||||||
|
router.Run(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
func corsMiddleware() gin.HandlerFunc {
|
func corsMiddleware() gin.HandlerFunc {
|
||||||
@@ -43,51 +57,16 @@ func corsMiddleware() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func getHandler(c *gin.Context) {
|
func printListeningAddresses(port string) {
|
||||||
// decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
host, _ := os.Hostname()
|
||||||
// if err != nil {
|
addrs, _ := net.LookupHost(host)
|
||||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"})
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fileInfo, err := os.Stat(decodedPath)
|
fmt.Printf("Listening on:\n")
|
||||||
// if os.IsNotExist(err) {
|
fmt.Printf(" ➜ Local: http://localhost%s\n", port)
|
||||||
// c.JSON(http.StatusNotFound, gin.H{"error": "Path not found"})
|
for _, addr := range addrs {
|
||||||
// return
|
if strings.Contains(addr, "%") {
|
||||||
// }
|
addr = strings.Split(addr, "%")[0]
|
||||||
|
}
|
||||||
// if fileInfo.IsDir() {
|
fmt.Printf(" ➜ Network: http://%s%s\n", addr, port)
|
||||||
// files, err := ioutil.ReadDir(decodedPath)
|
}
|
||||||
// if err != nil {
|
}
|
||||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to scan directory"})
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var fileDetails []gin.H
|
|
||||||
// for _, file := range files {
|
|
||||||
// fileDetails = append(fileDetails, gin.H{
|
|
||||||
// "name": file.Name(),
|
|
||||||
// "size": file.Size(),
|
|
||||||
// "type": func() string {
|
|
||||||
// if file.IsDir() {
|
|
||||||
// return "directory"
|
|
||||||
// }
|
|
||||||
// return "file"
|
|
||||||
// }(),
|
|
||||||
// "stats": gin.H{
|
|
||||||
// "atimeMs": file.ModTime().UnixNano() / 1e6,
|
|
||||||
// "birthtimeMs": file.ModTime().UnixNano() / 1e6,
|
|
||||||
// "ctimeMs": file.ModTime().UnixNano() / 1e6,
|
|
||||||
// "mtimeMs": file.ModTime().UnixNano() / 1e6,
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// if len(fileDetails) == 0 {
|
|
||||||
// c.JSON(http.StatusOK, []gin.H{})
|
|
||||||
// } else {
|
|
||||||
// c.JSON(http.StatusOK, fileDetails)
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// c.File(decodedPath)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
Reference in new issue
Block a user