Skip to content

Commit

Permalink
web
Browse files Browse the repository at this point in the history
  • Loading branch information
danbai225 committed Jan 4, 2024
1 parent ba33cfc commit d8a36ab
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
42 changes: 41 additions & 1 deletion api_server/server.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,63 @@
package api_server

import (
"embed"
"fmt"
logs "github.com/danbai225/go-logs"
"github.com/gin-gonic/gin"
"go-rustdesk-server/common"
"go-rustdesk-server/data_server"
"go-rustdesk-server/web"
"net/http"
"path/filepath"
"strings"
)

func walkDir(fs embed.FS, dir string, group *gin.RouterGroup, handlerFunc gin.HandlerFunc) error {
entries, err := fs.ReadDir(dir)
if err != nil {
return err
}
for _, entry := range entries {
if entry.IsDir() {
// 递归遍历子目录
err = walkDir(fs, filepath.Join(dir, entry.Name()), group, handlerFunc)
if err != nil {
return err
}
} else {
// 处理文件
path := filepath.Join(strings.ReplaceAll(dir, "dist", ""), entry.Name())
group.GET(path, handlerFunc)
}
}
return nil
}
func Start() {
router := gin.Default()
// 前端路由组
frontendGroup := router.Group("/")
{
frontendGroup.GET("/", func(ctx *gin.Context) {
file, _ := web.Dist.ReadFile("/dist/index.html")
ctx.Data(200, "text/html; charset=utf-8", file)
})
_ = walkDir(web.Dist, "dist", frontendGroup, func(ctx *gin.Context) {
ctx.FileFromFS("/dist"+ctx.Request.URL.Path, http.FS(web.Dist))
})
}
// 后端路由组
apiGroup := router.Group("/api/v1")
apiGroup.POST("/login", login)
{
apiGroup.GET("/login", login)
}
// 启动Web服务器,并指定端口
err := router.Run(fmt.Sprintf(":%d", common.Conf.WebPort))
if err != nil {
logs.Err(err)
}
}

func login(ctx *gin.Context) {
db, err := data_server.GetDataSever()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions web/dist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package web

import (
"embed"
_ "embed"
)

//go:embed dist/*
var Dist embed.FS
10 changes: 10 additions & 0 deletions web/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

</body>
</html>
1 change: 1 addition & 0 deletions web/dist/js/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123

0 comments on commit d8a36ab

Please sign in to comment.