animeic-cd 1 năm trước cách đây
mục cha
commit
53753ea854
9 tập tin đã thay đổi với 76 bổ sung21 xóa
  1. 2 1
      src/.gitignore
  2. 3 5
      src/api/api.go
  3. 46 3
      src/api/upload.go
  4. 18 4
      src/api/version.go
  5. 1 1
      src/build.sh
  6. 1 1
      src/db/db.go
  7. 4 2
      src/db/model/versions.go
  8. 0 4
      src/go.mod
  9. 1 0
      src/package/readme.md

+ 2 - 1
src/.gitignore

@@ -1,4 +1,5 @@
 local*
 *.log
 images
-assets
+assets
+package

+ 3 - 5
src/api/api.go

@@ -8,8 +8,6 @@ import (
 	"fmt"
 
 	"github.com/gin-contrib/cors"
-	"github.com/gin-contrib/sessions"
-	"github.com/gin-contrib/sessions/cookie"
 	"github.com/gin-gonic/gin"
 	"github.com/go-redis/redis/v8"
 )
@@ -46,9 +44,9 @@ func NewHttpService(app *conf.AppConf, dbMongo *db.MongoDB, redisClient *redis.C
 
 	engine := gin.Default()
 
-	store := cookie.NewStore([]byte("spu3d-server"))
-	engine.Use(sessions.Sessions("dcsession", store))
-	engine.Static("/pkg", "package")
+	// store := cookie.NewStore([]byte("spu3d-server"))
+	// engine.Use(sessions.Sessions("dcsession", store))
+	engine.Static("/crsvc/pkg", "package")
 	config := cors.DefaultConfig()
 	// config.AllowOrigins == []string{"http://google.com", "http://facebook.com"}
 	config.AllowAllOrigins = true

+ 46 - 3
src/api/upload.go

@@ -1,11 +1,54 @@
 package api
 
-import "github.com/gin-gonic/gin"
+import (
+	"cr-svc/db/model"
+	"cr-svc/db/repo"
+	"fmt"
+	"path/filepath"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
 
 func Upload(r *GinRouter) {
-	r.POSTJWT("/upload/pkg", UploadPkg)
+	r.POST("/upload/pkg", UploadPkg)
 }
 
+var LocalHost = "http://localhost:8085/cr/pkg"
+
+// type UploadReq struct {
+// 	Version string
+// 	Name    string
+// }
+
 func UploadPkg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	return nil, nil
+
+	version := c.PostForm("version")
+	name := c.PostForm("name")
+	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
+	file, err := c.FormFile("file")
+	if err != nil {
+		return nil, err
+	}
+
+	size := fmt.Sprintf("%.2fM", float64(file.Size)/float64(1024*1024))
+	fmt.Println(size)
+
+	basePath := "./package/"
+	filename := basePath + filepath.Base(file.Filename)
+	if err := c.SaveUploadedFile(file, filename); err != nil {
+		return nil, err
+	}
+	versions := &model.Versions{
+		UserId:     userId,
+		UserName:   apictx.User.Name,
+		Name:       name,
+		Url:        fmt.Sprintf("%s/%s", LocalHost, file.Filename),
+		Size:       size,
+		Version:    version,
+		CreateTime: time.Now(),
+		UpdateTime: time.Now(),
+	}
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionVersions, versions)
 }

+ 18 - 4
src/api/version.go

@@ -1,7 +1,9 @@
 package api
 
 import (
-	"fmt"
+	"cr-svc/db/model"
+	"cr-svc/db/repo"
+	"errors"
 
 	"github.com/gin-gonic/gin"
 )
@@ -12,10 +14,22 @@ func Version(r *GinRouter) {
 }
 
 func GetVersion(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	fmt.Println("succ")
-	return nil, nil
+	page, size, query := UtilQueryPageSize(c)
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		CollectName: repo.CollectionVersions,
+		Page:        page,
+		Size:        size,
+		Query:       query,
+	})
 }
 
 func VersionLatest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	return nil, nil
+	version := &model.Versions{}
+	ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionVersions,
+	}, version)
+	if !ok || err != nil {
+		return nil, errors.New("未找到数据")
+	}
+	return version, nil
 }

+ 1 - 1
src/build.sh

@@ -2,7 +2,7 @@
 
 # 编译应用
 echo "building..."
-go build -o pack-cr
+go build -o cr-svc
 
 # 命名镜像
 local_imge="pack-cr:v1.0.0"

+ 1 - 1
src/db/db.go

@@ -36,7 +36,7 @@ func (db *MongoDB) GetOrCreateDatabase(name string) *mongo.Database {
 
 func NewMongoDB(bus *comm.NatsBus) *MongoDB {
 
-	inst, err := bus.NewMongoDBFromConfig("cr-mongo")
+	inst, err := bus.NewMongoDBFromConfigDev("cr-mongo")
 
 	if err != nil {
 		panic(err)

+ 4 - 2
src/db/model/versions.go

@@ -8,9 +8,11 @@ import (
 
 // 版本管理
 type Versions struct {
-	Id     primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	UserId primitive.ObjectID `bson:"userId,omitempty" json:"userId"`
+	Id       primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	UserId   primitive.ObjectID `bson:"userId,omitempty" json:"userId"`
+	UserName string             `bson:"userName,omitempty" json:"userName"`
 
+	Name    string `bson:"name,omitempty" json:"name"`       // 名字
 	Size    string `bson:"size,omitempty" json:"size"`       // 大小 2000M
 	Url     string `bson:"url,omitempty" json:"url"`         // 安装包地址
 	Version string `bson:"version,omitempty" json:"version"` // 版本 1.0.0

+ 0 - 4
src/go.mod

@@ -5,7 +5,6 @@ go 1.19
 require (
 	github.com/appleboy/gin-jwt/v2 v2.7.0
 	github.com/gin-contrib/cors v1.3.1
-	github.com/gin-contrib/sessions v0.0.3
 	github.com/gin-gonic/gin v1.7.4
 	github.com/go-redis/redis/v8 v8.11.5
 	github.com/natefinch/lumberjack v2.0.0+incompatible
@@ -31,9 +30,6 @@ require (
 	github.com/golang-migrate/migrate/v4 v4.15.2 // indirect
 	github.com/golang/protobuf v1.5.2 // indirect
 	github.com/golang/snappy v0.0.4 // indirect
-	github.com/gorilla/context v1.1.1 // indirect
-	github.com/gorilla/securecookie v1.1.1 // indirect
-	github.com/gorilla/sessions v1.1.3 // indirect
 	github.com/hashicorp/errwrap v1.1.0 // indirect
 	github.com/hashicorp/go-multierror v1.1.1 // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect

+ 1 - 0
src/package/readme.md

@@ -0,0 +1 @@
+# 存放安装包