Browse Source

动态宿主ip

animeic-cd 1 year ago
parent
commit
e5fdc6b873
7 changed files with 51 additions and 11 deletions
  1. 1 2
      src/api/upload.go
  2. 17 2
      src/api/version.go
  3. 3 0
      src/app.yaml
  4. 12 6
      src/conf/app.go
  5. 17 0
      src/conf/readfile.go
  6. 0 1
      src/main.go
  7. 1 0
      src/url

+ 1 - 2
src/api/upload.go

@@ -1,7 +1,6 @@
 package api
 
 import (
-	"cr-svc/conf"
 	"cr-svc/db/model"
 	"cr-svc/db/repo"
 	"fmt"
@@ -38,7 +37,7 @@ func UploadPkg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		UserId:     userId,
 		UserName:   apictx.User.Name,
 		Name:       name,
-		Url:        fmt.Sprintf("%s/%s", conf.AppConfig.UploadPkgUrl, file.Filename),
+		Url:        fmt.Sprintf("/cr/pkg/%s", file.Filename),
 		Size:       size,
 		Version:    version,
 		CreateTime: time.Now(),

+ 17 - 2
src/api/version.go

@@ -1,10 +1,12 @@
 package api
 
 import (
+	"cr-svc/conf"
 	"cr-svc/db/model"
 	"cr-svc/db/repo"
 	"cr-svc/log"
 	"errors"
+	"fmt"
 
 	"github.com/gin-gonic/gin"
 	"go.mongodb.org/mongo-driver/bson"
@@ -20,12 +22,22 @@ func Version(r *GinRouter) {
 
 func GetVersion(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
-	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionVersions,
 		Page:        page,
 		Size:        size,
 		Query:       query,
 	})
+	if err != nil {
+		return nil, err
+	}
+	if len(result.List) > 0 {
+		for _, v := range result.List {
+			url := v["url"]
+			v["url"] = fmt.Sprintf("http://%s:%d%s", conf.AppConfig.MasterIp, conf.AppConfig.ServerPort, url)
+		}
+	}
+	return result.List, nil
 }
 
 func VersionLatest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
@@ -41,7 +53,10 @@ func VersionLatest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if len(out.List) < 1 {
 		return map[string]interface{}{}, nil
 	}
-	return out.List[0], nil
+	latestVersion := out.List[0]
+	url := out.List[0]["url"]
+	latestVersion["url"] = fmt.Sprintf("http://%s:%d%s", conf.AppConfig.MasterIp, conf.AppConfig.ServerPort, url)
+	return latestVersion, nil
 }
 
 func UpdateVersion(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 3 - 0
src/app.yaml

@@ -1,6 +1,9 @@
 port: 6123
 name: crsvc
 version: 1.0.0
+masterIp: ""
+serverPort: 8085
+
 jwt: 
   timeoutHour: 168
   realm: crsvc

+ 12 - 6
src/conf/app.go

@@ -1,8 +1,10 @@
 package conf
 
 import (
+	"errors"
 	"fmt"
 	"os"
+	"strings"
 
 	"cr-svc/log"
 
@@ -18,6 +20,8 @@ type AppConf struct {
 		Key         string
 		TimeoutHour int
 	}
+	MasterIp   string
+	ServerPort int
 
 	Redis struct {
 		Addr     string
@@ -78,6 +82,14 @@ func NewAppConf(filePath string) (*AppConf, error) {
 	if err != nil {
 		return c, err
 	}
+	_masterIp, err := ReadMasterIp()
+	masterIp := strings.Replace(_masterIp, "\n", "", -1)
+	if err != nil || len(masterIp) < 1 {
+		fmt.Println(err)
+		return nil, errors.New("masterIp 获取错误")
+	}
+	fmt.Println("master ip =>:", masterIp)
+	c.MasterIp = masterIp
 
 	//初始化log
 	_ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level)
@@ -88,12 +100,6 @@ func NewAppConf(filePath string) (*AppConf, error) {
 	}
 	fmt.Println("Env NATS=>", natsHost)
 
-	uploadPkgUrl := os.Getenv("UPLOAD_PKG_URL")
-	if len(uploadPkgUrl) > 0 {
-		c.UploadPkgUrl = uploadPkgUrl
-	}
-	fmt.Println("upload url =>", uploadPkgUrl)
-
 	AppConfig = c
 	return c, nil
 }

+ 17 - 0
src/conf/readfile.go

@@ -0,0 +1,17 @@
+package conf
+
+import (
+	"errors"
+	"fmt"
+	"os"
+)
+
+func ReadMasterIp() (string, error) {
+	_url, err := os.ReadFile("url")
+	if err != nil {
+		fmt.Println(err)
+		return "", errors.New("读取文件失败")
+	}
+
+	return string(_url), nil
+}

+ 0 - 1
src/main.go

@@ -34,7 +34,6 @@ func main() {
 
 	flag.Parse()
 	app := BuildApp()
-
 	err := app.Invoke(func(svc *api.Service, bus *comm.NatsBus) error {
 		go bus.Run(nil)
 

+ 1 - 0
src/url

@@ -0,0 +1 @@
+127.0.0.1