added check nil ctx value
This commit is contained in:
parent
9942b81b71
commit
3664c04465
@ -1,108 +0,0 @@
|
|||||||
package minio
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/minio/minio-go/v7"
|
|
||||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
||||||
"github.com/themakers/hlog"
|
|
||||||
)
|
|
||||||
|
|
||||||
type BlobStore struct {
|
|
||||||
log hlog.Logger
|
|
||||||
store *minio.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
const bucket = "pair"
|
|
||||||
|
|
||||||
func New(ctx context.Context, logger hlog.Logger,
|
|
||||||
endpoint, keyID, accessKey, token, region string,
|
|
||||||
useSSL bool) (*BlobStore, error) {
|
|
||||||
conn, err := minio.New(endpoint,
|
|
||||||
&minio.Options{
|
|
||||||
Creds: credentials.NewStaticV4(keyID, accessKey, token),
|
|
||||||
Secure: false,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
fmt.Println("monio", keyID, accessKey, token, region)
|
|
||||||
|
|
||||||
bucketExists, err := conn.BucketExists(ctx, bucket)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !bucketExists {
|
|
||||||
if err := conn.MakeBucket(ctx, bucket, minio.MakeBucketOptions{}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &BlobStore{
|
|
||||||
log: logger.Module("minio"),
|
|
||||||
store: conn,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bs *BlobStore) PutFile(
|
|
||||||
ctx context.Context,
|
|
||||||
filename string,
|
|
||||||
reader io.Reader,
|
|
||||||
size int64) error {
|
|
||||||
info, err := bs.store.PutObject(ctx, bucket, filename, reader, size, minio.PutObjectOptions{
|
|
||||||
//UserMetadata: nil,
|
|
||||||
//UserTags: nil,
|
|
||||||
//Progress: nil,
|
|
||||||
//ContentType: "",
|
|
||||||
//ContentEncoding: "",
|
|
||||||
//ContentDisposition: "",
|
|
||||||
//ContentLanguage: "",
|
|
||||||
//CacheControl: "",
|
|
||||||
//Mode: "",
|
|
||||||
//RetainUntilDate: time.Time{},
|
|
||||||
//ServerSideEncryption: nil,
|
|
||||||
//NumThreads: 0,
|
|
||||||
//StorageClass: "",
|
|
||||||
//WebsiteRedirectLocation: "",
|
|
||||||
//PartSize: 0,
|
|
||||||
//LegalHold: "",
|
|
||||||
//SendContentMd5: false,
|
|
||||||
//DisableMultipart: false,
|
|
||||||
//Internal: minio.AdvancedPutOptions{},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println("info", info)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bs *BlobStore) DeleteFile(ctx context.Context, id string) error {
|
|
||||||
if err := bs.store.RemoveObject(ctx, bucket, id, minio.RemoveObjectOptions{
|
|
||||||
//GovernanceBypass: false,
|
|
||||||
//VersionID: "",
|
|
||||||
//Internal: AdvancedRemoveOptions
|
|
||||||
}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bs *BlobStore) FileExists(ctx context.Context, filename string) (bool, error) {
|
|
||||||
_, err := bs.store.StatObject(ctx, bucket, filename, minio.StatObjectOptions{})
|
|
||||||
if err != nil {
|
|
||||||
if minio.ToErrorResponse(err).StatusCode == http.StatusNotFound {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
1069
dal/mongo/dal.go
1069
dal/mongo/dal.go
File diff suppressed because it is too large
Load Diff
@ -1,102 +0,0 @@
|
|||||||
package mongo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"github.com/stretchr/testify/suite"
|
|
||||||
"github.com/themakers/hlog"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"heruvym/internal/model"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
var mongoUri = "mongodb://localmongo1:30001,localmongo2:30002,localmongo3:30003/?replicaSet=my-rs"
|
|
||||||
|
|
||||||
type AccountTestSuite struct {
|
|
||||||
args *model.Account
|
|
||||||
dal *DAL
|
|
||||||
suite.Suite
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAccountTestSuite(t *testing.T) {
|
|
||||||
suite.Run(t, new(AccountTestSuite))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) SetupSuite() {
|
|
||||||
s.args = &model.Account{
|
|
||||||
UserID: "fhka41hakfh41z",
|
|
||||||
Nickname: "SomeNick",
|
|
||||||
Role: "manager",
|
|
||||||
}
|
|
||||||
|
|
||||||
logger := hlog.New(zap.NewNop())
|
|
||||||
dal, err := New(context.Background(), mongoUri, "support", logger)
|
|
||||||
|
|
||||||
if s.NoError(err) {
|
|
||||||
s.dal = dal
|
|
||||||
result, err := s.dal.InsertAccount(context.Background(), s.args)
|
|
||||||
if s.NoError(err) {
|
|
||||||
s.args = result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TestGet() {
|
|
||||||
r, err := s.dal.GetAccount(context.Background(), s.args.ID)
|
|
||||||
s.NoError(err)
|
|
||||||
s.NotNil(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TestGetByUserID() {
|
|
||||||
r, err := s.dal.GetAccountByUserID(context.Background(), s.args.UserID)
|
|
||||||
s.NoError(err)
|
|
||||||
s.NotNil(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TestGetAccountPage() {
|
|
||||||
r, err := s.dal.GetAccountPage(context.Background(), "", 0, 10)
|
|
||||||
s.NoError(err)
|
|
||||||
s.NotNil(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TestSetAccountRole() {
|
|
||||||
arg := "admin"
|
|
||||||
r, err := s.dal.SetAccountRole(context.Background(), s.args.UserID, arg)
|
|
||||||
s.NoError(err)
|
|
||||||
if s.NotNil(r) {
|
|
||||||
s.Equal(arg, r.Role)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TestSetAccountNickname() {
|
|
||||||
arg := "NewNickname"
|
|
||||||
r, err := s.dal.SetAccountNickname(context.Background(), s.args.UserID, arg)
|
|
||||||
s.NoError(err)
|
|
||||||
if s.NotNil(r) {
|
|
||||||
s.Equal(arg, r.Nickname)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TestSetAccountAvatar() {
|
|
||||||
arg := "/new/avatar.jpeg"
|
|
||||||
r, err := s.dal.SetAccountAvatar(context.Background(), s.args.UserID, arg)
|
|
||||||
s.NoError(err)
|
|
||||||
if s.NotNil(r) {
|
|
||||||
s.Equal(arg, r.Avatar)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TestSetAccountDelete() {
|
|
||||||
arg := true
|
|
||||||
r, err := s.dal.SetAccountDelete(context.Background(), s.args.UserID, arg)
|
|
||||||
s.NoError(err)
|
|
||||||
if s.NotNil(r) {
|
|
||||||
s.Equal(arg, r.IsDeleted)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *AccountTestSuite) TearDownSuite() {
|
|
||||||
r, err := s.dal.DeleteAccount(context.Background(), s.args.UserID)
|
|
||||||
s.NoError(err)
|
|
||||||
s.NotNil(r)
|
|
||||||
}
|
|
@ -2,10 +2,16 @@ version: '3.8'
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
mongo:
|
mongo:
|
||||||
image: mongo:latest
|
image: mongo
|
||||||
container_name: mongo
|
|
||||||
ports:
|
ports:
|
||||||
- "27017:27017"
|
- "27020:27017"
|
||||||
|
environment:
|
||||||
|
- MONGO_INITDB_ROOT_USERNAME=test
|
||||||
|
- MONGO_INITDB_ROOT_PASSWORD=test
|
||||||
|
- MONGO_INITDB_AUTH_MECHANISM=SCRAM-SHA-1
|
||||||
|
volumes:
|
||||||
|
- mongo_data:/data/db
|
||||||
|
|
||||||
|
|
||||||
minio:
|
minio:
|
||||||
image: minio/minio
|
image: minio/minio
|
||||||
@ -32,5 +38,6 @@ services:
|
|||||||
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
mongo_data:
|
||||||
minio_storage:
|
minio_storage:
|
||||||
redis_data:
|
redis_data:
|
@ -94,6 +94,15 @@ func (o *OtherController) PutFile(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "not authorized"})
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "not authorized"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
form, err := ctx.MultipartForm()
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "can not parse multipart: " + err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
if form == nil || form.File == nil {
|
||||||
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "no multipart or file"})
|
||||||
|
}
|
||||||
|
|
||||||
TimeKey := fmt.Sprintf("sendLockHeruvym:%s", sess.Id)
|
TimeKey := fmt.Sprintf("sendLockHeruvym:%s", sess.Id)
|
||||||
isNewKey, err := o.redisClient.SetNX(ctx.Context(), TimeKey, time.Now().Unix(), 30*time.Second).Result()
|
isNewKey, err := o.redisClient.SetNX(ctx.Context(), TimeKey, time.Now().Unix(), 30*time.Second).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,15 +113,6 @@ func (o *OtherController) PutFile(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{"error": "file upload limit exceeded"})
|
return ctx.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{"error": "file upload limit exceeded"})
|
||||||
}
|
}
|
||||||
|
|
||||||
form, err := ctx.MultipartForm()
|
|
||||||
if err != nil {
|
|
||||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "can not parse multipart: " + err.Error()})
|
|
||||||
}
|
|
||||||
|
|
||||||
if form == nil || form.File == nil {
|
|
||||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "no multipart or file"})
|
|
||||||
}
|
|
||||||
|
|
||||||
files := form.File
|
files := form.File
|
||||||
for _, fileHeaders := range files {
|
for _, fileHeaders := range files {
|
||||||
for _, fileHeader := range fileHeaders {
|
for _, fileHeader := range fileHeaders {
|
||||||
@ -253,8 +253,8 @@ func (o *OtherController) PutFile(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "can not store message: " + err.Error()})
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "can not store message: " + err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
domain := ctx.Context().Value(middleware.HostKey).(string)
|
domain, ok := ctx.Context().Value(middleware.HostKey).(string)
|
||||||
if domain == "" {
|
if !ok || domain == "" {
|
||||||
fmt.Println("domain is nil err")
|
fmt.Println("domain is nil err")
|
||||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "domain is nil"})
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "domain is nil"})
|
||||||
}
|
}
|
||||||
|
@ -110,8 +110,8 @@ func (t *TicketController) CreateTicket(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
domain := ctx.Context().Value(middleware.HostKey).(string)
|
domain, ok := ctx.Context().Value(middleware.HostKey).(string)
|
||||||
if domain == "" {
|
if !ok || domain == "" {
|
||||||
t.zapLogger.Error("domain is nil err")
|
t.zapLogger.Error("domain is nil err")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,8 +178,8 @@ func (t *TicketController) PutMessage(ctx *fiber.Ctx) error {
|
|||||||
sess := jwt_adapter.Get(ctx.Context())
|
sess := jwt_adapter.Get(ctx.Context())
|
||||||
request.Files = []string{}
|
request.Files = []string{}
|
||||||
|
|
||||||
domain := ctx.Context().Value(middleware.HostKey).(string)
|
domain, ok := ctx.Context().Value(middleware.HostKey).(string)
|
||||||
if domain == "" {
|
if !ok || domain == "" {
|
||||||
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "domain is nil"})
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "domain is nil"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,9 +16,9 @@ type Config struct {
|
|||||||
NumberPortLocal string `env:"BB_PORT" envDefault:"1488"`
|
NumberPortLocal string `env:"BB_PORT" envDefault:"1488"`
|
||||||
AccountAddress string `env:"BB_AccountAddress" envDefault:":8931"`
|
AccountAddress string `env:"BB_AccountAddress" envDefault:":8931"`
|
||||||
LoggerDevMode bool `env:"BB_IS_PROD" envDefault:"false"`
|
LoggerDevMode bool `env:"BB_IS_PROD" envDefault:"false"`
|
||||||
MinioEndpoint string `env:"BB_MINIO_EP" envDefault:"localhost:9005"`
|
MinioEndpoint string `env:"BB_MINIO_EP" envDefault:"localhost:9000"`
|
||||||
MinioAccessKey string `env:"BB_MINIO_AK" envDefault:"admin"`
|
MinioAccessKey string `env:"BB_MINIO_AK" envDefault:"minioadmin"`
|
||||||
MinioSecretKey string `env:"BB_MINIO_SK" envDefault:"admin123"`
|
MinioSecretKey string `env:"BB_MINIO_SK" envDefault:"minioadmin"`
|
||||||
MinioRegion string `env:"S3_REGION" envDefault:""`
|
MinioRegion string `env:"S3_REGION" envDefault:""`
|
||||||
MinioToken string `env:"BB_MINIO_TOKEN" envDefault:""`
|
MinioToken string `env:"BB_MINIO_TOKEN" envDefault:""`
|
||||||
MongoDbTable string `env:"DATABASE_TABLE" envDefault:"profile"`
|
MongoDbTable string `env:"DATABASE_TABLE" envDefault:"profile"`
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetFileType(filename string) string {
|
func GetFileType(filename string) string {
|
||||||
|
return "image"
|
||||||
ext := strings.ToLower(filepath.Ext(filename))
|
ext := strings.ToLower(filepath.Ext(filename))
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".jpg", ".png":
|
case ".jpg", ".png":
|
||||||
|
10
main.go
10
main.go
@ -1,10 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/skeris/appInit"
|
|
||||||
"heruvym/app"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
appInit.Initialize(app.New, app.Options{})
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
package router
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewRouter(additional... map[string]http.HandlerFunc) *mux.Router {
|
|
||||||
apiMux := mux.NewRouter()
|
|
||||||
|
|
||||||
for _, handler := range additional {
|
|
||||||
for endpoint, handler := range handler {
|
|
||||||
apiMux.HandleFunc(endpoint, handler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return apiMux
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
type ErrorClose struct {
|
|
||||||
Err error
|
|
||||||
}
|
|
||||||
|
|
||||||
type ErrorMarshal struct {
|
|
||||||
Err error
|
|
||||||
}
|
|
||||||
|
|
||||||
type ErrorWrite struct {
|
|
||||||
Err error
|
|
||||||
}
|
|
1137
service/service.go
1137
service/service.go
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user