Skip to content

Commit

Permalink
feature: runtime 静态文件收集问题修复
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshuaikang committed Nov 11, 2023
1 parent 2642272 commit 5d5a0ca
Show file tree
Hide file tree
Showing 92 changed files with 93 additions and 119 deletions.
16 changes: 14 additions & 2 deletions conf/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ rabbitmq_host = ${RABBITMQ_HOST}
rabbitmq_user = ${RABBITMQ_USER}
rabbitmq_password = ${RABBITMQ_PASSWORD}
store_backend = ${STORE_BACKEND}
api_gateway_file_path = ${API_GATEWAY_FILE_PATH}
`

var Settings config.Configer
Expand All @@ -74,6 +76,8 @@ var apigwManagerMaintainers string
var userTokenKeyName string
var pluginApiDebugUsername string

var apiGatewayFilePath string

func IsDevMode() bool {
return Settings.DefaultString("environment", "dev") == "dev"
}
Expand Down Expand Up @@ -123,6 +127,14 @@ func ApigwBackendHost() string {
return apigwBackendHost
}

func initApigwFilePath() {
apiGatewayFilePath = Settings.DefaultString("api_gateway_file_path", "./data")
}

func ApigwFilePath() string {
return apiGatewayFilePath
}

func initRedisAddr() {
redisAddr = fmt.Sprintf(
"%v:%v",
Expand Down Expand Up @@ -306,5 +318,5 @@ func init() {
initUserTokenKeyName()
initPluginApiDebugUsername()
setupLog()

initApigwFilePath()
}
95 changes: 72 additions & 23 deletions runner/collectstatics.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,90 @@
package runner

import (
"log"
"os/exec"

"github.com/TencentBlueKing/beego-runtime/utils"
"embed"
"github.com/TencentBlueKing/beego-runtime/conf"
"io/fs"
"os"
"path/filepath"
)

func runCollectstatics() {
staticDir, err := utils.GetStaticDirPath()
if err != nil {
log.Fatalf("get static files dir failed: %v\n", err)
//go:embed views
var views embed.FS

//go:embed static
var static embed.FS

//go:embed data
var apiGwData embed.FS

func copyFiles(sourceDir, targetDir string, dirEntries []fs.DirEntry, Fs embed.FS) {
for _, entry := range dirEntries {
sourcePath := filepath.Join(sourceDir, entry.Name())
targetPath := filepath.Join(targetDir, entry.Name())

if entry.IsDir() {
// 如果是文件夹,则递归拷贝文件夹中的内容
subDirEntries, err := Fs.ReadDir(sourcePath)
if err != nil {
panic(err)
}
err = os.MkdirAll(targetPath, 0755)
if err != nil {
panic(err)
}
copyFiles(sourcePath, targetPath, subDirEntries, Fs)
} else {
// 如果是文件,则拷贝文件内容
fileData, err := Fs.ReadFile(sourcePath)
if err != nil {
panic(err)
}
err = os.WriteFile(targetPath, fileData, 0644)
if err != nil {
panic(err)
}
}
}
viewPath, err := utils.GetViewPath()
}

func syncFile() {
ViewDirEntries, err := views.ReadDir("views")
if err != nil {
log.Fatalf("get view path failed: %v\n", err)
panic(err)
}
definitionPath, err := utils.GetApigwDefinitionPath()
StaticDirEntries, err := static.ReadDir("static")
if err != nil {
log.Fatalf("get apigw definition path failed: %v\n", err)
panic(err)
}
resourcesPath, err := utils.GetApigwResourcesPath()

err = os.MkdirAll("./views", 0755)
if err != nil {
log.Fatalf("get apigw resources path failed: %v\n", err)
panic(err)
}

cmds := []*exec.Cmd{
exec.Command("cp", "-r", staticDir, "."),
exec.Command("cp", "-r", viewPath, "."),
exec.Command("cp", "-r", definitionPath, "."),
exec.Command("cp", "-r", resourcesPath, "."),
err = os.MkdirAll("./static", 0755)
if err != nil {
panic(err)
}

for _, c := range cmds {
log.Printf("run collect static command: %v\n", c)
if err := c.Run(); err != nil {
log.Fatalf("collect static failed: %v\n", err)
copyFiles("views", "./views", ViewDirEntries, views)
copyFiles("static", "./static", StaticDirEntries, static)

if !conf.IsDevMode() {

err = os.MkdirAll("./data", 0755)
if err != nil {
panic(err)
}

ApiGwDirEntries, err := apiGwData.ReadDir("data")
if err != nil {
panic(err)
}
copyFiles("data", "./data", ApiGwDirEntries, apiGwData)
}
}

func runCollectstatics() {
syncFile()
}
File renamed without changes.
File renamed without changes.
21 changes: 2 additions & 19 deletions runner/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,15 @@ import (

"github.com/TencentBlueKing/beego-runtime/conf"
"github.com/TencentBlueKing/beego-runtime/routers"
runtimeUtils "github.com/TencentBlueKing/beego-runtime/utils"
"github.com/beego/beego/v2/server/web"
)

func runServer() {
var staticDir string
var viewPath string
var err error
if conf.IsDevMode() {
staticDir, err = runtimeUtils.GetStaticDirPath()
if err != nil {
log.Fatalf("get static files dir failed: %v\n", err)
}
} else {
staticDir = "static"
}
staticDir = "static"
log.Printf("serve /static at %v\n", staticDir)

if conf.IsDevMode() {
viewPath, err = runtimeUtils.GetViewPath()
if err != nil {
log.Fatalf("get view path failed: %v\n", err)
}
} else {
viewPath = "views"
}
viewPath = "views"
log.Printf("serve views at %v\n", viewPath)

web.BConfig.CopyRequestBody = true
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
14 changes: 5 additions & 9 deletions runner/syncapigw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/TencentBlueKing/beego-runtime/conf"
"github.com/TencentBlueKing/beego-runtime/utils"
"github.com/TencentBlueKing/bk-apigateway-sdks/core/bkapi"
"github.com/TencentBlueKing/bk-apigateway-sdks/manager"
"github.com/sirupsen/logrus"
Expand All @@ -15,14 +14,11 @@ import (
func runSyncApigw() {
logger := logrus.New()
// load data path
definitionPath, err := utils.GetApigwDefinitionPath()
if err != nil {
log.Fatalf("get apigw definition path error: %v\n", err)
}
resourcesPath, err := utils.GetApigwResourcesPath()
if err != nil {
log.Fatalf("get apigw resources path error: %v\n", err)
}

apiGwFilePath := conf.ApigwFilePath()

definitionPath := fmt.Sprintf("%s/%s", apiGwFilePath, "api-definition.yml")
resourcesPath := fmt.Sprintf("%s/%s", apiGwFilePath, "api-resources.yml")

// create manager
config := bkapi.ClientConfig{
Expand Down
File renamed without changes.
66 changes: 0 additions & 66 deletions utils/module.go
Original file line number Diff line number Diff line change
@@ -1,67 +1 @@
package utils

import (
"os"
"path"

"github.com/TencentBlueKing/beego-runtime/info"
"golang.org/x/mod/module"
)

func GetApigwDefinitionPath() (string, error) {
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version())
if err != nil {
return "", err
}

return path.Join(baseDir, "data/api-definition.yml"), nil
}

func GetApigwResourcesPath() (string, error) {
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version())
if err != nil {
return "", err
}

return path.Join(baseDir, "data/api-resources.yml"), nil
}

func GetStaticDirPath() (string, error) {
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version())
if err != nil {
return "", err
}

return path.Join(baseDir, "static"), nil
}

func GetViewPath() (string, error) {
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version())
if err != nil {
return "", err
}

return path.Join(baseDir, "views"), nil
}

func GetModulePath(name, version string) (string, error) {
// first we need GOMODCACHE
cache, ok := os.LookupEnv("GOMODCACHE")
if !ok {
cache = path.Join(os.Getenv("GOPATH"), "pkg", "mod")
}

// then we need to escape path
escapedPath, err := module.EscapePath(name)
if err != nil {
return "", err
}

// version also
escapedVersion, err := module.EscapeVersion(version)
if err != nil {
return "", err
}

return path.Join(cache, escapedPath+"@"+escapedVersion), nil
}

0 comments on commit 5d5a0ca

Please sign in to comment.