正在显示
37 个修改的文件
包含
2114 行增加
和
0 行删除
.gitignore
0 → 100644
| 1 | +# Binaries for programs and plugins | ||
| 2 | +*.exe | ||
| 3 | +*.dll | ||
| 4 | +*.so | ||
| 5 | +*.dylib | ||
| 6 | +*.vscode | ||
| 7 | + | ||
| 8 | +# Test binary, build with `go test -c` | ||
| 9 | +*.test | ||
| 10 | + | ||
| 11 | +# Output of the go coverage tool, specifically when used with LiteIDE | ||
| 12 | +*.out | ||
| 13 | + | ||
| 14 | +*.log | ||
| 15 | +*debug | ||
| 16 | +*wasm | ||
| 17 | + | ||
| 18 | +*.idea | ||
| 19 | + | ||
| 20 | +*.tmp | ||
| 21 | + | ||
| 22 | +*.sum | ||
| 23 | + | ||
| 24 | +opp | ||
| 25 | +/vendor | ||
| 26 | +/*.exe~ |
.idea/vcs.xml
0 → 100644
Dockerfile
0 → 100644
| 1 | +FROM 192.168.0.243:5000/mmm/mmmopp:20200226 as builder | ||
| 2 | +#FROM golang:1.13 as builder | ||
| 3 | +ENV GOPROXY https://goproxy.cn | ||
| 4 | +ENV GO111MODULE on | ||
| 5 | +ENV GOPATH /go | ||
| 6 | +RUN cd /go/src/gocomm \ | ||
| 7 | + && git pull | ||
| 8 | +WORKDIR /go/src/openapi | ||
| 9 | + | ||
| 10 | +COPY go.mod . | ||
| 11 | + | ||
| 12 | +COPY . . | ||
| 13 | +RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -o opp main.go | ||
| 14 | + | ||
| 15 | +#FROM alpine:latest | ||
| 16 | +FROM 192.168.0.243:5000/mmm/mmmopp:20200110 | ||
| 17 | +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ | ||
| 18 | + && apk add --no-cache tzdata \ | ||
| 19 | + && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ | ||
| 20 | + && echo "Asia/Shanghai" > /etc/timezone \ | ||
| 21 | + && rm -rf /var/cache/apk/* /tmp/* /var/tmp/* $HOME/.cache ## 清除缓存 | ||
| 22 | +WORKDIR /root/ | ||
| 23 | +COPY --from=builder /go/src/opp . | ||
| 24 | + | ||
| 25 | +EXPOSE 8080 | ||
| 26 | +CMD ["./openapi"] |
conf/app.conf
0 → 100644
| 1 | +appname = open-api | ||
| 2 | +httpport = 8080 | ||
| 3 | +runmode = "${RUN_MODE||dev}" | ||
| 4 | + | ||
| 5 | +#没设置的话 this.Ctx.Input.RequestBody 没有值 | ||
| 6 | +copyrequestbody = true | ||
| 7 | + | ||
| 8 | +#enablexsrf = true | ||
| 9 | + | ||
| 10 | +#开启应用内文档开关 | ||
| 11 | +EnableDocs = true | ||
| 12 | + | ||
| 13 | +include "dev.conf" | ||
| 14 | +include "dev-online.conf" | ||
| 15 | +include "test.conf" | ||
| 16 | +include "prod.conf" | ||
| 17 | + | ||
| 18 | + |
conf/dev-online.conf
0 → 100644
conf/dev.conf
0 → 100644
conf/prod.conf
0 → 100644
conf/test.conf
0 → 100644
controllers/base.go
0 → 100644
| 1 | +package controllers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "crypto/sha256" | ||
| 5 | + "encoding/hex" | ||
| 6 | + "encoding/json" | ||
| 7 | + "fmt" | ||
| 8 | + "github.com/astaxie/beego/plugins/cors" | ||
| 9 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/time" | ||
| 10 | + "openapi/protocol" | ||
| 11 | + "strconv" | ||
| 12 | + "strings" | ||
| 13 | + | ||
| 14 | + "github.com/astaxie/beego/context" | ||
| 15 | + "github.com/astaxie/beego/validation" | ||
| 16 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | ||
| 17 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 18 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
| 19 | +) | ||
| 20 | + | ||
| 21 | +type BaseController struct { | ||
| 22 | + Header *protocol.RequestHeader | ||
| 23 | + mybeego.BaseController | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +func init() { | ||
| 27 | + | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +var DefaultController *BaseController = &BaseController{} | ||
| 31 | + | ||
| 32 | +//Valid valid struct | ||
| 33 | +func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) { | ||
| 34 | + /*校验*/ | ||
| 35 | + var err error | ||
| 36 | + valid := validation.Validation{} | ||
| 37 | + result, err = valid.Valid(obj) | ||
| 38 | + if err != nil { | ||
| 39 | + } | ||
| 40 | + if !result { | ||
| 41 | + for _, err := range valid.Errors { | ||
| 42 | + if strings.HasSuffix(err.Key, ".Mobile") { | ||
| 43 | + msg = protocol.BadRequestParam(2001) | ||
| 44 | + return | ||
| 45 | + } | ||
| 46 | + log.Error(err.Key, err.Message) | ||
| 47 | + } | ||
| 48 | + msg = protocol.BadRequestParam(2) | ||
| 49 | + return | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + return | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +func (this *BaseController) Resp(msg *protocol.ResponseMessage) { | ||
| 56 | + this.Data["json"] = msg | ||
| 57 | + this.ServeJSON() | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +func (this *BaseController) RespH5(msg *protocol.ResponseMessage) { | ||
| 61 | + if msg.Errno != 0 { | ||
| 62 | + msg.Errno = -1 | ||
| 63 | + } | ||
| 64 | + this.Data["json"] = msg | ||
| 65 | + this.ServeJSON() | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +//获取请求头信息 | ||
| 69 | +func GetRequestHeader(ctx *context.Context) *protocol.RequestHeader { | ||
| 70 | + h := &protocol.RequestHeader{} | ||
| 71 | + h.AccessToken = ctx.Input.Header("x-mmm-accesstoken") | ||
| 72 | + h.AppProject = ctx.Input.Header("x-mmm-appproject") | ||
| 73 | + h.DeviceType, _ = strconv.Atoi(ctx.Input.Header("x-mmm-devicetype")) | ||
| 74 | + h.Sign = ctx.Input.Header("x-mmm-sign") | ||
| 75 | + h.Uuid = ctx.Input.Header("x-mmm-uuid") | ||
| 76 | + h.TimeStamp = ctx.Input.Header("x-mmm-timestamp") | ||
| 77 | + h.Version = ctx.Input.Header("x-mmm-version") | ||
| 78 | + return h | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +//过滤器 | ||
| 82 | +func FilterComm(ctx *context.Context) { | ||
| 83 | + if !CheckSign(ctx) { | ||
| 84 | + return | ||
| 85 | + } | ||
| 86 | + return | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +//检查签名 | ||
| 90 | +func CheckSign(ctx *context.Context) (result bool) { | ||
| 91 | + var ( | ||
| 92 | + h *protocol.RequestHeader | ||
| 93 | + sign string | ||
| 94 | + signHex string | ||
| 95 | + ) | ||
| 96 | + result = true | ||
| 97 | + h = GetRequestHeader(ctx) | ||
| 98 | + //1.检查签名 | ||
| 99 | + sign = fmt.Sprintf("v!(MmM%v%v%vMmM)i^", h.TimeStamp, h.Uuid, h.AccessToken) | ||
| 100 | + sha256 := sha256.New() | ||
| 101 | + sha256.Write([]byte(sign)) | ||
| 102 | + signHex = hex.EncodeToString(sha256.Sum(nil)) | ||
| 103 | + if strings.Compare(signHex, h.Sign) != 0 { | ||
| 104 | + msg := protocol.BadRequestParam(113) | ||
| 105 | + log.Error(fmt.Sprintf("%v req:%v resp:%v %v", ctx.Request.RequestURI, common.AssertJson(h), common.AssertJson(msg), signHex)) | ||
| 106 | + ctx.Output.JSON(msg, false, false) | ||
| 107 | + result = false | ||
| 108 | + return | ||
| 109 | + } | ||
| 110 | + return | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +//AllowOption 允许跨域请求 | ||
| 114 | +var AllowOption = func(ctx *context.Context) { | ||
| 115 | + if ctx.Request.Method != "OPTIONS" { | ||
| 116 | + return | ||
| 117 | + } | ||
| 118 | + f := cors.Allow(&cors.Options{ | ||
| 119 | + AllowMethods: []string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}, //允许的请求类型 | ||
| 120 | + AllowHeaders: []string{"Origin", "Accept", "Content-Type", "Authorization", | ||
| 121 | + "x-mmm-cid", "x-mmm-uid", "x-mmm-accesstoken", "x-mmm-refreshtoken", "x-requested-with"}, //允许的头部信息 | ||
| 122 | + ExposeHeaders: []string{"Content-Length"}, //允许暴露的头信息 | ||
| 123 | + AllowCredentials: false, //不允许共享AuthTuffic证书 | ||
| 124 | + AllowAllOrigins: true, //允许的请求来源 | ||
| 125 | + }) | ||
| 126 | + f(ctx) | ||
| 127 | + ctx.Output.Body([]byte("{}")) | ||
| 128 | + ctx.Output.SetStatus(204) | ||
| 129 | + return | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +//LogRequestData Before Router | ||
| 133 | +var LogRequestData = func(ctx *context.Context) { | ||
| 134 | + log.Info("====>Recv Request:%s", ctx.Input.URI()) | ||
| 135 | + hmap := map[string]string{ | ||
| 136 | + //protocol.HeaderAccessToken: ctx.Input.Header(protocol.HeaderAccessToken), | ||
| 137 | + //protocol.HeaderRefreshToken: ctx.Input.Header(protocol.HeaderRefreshToken), | ||
| 138 | + } | ||
| 139 | + if ctx.Input.RequestBody != nil { | ||
| 140 | + log.Info("====>Recv data from client:\nHeadData: %v \nBodyData: %s", hmap, string(ctx.Input.RequestBody)) | ||
| 141 | + } else { | ||
| 142 | + log.Info("====>Recv data from client:\nHeadData: %v ", hmap) | ||
| 143 | + } | ||
| 144 | +} | ||
| 145 | + | ||
| 146 | +func (this *BaseController) Prepare() { | ||
| 147 | + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | ||
| 148 | + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*") | ||
| 149 | + if this.Ctx.Input.Method() == "OPTIONS" { | ||
| 150 | + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") | ||
| 151 | + return | ||
| 152 | + } | ||
| 153 | + this.Query = map[string]string{} | ||
| 154 | + input := this.Input() | ||
| 155 | + for k := range input { | ||
| 156 | + this.Query[k] = input.Get(k) | ||
| 157 | + } | ||
| 158 | + if this.Ctx.Input.RequestBody != nil { | ||
| 159 | + this.ByteBody = this.Ctx.Input.RequestBody[:] | ||
| 160 | + if len(this.ByteBody) < 1 { | ||
| 161 | + this.ByteBody = []byte("{}") | ||
| 162 | + } | ||
| 163 | + this.RequestHead = this.GetRequestHead() | ||
| 164 | + this.Header = GetRequestHeader(this.Ctx) | ||
| 165 | + this.Header.SetRequestId(fmt.Sprintf("%v.%v.%s", this.Header.Uid, time.GetTimeByYyyymmddhhmmss(), this.Ctx.Request.URL)) | ||
| 166 | + log.Debug(fmt.Sprintf("====>Recv data from uid(%d) ucid(%v) client:\nHeadData: %s\nRequestId:%s BodyData: %s", this.Header.Uid, this.Header.UserId, common.AssertJson(this.Header), this.Header.GetRequestId(), string(this.ByteBody))) | ||
| 167 | + } | ||
| 168 | +} | ||
| 169 | + | ||
| 170 | +func (this *BaseController) Finish() { | ||
| 171 | + if this.Ctx.Input.Method() == "OPTIONS" { | ||
| 172 | + return | ||
| 173 | + } | ||
| 174 | + strByte, _ := json.Marshal(this.Data["json"]) | ||
| 175 | + length := len(strByte) | ||
| 176 | + if length > 5000 { | ||
| 177 | + log.Debug(fmt.Sprintf("<====Send to uid(%d) ucid(%v) client: %d byte\nRequestId:%s RspBodyData: %s......", this.Header.Uid, this.Header.UserId, length, this.Header.GetRequestId(), string(strByte[:5000]))) | ||
| 178 | + } else { | ||
| 179 | + log.Debug(fmt.Sprintf("<====Send to uid(%d) ucid(%v) client: %d byte\nRequestId:%s RspBodyData: %s", this.Header.Uid, this.Header.UserId, length, this.Header.GetRequestId(), string(strByte))) | ||
| 180 | + } | ||
| 181 | +} |
controllers/v1/vod.go
0 → 100644
| 1 | +package v1 | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "encoding/json" | ||
| 5 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 6 | + "openapi/controllers" | ||
| 7 | + "openapi/internal/aliyun" | ||
| 8 | + "openapi/protocol" | ||
| 9 | + "openapi/services/vod" | ||
| 10 | +) | ||
| 11 | + | ||
| 12 | +type VodController struct { | ||
| 13 | + controllers.BaseController | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +//创建视频上传凭证 CreateUploadVideo | ||
| 17 | +// @router /createUploadVideo [post] | ||
| 18 | +func (this *VodController) CreateUploadVideo() { | ||
| 19 | + var msg *protocol.ResponseMessage | ||
| 20 | + defer func() { | ||
| 21 | + this.Resp(msg) | ||
| 22 | + }() | ||
| 23 | + var request *aliyun.CreateUploadVideoRequest | ||
| 24 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
| 25 | + log.Error(err) | ||
| 26 | + msg = protocol.BadRequestParam(1) | ||
| 27 | + return | ||
| 28 | + } | ||
| 29 | + if b, m := this.Valid(request); !b { | ||
| 30 | + msg = m | ||
| 31 | + return | ||
| 32 | + } | ||
| 33 | + if len(request.FileName) == 0 { | ||
| 34 | + request.FileName = aliyun.DefaultVideoFileName | ||
| 35 | + } | ||
| 36 | + header := controllers.GetRequestHeader(this.Ctx) | ||
| 37 | + msg = protocol.NewReturnResponse(vod.CreateUploadVideo(header, request)) | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +//刷新视频上传凭证 RefreshUploadVideo | ||
| 41 | +// @router /refreshUploadVideo [post] | ||
| 42 | +func (this *VodController) RefreshUploadVideo() { | ||
| 43 | + var msg *protocol.ResponseMessage | ||
| 44 | + defer func() { | ||
| 45 | + this.Resp(msg) | ||
| 46 | + }() | ||
| 47 | + var request *aliyun.RefreshUploadVideoRequest | ||
| 48 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
| 49 | + log.Error(err) | ||
| 50 | + msg = protocol.BadRequestParam(1) | ||
| 51 | + return | ||
| 52 | + } | ||
| 53 | + if b, m := this.Valid(request); !b { | ||
| 54 | + msg = m | ||
| 55 | + return | ||
| 56 | + } | ||
| 57 | + header := controllers.GetRequestHeader(this.Ctx) | ||
| 58 | + msg = protocol.NewReturnResponse(vod.RefreshUploadVideo(header, request)) | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +//创建图片上传凭证 CreateUploadImage | ||
| 62 | +// @router /createUploadImage [post] | ||
| 63 | +func (this *VodController) CreateUploadImage() { | ||
| 64 | + var msg *protocol.ResponseMessage | ||
| 65 | + defer func() { | ||
| 66 | + this.Resp(msg) | ||
| 67 | + }() | ||
| 68 | + var request *aliyun.CreateUploadImageRequest | ||
| 69 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
| 70 | + log.Error(err) | ||
| 71 | + msg = protocol.BadRequestParam(1) | ||
| 72 | + return | ||
| 73 | + } | ||
| 74 | + if b, m := this.Valid(request); !b { | ||
| 75 | + msg = m | ||
| 76 | + return | ||
| 77 | + } | ||
| 78 | + header := controllers.GetRequestHeader(this.Ctx) | ||
| 79 | + msg = protocol.NewReturnResponse(vod.CreateUploadImage(header, request)) | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +//创建图片上传凭证 CreateUploadImages | ||
| 83 | +// @router /createUploadImages [post] | ||
| 84 | +func (this *VodController) CreateUploadImages() { | ||
| 85 | + var msg *protocol.ResponseMessage | ||
| 86 | + defer func() { | ||
| 87 | + this.Resp(msg) | ||
| 88 | + }() | ||
| 89 | + var ( | ||
| 90 | + request *aliyun.CreateUploadImagesRequest | ||
| 91 | + response *aliyun.CreateUploadImagesResponse = &aliyun.CreateUploadImagesResponse{ | ||
| 92 | + List: make([]*aliyun.CreateUploadImageResponse, 0), | ||
| 93 | + } | ||
| 94 | + ) | ||
| 95 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
| 96 | + log.Error(err) | ||
| 97 | + msg = protocol.BadRequestParam(1) | ||
| 98 | + return | ||
| 99 | + } | ||
| 100 | + if b, m := this.Valid(request); !b { | ||
| 101 | + msg = m | ||
| 102 | + return | ||
| 103 | + } | ||
| 104 | + var err error | ||
| 105 | + if len(request.Items) == 0 { | ||
| 106 | + msg = protocol.BadRequestParam(2) | ||
| 107 | + } | ||
| 108 | + header := controllers.GetRequestHeader(this.Ctx) | ||
| 109 | + for i := range request.Items { | ||
| 110 | + r := request.Items[i] | ||
| 111 | + if rsp, e := vod.CreateUploadImage(header, r); e != nil { | ||
| 112 | + err = e | ||
| 113 | + break | ||
| 114 | + } else { | ||
| 115 | + response.List = append(response.List, rsp) | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + msg = protocol.NewReturnResponse(response, err) | ||
| 119 | +} | ||
| 120 | + | ||
| 121 | +//获取视频播放地址 GetPlayInfo | ||
| 122 | +// @router /getPlayInfo [post] | ||
| 123 | +func (this *VodController) GetPlayInfo() { | ||
| 124 | + var msg *protocol.ResponseMessage | ||
| 125 | + defer func() { | ||
| 126 | + this.Resp(msg) | ||
| 127 | + }() | ||
| 128 | + var request *aliyun.GetPlayInfoRequest | ||
| 129 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
| 130 | + log.Error(err) | ||
| 131 | + msg = protocol.BadRequestParam(1) | ||
| 132 | + return | ||
| 133 | + } | ||
| 134 | + if b, m := this.Valid(request); !b { | ||
| 135 | + msg = m | ||
| 136 | + return | ||
| 137 | + } | ||
| 138 | + header := controllers.GetRequestHeader(this.Ctx) | ||
| 139 | + msg = protocol.NewReturnResponse(vod.GetPlayInfo(header, request)) | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +//获取视频播放凭证 GetVideoPlayAuth | ||
| 143 | +// @router /getVideoPlayAuth [post] | ||
| 144 | +func (this *VodController) GetVideoPlayAuth() { | ||
| 145 | + var msg *protocol.ResponseMessage | ||
| 146 | + defer func() { | ||
| 147 | + this.Resp(msg) | ||
| 148 | + }() | ||
| 149 | + var request *aliyun.GetVideoPlayAuthRequest | ||
| 150 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
| 151 | + log.Error(err) | ||
| 152 | + msg = protocol.BadRequestParam(1) | ||
| 153 | + return | ||
| 154 | + } | ||
| 155 | + if b, m := this.Valid(request); !b { | ||
| 156 | + msg = m | ||
| 157 | + return | ||
| 158 | + } | ||
| 159 | + header := controllers.GetRequestHeader(this.Ctx) | ||
| 160 | + msg = protocol.NewReturnResponse(vod.GetVideoPlayAuth(header, request)) | ||
| 161 | +} |
deploy/k8s/dev/install.sh
0 → 100644
| 1 | +#!/bin/bash | ||
| 2 | +export PATH=/root/local/bin:$PATH | ||
| 3 | +kubectl -n mmm-suplus-dev get pods | grep -q mmmopp | ||
| 4 | +if [ "$?" == "1" ];then | ||
| 5 | + kubectl create -f /tmp/dev/mmm-go-opp/opp.yaml --record | ||
| 6 | + kubectl -n mmm-suplus-dev get svc | grep -q mmmopp | ||
| 7 | + if [ "$?" == "0" ];then | ||
| 8 | + echo "mmmopp service install success!" | ||
| 9 | + else | ||
| 10 | + echo "mmmopp service install fail!" | ||
| 11 | + fi | ||
| 12 | + kubectl -n mmm-suplus-dev get pods | grep -q mmmopp | ||
| 13 | + if [ "$?" == "0" ];then | ||
| 14 | + echo "mmmopp deployment install success!" | ||
| 15 | + else | ||
| 16 | + echo "mmmopp deployment install fail!" | ||
| 17 | + fi | ||
| 18 | +else | ||
| 19 | + kubectl delete -f /tmp/dev/mmm-go-opp/opp.yaml | ||
| 20 | + kubectl -n mmm-suplus-dev get svc | grep -q mmmopp | ||
| 21 | + while [ "$?" == "0" ] | ||
| 22 | + do | ||
| 23 | + kubectl -n mmm-suplus-dev get svc | grep -q mmmopp | ||
| 24 | + done | ||
| 25 | + kubectl -n mmm-suplus-dev get pods | grep -q mmmopp | ||
| 26 | + while [ "$?" == "0" ] | ||
| 27 | + do | ||
| 28 | + kubectl -n mmm-suplus-dev get pods | grep -q mmmopp | ||
| 29 | + done | ||
| 30 | + kubectl create -f /tmp/dev/mmm-go-opp/opp.yaml --record | ||
| 31 | + kubectl -n mmm-suplus-dev get svc | grep -q mmmopp | ||
| 32 | + if [ "$?" == "0" ];then | ||
| 33 | + echo "mmmopp service update success!" | ||
| 34 | + else | ||
| 35 | + echo "opp service update fail!" | ||
| 36 | + fi | ||
| 37 | + kubectl -n mmm-suplus-dev get pods | grep -q opp | ||
| 38 | + if [ "$?" == "0" ];then | ||
| 39 | + echo "opp deployment update success!" | ||
| 40 | + else | ||
| 41 | + echo "opp deployment update fail!" | ||
| 42 | + fi | ||
| 43 | +fi |
deploy/k8s/dev/opp.yaml
0 → 100644
| 1 | +apiVersion: v1 | ||
| 2 | +kind: Service | ||
| 3 | +metadata: | ||
| 4 | + name: mmmopp | ||
| 5 | + namespace: mmm-suplus-dev | ||
| 6 | + labels: | ||
| 7 | + k8s-app: mmmopp | ||
| 8 | +spec: | ||
| 9 | + ports: | ||
| 10 | + - name: "http" | ||
| 11 | + port: 80 | ||
| 12 | + targetPort: 8080 | ||
| 13 | + selector: | ||
| 14 | + k8s-app: mmmopp | ||
| 15 | +--- | ||
| 16 | +apiVersion: extensions/v1beta1 | ||
| 17 | +kind: Deployment | ||
| 18 | +metadata: | ||
| 19 | + name: mmmopp | ||
| 20 | + namespace: mmm-suplus-dev | ||
| 21 | + labels: | ||
| 22 | + k8s-app: mmmopp | ||
| 23 | +spec: | ||
| 24 | + replicas: 1 | ||
| 25 | + template: | ||
| 26 | + metadata: | ||
| 27 | + labels: | ||
| 28 | + k8s-app: mmmopp | ||
| 29 | + spec: | ||
| 30 | + affinity: | ||
| 31 | + nodeAffinity: | ||
| 32 | + preferredDuringSchedulingIgnoredDuringExecution: | ||
| 33 | + - preference: {} | ||
| 34 | + weight: 100 | ||
| 35 | + requiredDuringSchedulingIgnoredDuringExecution: | ||
| 36 | + nodeSelectorTerms: | ||
| 37 | + - matchExpressions: | ||
| 38 | + - key: kubernetes.io/hostname | ||
| 39 | + operator: In | ||
| 40 | + values: | ||
| 41 | + - cn-hangzhou.i-bp1djh1xn7taumbue1ze | ||
| 42 | + - cn-hangzhou.i-bp1djh1xn7taumbue1zd | ||
| 43 | + - cn-hangzhou.i-bp1euf5u1ph9kbhtndhb | ||
| 44 | + - cn-hangzhou.i-bp1hyp5oips9cdwxxgxy | ||
| 45 | + containers: | ||
| 46 | + - name: mmmopp | ||
| 47 | + image: 192.168.0.243:5000/mmm/mmmopp:dev | ||
| 48 | + imagePullPolicy: Always | ||
| 49 | + volumeMounts: | ||
| 50 | + - mountPath: /opt/logs | ||
| 51 | + name: accesslogs | ||
| 52 | + - mountPath: /var/www/opp/file | ||
| 53 | + name: mmmjihuitest-pvc1 | ||
| 54 | + ports: | ||
| 55 | + - containerPort: 8080 | ||
| 56 | + env: | ||
| 57 | + - name: MYSQL_HOST | ||
| 58 | + valueFrom: | ||
| 59 | + configMapKeyRef: | ||
| 60 | + name: suplus-config | ||
| 61 | + key: mysql.host | ||
| 62 | + - name: MYSQL_PORT | ||
| 63 | + valueFrom: | ||
| 64 | + configMapKeyRef: | ||
| 65 | + name: suplus-config | ||
| 66 | + key: mysql.port | ||
| 67 | + - name: REDIS_HOST | ||
| 68 | + valueFrom: | ||
| 69 | + configMapKeyRef: | ||
| 70 | + name: suplus-config | ||
| 71 | + key: redis.ip | ||
| 72 | + - name: REDIS_PORT | ||
| 73 | + valueFrom: | ||
| 74 | + configMapKeyRef: | ||
| 75 | + name: suplus-config | ||
| 76 | + key: redis.port | ||
| 77 | + - name: MYSQL_USER | ||
| 78 | + valueFrom: | ||
| 79 | + configMapKeyRef: | ||
| 80 | + name: suplus-config | ||
| 81 | + key: mysql.user | ||
| 82 | + - name: MYSQL_PASSWORD | ||
| 83 | + valueFrom: | ||
| 84 | + configMapKeyRef: | ||
| 85 | + name: suplus-config | ||
| 86 | + key: mysql.password | ||
| 87 | + - name: MYSQL_DB_NAME | ||
| 88 | + value: "opportunity_dev" | ||
| 89 | + - name: RUN_MODE | ||
| 90 | + value: "dev-online" | ||
| 91 | + - name: LOG_LEVEL | ||
| 92 | + value: "debug" | ||
| 93 | + - name: aliyun_logs_suplusfile | ||
| 94 | + value: "stdout" | ||
| 95 | + - name: aliyun_logs_access | ||
| 96 | + value: " /opt/logs/app.log" | ||
| 97 | + volumes: | ||
| 98 | + - name: accesslogs | ||
| 99 | + emptyDir: {} | ||
| 100 | + - name: mmmjihuitest-pvc1 | ||
| 101 | + persistentVolumeClaim: | ||
| 102 | + claimName: mmmjihuitest-pvc |
deploy/k8s/prd/install.sh
0 → 100644
| 1 | +#!/bin/bash | ||
| 2 | +export PATH=/root/local/bin:$PATH | ||
| 3 | +kubectl -n mmm-suplus-prd get pods | grep -q mmmopp | ||
| 4 | +if [ "$?" == "1" ];then | ||
| 5 | + kubectl create -f /tmp/prd/mmm-go-opp/opp.yaml --record | ||
| 6 | + kubectl -n mmm-suplus-prd get svc | grep -q mmmopp | ||
| 7 | + if [ "$?" == "0" ];then | ||
| 8 | + echo "mmmopp service install success!" | ||
| 9 | + else | ||
| 10 | + echo "mmmopp service install fail!" | ||
| 11 | + fi | ||
| 12 | + kubectl -n mmm-suplus-prd get pods | grep -q mmmopp | ||
| 13 | + if [ "$?" == "0" ];then | ||
| 14 | + echo "mmmopp deployment install success!" | ||
| 15 | + else | ||
| 16 | + echo "mmmopp deployment install fail!" | ||
| 17 | + fi | ||
| 18 | +else | ||
| 19 | + kubectl delete -f /tmp/prd/mmm-go-opp/opp.yaml | ||
| 20 | + kubectl -n mmm-suplus-prd get svc | grep -q mmmopp | ||
| 21 | + while [ "$?" == "0" ] | ||
| 22 | + do | ||
| 23 | + kubectl -n mmm-suplus-prd get svc | grep -q mmmopp | ||
| 24 | + done | ||
| 25 | + kubectl -n mmm-suplus-prd get pods | grep -q mmmopp | ||
| 26 | + while [ "$?" == "0" ] | ||
| 27 | + do | ||
| 28 | + kubectl -n mmm-suplus-prd get pods | grep -q mmmopp | ||
| 29 | + done | ||
| 30 | + kubectl create -f /tmp/prd/mmm-go-opp/opp.yaml --record | ||
| 31 | + kubectl -n mmm-suplus-prd get svc | grep -q mmmopp | ||
| 32 | + if [ "$?" == "0" ];then | ||
| 33 | + echo "mmmopp service update success!" | ||
| 34 | + else | ||
| 35 | + echo "opp service update fail!" | ||
| 36 | + fi | ||
| 37 | + kubectl -n mmm-suplus-prd get pods | grep -q opp | ||
| 38 | + if [ "$?" == "0" ];then | ||
| 39 | + echo "opp deployment update success!" | ||
| 40 | + else | ||
| 41 | + echo "opp deployment update fail!" | ||
| 42 | + fi | ||
| 43 | +fi |
deploy/k8s/prd/opp.yaml
0 → 100644
| 1 | +apiVersion: v1 | ||
| 2 | +kind: Service | ||
| 3 | +metadata: | ||
| 4 | + name: mmmopp | ||
| 5 | + namespace: mmm-suplus-prd | ||
| 6 | + labels: | ||
| 7 | + k8s-app: mmmopp | ||
| 8 | +spec: | ||
| 9 | + ports: | ||
| 10 | + - name: "http" | ||
| 11 | + port: 80 | ||
| 12 | + targetPort: 8080 | ||
| 13 | + - name: "https" | ||
| 14 | + port: 443 | ||
| 15 | + targetPort: 443 | ||
| 16 | + selector: | ||
| 17 | + k8s-app: mmmopp | ||
| 18 | +--- | ||
| 19 | +apiVersion: extensions/v1beta1 | ||
| 20 | +kind: Deployment | ||
| 21 | +metadata: | ||
| 22 | + name: mmmopp | ||
| 23 | + namespace: mmm-suplus-prd | ||
| 24 | + labels: | ||
| 25 | + k8s-app: mmmopp | ||
| 26 | +spec: | ||
| 27 | + replicas: 1 | ||
| 28 | + template: | ||
| 29 | + metadata: | ||
| 30 | + labels: | ||
| 31 | + k8s-app: mmmopp | ||
| 32 | + spec: | ||
| 33 | + affinity: | ||
| 34 | + nodeAffinity: | ||
| 35 | + preferredDuringSchedulingIgnoredDuringExecution: | ||
| 36 | + - preference: {} | ||
| 37 | + weight: 100 | ||
| 38 | + requiredDuringSchedulingIgnoredDuringExecution: | ||
| 39 | + nodeSelectorTerms: | ||
| 40 | + - matchExpressions: | ||
| 41 | + - key: kubernetes.io/hostname | ||
| 42 | + operator: In | ||
| 43 | + values: | ||
| 44 | + - cn-hangzhou.i-bp1djh1xn7taumbue1ze | ||
| 45 | + - cn-hangzhou.i-bp1djh1xn7taumbue1zd | ||
| 46 | + - cn-hangzhou.i-bp1euf5u1ph9kbhtndhb | ||
| 47 | + - cn-hangzhou.i-bp1hyp5oips9cdwxxgxy | ||
| 48 | + containers: | ||
| 49 | + - name: mmmopp | ||
| 50 | + image: 192.168.0.243:5000/mmm/mmmopp:master | ||
| 51 | + imagePullPolicy: Always | ||
| 52 | + volumeMounts: | ||
| 53 | + - mountPath: /opt/logs | ||
| 54 | + name: accesslogs | ||
| 55 | + - mountPath: /var/www/opp/file | ||
| 56 | + name: opportunity-pvc1 | ||
| 57 | + ports: | ||
| 58 | + - containerPort: 8080 | ||
| 59 | + - containerPort: 443 | ||
| 60 | + env: | ||
| 61 | + - name: MYSQL_HOST | ||
| 62 | + valueFrom: | ||
| 63 | + configMapKeyRef: | ||
| 64 | + name: suplus-config | ||
| 65 | + key: mysql.host | ||
| 66 | + - name: MYSQL_PORT | ||
| 67 | + valueFrom: | ||
| 68 | + configMapKeyRef: | ||
| 69 | + name: suplus-config | ||
| 70 | + key: mysql.port | ||
| 71 | + - name: REDIS_HOST | ||
| 72 | + valueFrom: | ||
| 73 | + configMapKeyRef: | ||
| 74 | + name: suplus-config | ||
| 75 | + key: redis.ip | ||
| 76 | + - name: REDIS_PORT | ||
| 77 | + valueFrom: | ||
| 78 | + configMapKeyRef: | ||
| 79 | + name: suplus-config | ||
| 80 | + key: redis.port | ||
| 81 | + - name: MYSQL_USER | ||
| 82 | + valueFrom: | ||
| 83 | + configMapKeyRef: | ||
| 84 | + name: suplus-config | ||
| 85 | + key: mysql.user | ||
| 86 | + - name: MYSQL_PASSWORD | ||
| 87 | + valueFrom: | ||
| 88 | + configMapKeyRef: | ||
| 89 | + name: suplus-config | ||
| 90 | + key: mysql.password | ||
| 91 | + - name: MYSQL_DB_NAME | ||
| 92 | + value: "opportunity" | ||
| 93 | + - name: RUN_MODE | ||
| 94 | + value: "prod" | ||
| 95 | + - name: LOG_LEVEL | ||
| 96 | + value: "debug" | ||
| 97 | + - name: aliyun_logs_suplusfile | ||
| 98 | + value: "stdout" | ||
| 99 | + - name: aliyun_logs_access | ||
| 100 | + value: " /opt/logs/app.log" | ||
| 101 | + volumes: | ||
| 102 | + - name: accesslogs | ||
| 103 | + emptyDir: {} | ||
| 104 | + - name: opportunity-pvc1 | ||
| 105 | + persistentVolumeClaim: | ||
| 106 | + claimName: opportunity-pvc5 |
deploy/k8s/test/install.sh
0 → 100644
| 1 | +#!/bin/bash | ||
| 2 | +export PATH=/root/local/bin:$PATH | ||
| 3 | +kubectl -n mmm-suplus-test get pods | grep -q mmmopp | ||
| 4 | +if [ "$?" == "1" ];then | ||
| 5 | + kubectl create -f /tmp/test/mmm-go-opp/opp.yaml --record | ||
| 6 | + kubectl -n mmm-suplus-test get svc | grep -q mmmopp | ||
| 7 | + if [ "$?" == "0" ];then | ||
| 8 | + echo "mmmopp service install success!" | ||
| 9 | + else | ||
| 10 | + echo "mmmopp service install fail!" | ||
| 11 | + fi | ||
| 12 | + kubectl -n mmm-suplus-test get pods | grep -q mmmopp | ||
| 13 | + if [ "$?" == "0" ];then | ||
| 14 | + echo "mmmopp deployment install success!" | ||
| 15 | + else | ||
| 16 | + echo "mmmopp deployment install fail!" | ||
| 17 | + fi | ||
| 18 | +else | ||
| 19 | + kubectl delete -f /tmp/test/mmm-go-opp/opp.yaml | ||
| 20 | + kubectl -n mmm-suplus-test get svc | grep -q mmmopp | ||
| 21 | + while [ "$?" == "0" ] | ||
| 22 | + do | ||
| 23 | + kubectl -n mmm-suplus-test get svc | grep -q mmmopp | ||
| 24 | + done | ||
| 25 | + kubectl -n mmm-suplus-test get pods | grep -q mmmopp | ||
| 26 | + while [ "$?" == "0" ] | ||
| 27 | + do | ||
| 28 | + kubectl -n mmm-suplus-test get pods | grep -q mmmopp | ||
| 29 | + done | ||
| 30 | + kubectl create -f /tmp/test/mmm-go-opp/opp.yaml --record | ||
| 31 | + kubectl -n mmm-suplus-test get svc | grep -q mmmopp | ||
| 32 | + if [ "$?" == "0" ];then | ||
| 33 | + echo "mmmopp service update success!" | ||
| 34 | + else | ||
| 35 | + echo "opp service update fail!" | ||
| 36 | + fi | ||
| 37 | + kubectl -n mmm-suplus-test get pods | grep -q opp | ||
| 38 | + if [ "$?" == "0" ];then | ||
| 39 | + echo "opp deployment update success!" | ||
| 40 | + else | ||
| 41 | + echo "opp deployment update fail!" | ||
| 42 | + fi | ||
| 43 | +fi |
deploy/k8s/test/opp.yaml
0 → 100644
| 1 | +apiVersion: v1 | ||
| 2 | +kind: Service | ||
| 3 | +metadata: | ||
| 4 | + name: mmmopp | ||
| 5 | + namespace: mmm-suplus-test | ||
| 6 | + labels: | ||
| 7 | + k8s-app: mmmopp | ||
| 8 | +spec: | ||
| 9 | + ports: | ||
| 10 | + - name: "http" | ||
| 11 | + port: 80 | ||
| 12 | + targetPort: 8080 | ||
| 13 | + - name: "https" | ||
| 14 | + port: 443 | ||
| 15 | + targetPort: 443 | ||
| 16 | + selector: | ||
| 17 | + k8s-app: mmmopp | ||
| 18 | +--- | ||
| 19 | +apiVersion: extensions/v1beta1 | ||
| 20 | +kind: Deployment | ||
| 21 | +metadata: | ||
| 22 | + name: mmmopp | ||
| 23 | + namespace: mmm-suplus-test | ||
| 24 | + labels: | ||
| 25 | + k8s-app: mmmopp | ||
| 26 | +spec: | ||
| 27 | + replicas: 1 | ||
| 28 | + template: | ||
| 29 | + metadata: | ||
| 30 | + labels: | ||
| 31 | + k8s-app: mmmopp | ||
| 32 | + spec: | ||
| 33 | + affinity: | ||
| 34 | + nodeAffinity: | ||
| 35 | + preferredDuringSchedulingIgnoredDuringExecution: | ||
| 36 | + - preference: {} | ||
| 37 | + weight: 100 | ||
| 38 | + requiredDuringSchedulingIgnoredDuringExecution: | ||
| 39 | + nodeSelectorTerms: | ||
| 40 | + - matchExpressions: | ||
| 41 | + - key: kubernetes.io/hostname | ||
| 42 | + operator: In | ||
| 43 | + values: | ||
| 44 | + - cn-hangzhou.i-bp1djh1xn7taumbue1ze | ||
| 45 | + - cn-hangzhou.i-bp1djh1xn7taumbue1zd | ||
| 46 | + - cn-hangzhou.i-bp1euf5u1ph9kbhtndhb | ||
| 47 | + - cn-hangzhou.i-bp1hyp5oips9cdwxxgxy | ||
| 48 | + containers: | ||
| 49 | + - name: mmmopp | ||
| 50 | + image: 192.168.0.243:5000/mmm/mmmopp:dev | ||
| 51 | + imagePullPolicy: Always | ||
| 52 | + volumeMounts: | ||
| 53 | + - mountPath: /opt/logs | ||
| 54 | + name: accesslogs | ||
| 55 | + - mountPath: /var/www/opp/file | ||
| 56 | + name: mmmjihuitest2-pvc1 | ||
| 57 | + ports: | ||
| 58 | + - containerPort: 8080 | ||
| 59 | + - containerPort: 443 | ||
| 60 | + env: | ||
| 61 | + - name: MYSQL_HOST | ||
| 62 | + valueFrom: | ||
| 63 | + configMapKeyRef: | ||
| 64 | + name: suplus-config | ||
| 65 | + key: mysql.host | ||
| 66 | + - name: MYSQL_PORT | ||
| 67 | + valueFrom: | ||
| 68 | + configMapKeyRef: | ||
| 69 | + name: suplus-config | ||
| 70 | + key: mysql.port | ||
| 71 | + - name: REDIS_HOST | ||
| 72 | + valueFrom: | ||
| 73 | + configMapKeyRef: | ||
| 74 | + name: suplus-config | ||
| 75 | + key: redis.ip | ||
| 76 | + - name: REDIS_PORT | ||
| 77 | + valueFrom: | ||
| 78 | + configMapKeyRef: | ||
| 79 | + name: suplus-config | ||
| 80 | + key: redis.port | ||
| 81 | + - name: MYSQL_USER | ||
| 82 | + valueFrom: | ||
| 83 | + configMapKeyRef: | ||
| 84 | + name: suplus-config | ||
| 85 | + key: mysql.user | ||
| 86 | + - name: MYSQL_PASSWORD | ||
| 87 | + valueFrom: | ||
| 88 | + configMapKeyRef: | ||
| 89 | + name: suplus-config | ||
| 90 | + key: mysql.password | ||
| 91 | + - name: MYSQL_DB_NAME | ||
| 92 | + value: "opportunity_test" | ||
| 93 | + - name: RUN_MODE | ||
| 94 | + value: "test" | ||
| 95 | + - name: LOG_LEVEL | ||
| 96 | + value: "debug" | ||
| 97 | + - name: aliyun_logs_suplusfile | ||
| 98 | + value: "stdout" | ||
| 99 | + - name: aliyun_logs_access | ||
| 100 | + value: " /opt/logs/app.log" | ||
| 101 | + volumes: | ||
| 102 | + - name: accesslogs | ||
| 103 | + emptyDir: {} | ||
| 104 | + - name: mmmjihuitest2-pvc1 | ||
| 105 | + persistentVolumeClaim: | ||
| 106 | + claimName: mmmjihuitest2-pvc |
go.mod
0 → 100644
internal/aliyun/const.go
0 → 100644
| 1 | +package aliyun | ||
| 2 | + | ||
| 3 | +import "github.com/astaxie/beego" | ||
| 4 | + | ||
| 5 | +var ( | ||
| 6 | + RegionID = "cn-shanghai" | ||
| 7 | + AccessKeyID = beego.AppConfig.String("AccessKeyID") | ||
| 8 | + AccessKeySecret = beego.AppConfig.String("AccessKeySecret") | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +const ( | ||
| 12 | + FileImage = "image" | ||
| 13 | + FileVoice = "voice" | ||
| 14 | + FileVideo = "video" | ||
| 15 | +) | ||
| 16 | + | ||
| 17 | +const ( | ||
| 18 | + DefaultVideoFileName = "test.mp4" | ||
| 19 | + DefaultVoiceFileName = "test.mp3" | ||
| 20 | + DefaultImageFileName = "test.jpg" | ||
| 21 | +) |
internal/aliyun/oss.go
0 → 100644
| 1 | +package aliyun | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/astaxie/beego" | ||
| 5 | +) | ||
| 6 | + | ||
| 7 | +/* | ||
| 8 | +AK AccessKey 完全访问权限 | ||
| 9 | +STS (Security Token Service) 临时访问控制 | ||
| 10 | +*/ | ||
| 11 | + | ||
| 12 | +var ( | ||
| 13 | + Endpoint string = beego.AppConfig.String("end_point") | ||
| 14 | + BucketName string = beego.AppConfig.String("bucket_name") | ||
| 15 | + //BucketNameCdn string = beego.AppConfig.String("bucket_name_cdn") | ||
| 16 | + //EndpointCdn string = beego.AppConfig.String("end_point_cdn") | ||
| 17 | +) | ||
| 18 | + | ||
| 19 | +//type OssSts struct{ | ||
| 20 | +// | ||
| 21 | +//} | ||
| 22 | + | ||
| 23 | +//func NewSTS(){ | ||
| 24 | +// client,_ :=oos.NewClientWithAccessKey(RegionID,AccessKeyID,AccessKeySecret) | ||
| 25 | +// client.DoAction() | ||
| 26 | +//} |
internal/aliyun/struct.go
0 → 100644
| 1 | +package aliyun | ||
| 2 | + | ||
| 3 | +//创建视频上传凭证 | ||
| 4 | +/*CreateUploadVideo */ | ||
| 5 | +type CreateUploadVideoRequest struct { | ||
| 6 | + FileName string `json:"fileName"` | ||
| 7 | +} | ||
| 8 | + | ||
| 9 | +type CreateUploadVideoResponse struct { | ||
| 10 | + RequestId string `json:"requestId"` | ||
| 11 | + VideoId string `json:"videoId"` | ||
| 12 | + UploadAddress string `json:"uploadAddress"` | ||
| 13 | + UploadAuth string `json:"uploadAuth"` | ||
| 14 | + FileURL string `json:"fileURL"` | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +//刷新视频上传凭证 RefreshUploadVideo | ||
| 18 | +type RefreshUploadVideoRequest struct { | ||
| 19 | + VideoId string `json:"videoId" valid:"Required;"` | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +type RefreshUploadVideoResponse struct { | ||
| 23 | + RequestId string `json:"requestId"` | ||
| 24 | + VideoId string `json:"videoId"` | ||
| 25 | + UploadAddress string `json:"uploadAddress"` | ||
| 26 | + UploadAuth string `json:"uploadAuth"` | ||
| 27 | + FileURL string `json:"fileURL"` | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +/*GetPlayInfo 获取播放信息*/ | ||
| 31 | +type GetPlayInfoRequest struct { | ||
| 32 | + VideoId string `json:"videoId" xml:"VideoId"` | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +// GetPlayInfoResponse is the response struct for api GetPlayInfo | ||
| 36 | +type GetPlayInfoResponse struct { | ||
| 37 | + //*responses.BaseResponse | ||
| 38 | + //RequestId string `json:"RequestId" xml:"RequestId"` | ||
| 39 | + VideoBase VideoBase `json:"VideoBase" xml:"VideoBase"` | ||
| 40 | + PlayInfoList PlayInfoListInGetPlayInfo `json:"PlayInfoList" xml:"PlayInfoList"` | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +/*获取视频播放凭证*/ | ||
| 44 | +type GetVideoPlayAuthRequest struct { | ||
| 45 | + VideoId string `json:"videoId" xml:"VideoId"` | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +// VideoBase is a nested struct in vod response | ||
| 49 | +type VideoBase struct { | ||
| 50 | + OutputType string `json:"OutputType" xml:"OutputType"` | ||
| 51 | + CoverURL string `json:"CoverURL" xml:"CoverURL"` | ||
| 52 | + Duration string `json:"Duration" xml:"Duration"` | ||
| 53 | + Status string `json:"Status" xml:"Status"` | ||
| 54 | + Title string `json:"Title" xml:"Title"` | ||
| 55 | + VideoId string `json:"VideoId" xml:"VideoId"` | ||
| 56 | + MediaType string `json:"MediaType" xml:"MediaType"` | ||
| 57 | + CreationTime string `json:"CreationTime" xml:"CreationTime"` | ||
| 58 | + TranscodeMode string `json:"TranscodeMode" xml:"TranscodeMode"` | ||
| 59 | + //ThumbnailList ThumbnailListInGetPlayInfo `json:"ThumbnailList" xml:"ThumbnailList"` | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +type PlayInfoListInGetPlayInfo struct { | ||
| 63 | + PlayInfo []PlayInfo `json:"PlayInfo" xml:"PlayInfo"` | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +// PlayInfo is a nested struct in vod response | ||
| 67 | +type PlayInfo struct { | ||
| 68 | + JobId string `json:"JobId" xml:"JobId"` | ||
| 69 | + Format string `json:"Format" xml:"Format"` | ||
| 70 | + PreprocessStatus string `json:"PreprocessStatus" xml:"PreprocessStatus"` | ||
| 71 | + EncryptType string `json:"EncryptType" xml:"EncryptType"` | ||
| 72 | + Fps string `json:"Fps" xml:"Fps"` | ||
| 73 | + ModificationTime string `json:"ModificationTime" xml:"ModificationTime"` | ||
| 74 | + NarrowBandType string `json:"NarrowBandType" xml:"NarrowBandType"` | ||
| 75 | + Bitrate string `json:"Bitrate" xml:"Bitrate"` | ||
| 76 | + Encrypt int64 `json:"Encrypt" xml:"Encrypt"` | ||
| 77 | + Rand string `json:"Rand" xml:"Rand"` | ||
| 78 | + CreationTime string `json:"CreationTime" xml:"CreationTime"` | ||
| 79 | + StreamType string `json:"StreamType" xml:"StreamType"` | ||
| 80 | + Height int64 `json:"Height" xml:"Height"` | ||
| 81 | + WatermarkId string `json:"WatermarkId" xml:"WatermarkId"` | ||
| 82 | + Duration string `json:"Duration" xml:"Duration"` | ||
| 83 | + Complexity string `json:"Complexity" xml:"Complexity"` | ||
| 84 | + Width int64 `json:"Width" xml:"Width"` | ||
| 85 | + Size int64 `json:"Size" xml:"Size"` | ||
| 86 | + Status string `json:"Status" xml:"Status"` | ||
| 87 | + Definition string `json:"Definition" xml:"Definition"` | ||
| 88 | + Plaintext string `json:"Plaintext" xml:"Plaintext"` | ||
| 89 | + PlayURL string `json:"PlayURL" xml:"PlayURL"` | ||
| 90 | + Specification string `json:"Specification" xml:"Specification"` | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +//GetVideoPlayAuthResponse is the response struct for api GetVideoPlayAuth | ||
| 94 | +type GetVideoPlayAuthResponse struct { | ||
| 95 | + RequestId string `json:"requestId" xml:"RequestId"` | ||
| 96 | + PlayAuth string `json:"playAuth" xml:"PlayAuth"` | ||
| 97 | + VideoMeta VideoMeta `json:"videoMeta" xml:"VideoMeta"` | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +// VideoMeta is a nested struct in vod response | ||
| 101 | +type VideoMeta struct { | ||
| 102 | + CoverURL string `json:"coverURL" xml:"CoverURL"` | ||
| 103 | + Duration float64 `json:"duration" xml:"Duration"` | ||
| 104 | + Status string `json:"-" xml:"Status"` | ||
| 105 | + Title string `json:"title" xml:"Title"` | ||
| 106 | + VideoId string `json:"videoId" xml:"VideoId"` | ||
| 107 | +} |
internal/aliyun/struct_image.go
0 → 100644
| 1 | +package aliyun | ||
| 2 | + | ||
| 3 | +type CreateUploadImageRequest struct { | ||
| 4 | + FileName string `json:"fileName"` | ||
| 5 | +} | ||
| 6 | + | ||
| 7 | +type CreateUploadImageResponse struct { | ||
| 8 | + RequestId string `json:"requestId"` | ||
| 9 | + ImageId string `json:"imageId" ` | ||
| 10 | + ImageURL string `json:"imageURL"` | ||
| 11 | + UploadAddress string `json:"uploadAddress"` | ||
| 12 | + UploadAuth string `json:"uploadAuth"` | ||
| 13 | + FileURL string `json:"fileURL"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +type CreateUploadImagesRequest struct { | ||
| 17 | + Items []*CreateUploadImageRequest `json:"items"` | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +type CreateUploadImagesResponse struct { | ||
| 21 | + List []*CreateUploadImageResponse `json:"list"` | ||
| 22 | +} |
internal/aliyun/utils.go
0 → 100644
| 1 | +package aliyun | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "encoding/base64" | ||
| 5 | + "encoding/json" | ||
| 6 | + "fmt" | ||
| 7 | + "strings" | ||
| 8 | +) | ||
| 9 | + | ||
| 10 | +type UploadAddress struct { | ||
| 11 | + Endpoint string | ||
| 12 | + Bucket string | ||
| 13 | + FileName string | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +//@cname 绑定域名 | ||
| 17 | +func (up UploadAddress) GetFileUrl(cname string) string { | ||
| 18 | + if len(cname) > 0 { | ||
| 19 | + return fmt.Sprintf("%v%v", cname, up.FileName) | ||
| 20 | + } | ||
| 21 | + if strings.Contains(up.Endpoint, "https") { | ||
| 22 | + return fmt.Sprintf("%v%v.%v/%v", up.Endpoint[:8], up.Bucket, up.Endpoint[8:], up.FileName) | ||
| 23 | + } else if strings.Contains(up.Endpoint, "http") { | ||
| 24 | + return fmt.Sprintf("%v%v.%v/%v", up.Endpoint[:7], up.Bucket, up.Endpoint[7:], up.FileName) | ||
| 25 | + } | ||
| 26 | + return "" | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +//解析UploadAddress | ||
| 30 | +func ParseUploadAddress(uploadAddress string) (up UploadAddress, err error) { | ||
| 31 | + var jsData []byte | ||
| 32 | + jsData, err = base64.StdEncoding.DecodeString(uploadAddress) | ||
| 33 | + if err != nil { | ||
| 34 | + return | ||
| 35 | + } | ||
| 36 | + if err = json.Unmarshal(jsData, &up); err != nil { | ||
| 37 | + return | ||
| 38 | + } | ||
| 39 | + return | ||
| 40 | +} |
internal/aliyun/vod.go
0 → 100644
| 1 | +package aliyun | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/aliyun/alibaba-cloud-sdk-go/sdk" | ||
| 6 | + "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" | ||
| 7 | + "github.com/aliyun/alibaba-cloud-sdk-go/services/vod" | ||
| 8 | + "github.com/astaxie/beego" | ||
| 9 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | ||
| 10 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 11 | + comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time" | ||
| 12 | + "openapi/internal/utils" | ||
| 13 | + "path" | ||
| 14 | + "path/filepath" | ||
| 15 | + "time" | ||
| 16 | +) | ||
| 17 | + | ||
| 18 | +//客户端 | ||
| 19 | +func DefaultVodClient() (client *vod.Client, err error) { | ||
| 20 | + return InitVodClient(AccessKeyID, AccessKeySecret) | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +//初始化客户端 | ||
| 24 | +func InitVodClient(accessKeyId string, accessKeySecret string) (client *vod.Client, err error) { | ||
| 25 | + // 点播服务接入区域 | ||
| 26 | + regionId := RegionID | ||
| 27 | + // 创建授权对象 | ||
| 28 | + credential := &credentials.AccessKeyCredential{ | ||
| 29 | + accessKeyId, | ||
| 30 | + accessKeySecret, | ||
| 31 | + } | ||
| 32 | + // 自定义config | ||
| 33 | + config := sdk.NewConfig() | ||
| 34 | + config.AutoRetry = true // 失败是否自动重试 | ||
| 35 | + config.MaxRetryTime = 3 // 最大重试次数 | ||
| 36 | + config.Timeout = 3000000000 // 连接超时,单位:纳秒;默认为3秒 | ||
| 37 | + // 创建vodClient实例 | ||
| 38 | + return vod.NewClientWithOptions(regionId, config, credential) | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +//获取视频上传地址和凭证,并创建视频信息 | ||
| 42 | +func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (response *CreateUploadVideoResponse, err error) { | ||
| 43 | + request := vod.CreateCreateUploadVideoRequest() | ||
| 44 | + filePath := getFileName(FileVideo, r.FileName) | ||
| 45 | + if filepath.Ext(r.FileName) == "mp3" { | ||
| 46 | + filePath = getFileName(FileVoice, r.FileName) | ||
| 47 | + } | ||
| 48 | + request.Title = filePath | ||
| 49 | + request.FileName = filePath | ||
| 50 | + request.AcceptFormat = "JSON" | ||
| 51 | + rsp, err := client.CreateUploadVideo(request) | ||
| 52 | + if err != nil { | ||
| 53 | + return | ||
| 54 | + } | ||
| 55 | + response = &CreateUploadVideoResponse{ | ||
| 56 | + RequestId: rsp.RequestId, | ||
| 57 | + VideoId: rsp.VideoId, | ||
| 58 | + UploadAddress: rsp.UploadAddress, | ||
| 59 | + UploadAuth: rsp.UploadAuth, | ||
| 60 | + } | ||
| 61 | + if up, e := ParseUploadAddress(rsp.UploadAddress); e != nil { | ||
| 62 | + log.Error(e) | ||
| 63 | + } else { | ||
| 64 | + response.FileURL = up.GetFileUrl("") | ||
| 65 | + } | ||
| 66 | + return | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +func RefreshUploadVideo(client *vod.Client, r *RefreshUploadVideoRequest) (response *RefreshUploadVideoResponse, err error) { | ||
| 70 | + request := vod.CreateRefreshUploadVideoRequest() | ||
| 71 | + request.VideoId = r.VideoId | ||
| 72 | + request.AcceptFormat = "JSON" | ||
| 73 | + | ||
| 74 | + var rsp *vod.RefreshUploadVideoResponse | ||
| 75 | + rsp, err = client.RefreshUploadVideo(request) | ||
| 76 | + if err != nil { | ||
| 77 | + return | ||
| 78 | + } | ||
| 79 | + if err = utils.JsonDeepCopy(&response, &rsp); err != nil { | ||
| 80 | + return | ||
| 81 | + } | ||
| 82 | + if up, e := ParseUploadAddress(rsp.UploadAddress); e != nil { | ||
| 83 | + log.Error(e) | ||
| 84 | + } else { | ||
| 85 | + response.FileURL = up.GetFileUrl("") | ||
| 86 | + } | ||
| 87 | + return | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +//获取图片上传地址和凭证,并创建视频信息 | ||
| 91 | +func CreateUploadImage(client *vod.Client, r *CreateUploadImageRequest) (response *CreateUploadImageResponse, err error) { | ||
| 92 | + request := vod.CreateCreateUploadImageRequest() | ||
| 93 | + filePath := getFileName(FileImage, r.FileName) | ||
| 94 | + request.ImageType = "default" | ||
| 95 | + request.Title = filePath | ||
| 96 | + request.ImageExt = filepath.Ext(r.FileName)[1:] | ||
| 97 | + request.AcceptFormat = "JSON" | ||
| 98 | + //request.StorageLocation = filepath.Base(request.Title) | ||
| 99 | + rsp, err := client.CreateUploadImage(request) | ||
| 100 | + if err != nil { | ||
| 101 | + return | ||
| 102 | + } | ||
| 103 | + err = utils.JsonDeepCopy(&response, &rsp) | ||
| 104 | + return | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +//获取播放信息 | ||
| 108 | +func GetPlayInfo(client *vod.Client, r *GetPlayInfoRequest) (response *vod.GetPlayInfoResponse, err error) { | ||
| 109 | + request := vod.CreateGetPlayInfoRequest() | ||
| 110 | + request.VideoId = r.VideoId | ||
| 111 | + request.AcceptFormat = "JSON" | ||
| 112 | + return client.GetPlayInfo(request) | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +//获取播放信息 | ||
| 116 | +func GetGetVideoPlayAuth(client *vod.Client, r *GetVideoPlayAuthRequest) (response GetVideoPlayAuthResponse, err error) { | ||
| 117 | + request := vod.CreateGetVideoPlayAuthRequest() | ||
| 118 | + request.VideoId = r.VideoId | ||
| 119 | + request.AcceptFormat = "JSON" | ||
| 120 | + var rsp *vod.GetVideoPlayAuthResponse | ||
| 121 | + if rsp, err = client.GetVideoPlayAuth(request); err != nil { | ||
| 122 | + log.Error(err) | ||
| 123 | + return | ||
| 124 | + } | ||
| 125 | + err = utils.JsonDeepCopy(&response, rsp) | ||
| 126 | + return | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +//fileType: video voice image | ||
| 130 | +func getFileName(fileType string, filename string) string { | ||
| 131 | + date := comm_time.GetTimeByYyyymmdd() | ||
| 132 | + subfix := path.Ext(filename) | ||
| 133 | + prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32)) | ||
| 134 | + filename = fmt.Sprintf("%v%v", prefix, subfix) | ||
| 135 | + sourcePath := fmt.Sprintf("%v/%v/%v/%v/%v", beego.BConfig.AppName, beego.BConfig.RunMode, fileType, date, filename) | ||
| 136 | + return sourcePath | ||
| 137 | +} |
internal/utils/file.go
0 → 100644
| 1 | +package utils | ||
| 2 | + | ||
| 3 | +import "os" | ||
| 4 | + | ||
| 5 | +// 判断所给路径文件/文件夹是否存在 | ||
| 6 | +func Exists(path string) bool { | ||
| 7 | + _, err := os.Stat(path) //os.Stat获取文件信息 | ||
| 8 | + if err != nil { | ||
| 9 | + if os.IsExist(err) { | ||
| 10 | + return true | ||
| 11 | + } | ||
| 12 | + return false | ||
| 13 | + } | ||
| 14 | + return true | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +// 判断所给路径是否为文件夹 | ||
| 18 | +func IsDir(path string) bool { | ||
| 19 | + s, err := os.Stat(path) | ||
| 20 | + if err != nil { | ||
| 21 | + return false | ||
| 22 | + } | ||
| 23 | + return s.IsDir() | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +// 判断所给路径是否为文件 | ||
| 27 | +func IsFile(path string) bool { | ||
| 28 | + return !IsDir(path) | ||
| 29 | +} |
internal/utils/log.go
0 → 100644
| 1 | +package utils | ||
| 2 | + | ||
| 3 | +import "github.com/astaxie/beego" | ||
| 4 | + | ||
| 5 | +func init() { | ||
| 6 | + logLevelMap := make(map[string]int) | ||
| 7 | + logLevelMap["debug"] = beego.LevelDebug | ||
| 8 | + logLevelMap["info"] = beego.LevelInformational | ||
| 9 | + logLevelMap["warn"] = beego.LevelWarning | ||
| 10 | + logLevelMap["error"] = beego.LevelError | ||
| 11 | + logLevelMap["fatal"] = beego.LevelEmergency | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func ResolveLogLevel(level string) int { | ||
| 15 | + switch level { | ||
| 16 | + case "debug": | ||
| 17 | + return beego.LevelDebug | ||
| 18 | + case "info": | ||
| 19 | + return beego.LevelInformational | ||
| 20 | + case "warn": | ||
| 21 | + return beego.LevelWarning | ||
| 22 | + case "error": | ||
| 23 | + return beego.LevelError | ||
| 24 | + case "fatal": | ||
| 25 | + return beego.LevelEmergency | ||
| 26 | + } | ||
| 27 | + return beego.LevelInformational | ||
| 28 | +} |
internal/utils/math.go
0 → 100644
| 1 | +package utils | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "math" | ||
| 6 | + "strconv" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +func decimal(value float64) float64 { | ||
| 10 | + return math.Trunc(value*1e1+0.5) * 1e-1 | ||
| 11 | +} | ||
| 12 | + | ||
| 13 | +func Decimal(value float64) float64 { | ||
| 14 | + value = decimal(value) | ||
| 15 | + value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64) | ||
| 16 | + return value | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +func DecimalToNumber(value float64) float64 { | ||
| 20 | + value = decimal(value) | ||
| 21 | + value, _ = strconv.ParseFloat(fmt.Sprintf("%.f", value), 64) | ||
| 22 | + return value | ||
| 23 | +} |
internal/utils/sql.go
0 → 100644
| 1 | +package utils | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "bytes" | ||
| 5 | + "errors" | ||
| 6 | + "fmt" | ||
| 7 | + "github.com/astaxie/beego/orm" | ||
| 8 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 9 | + "reflect" | ||
| 10 | + "strings" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +// 更新指定表的几个列 | ||
| 14 | +func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{}) error { | ||
| 15 | + if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr { | ||
| 16 | + err := errors.New("UpdateTableByMap: tableStruct must ptr") | ||
| 17 | + log.Error(err) | ||
| 18 | + return err | ||
| 19 | + } | ||
| 20 | + if len(changeMap) < 1 { | ||
| 21 | + log.Info("changeMap is nil") | ||
| 22 | + return nil | ||
| 23 | + } | ||
| 24 | + o := orm.NewOrm() | ||
| 25 | + changeColumn := make([]string, 0, len(changeMap)) | ||
| 26 | + for i, v := range changeMap { | ||
| 27 | + changeColumn = append(changeColumn, i) | ||
| 28 | + if err := SetStructValueByType(tabeleStruct, i, v); err != nil { | ||
| 29 | + log.Error(err, i, v) | ||
| 30 | + return err | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + num, err := o.Update(tabeleStruct, changeColumn...) | ||
| 34 | + if err != nil { | ||
| 35 | + log.Error(err) | ||
| 36 | + return err | ||
| 37 | + } | ||
| 38 | + log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v values:%v", GetTableName(tabeleStruct), num, changeColumn, changeMap)) | ||
| 39 | + return nil | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +func UpdateTableByMapWithOrmer(o orm.Ormer, tabeleStruct interface{}, changeMap map[string]interface{}) error { | ||
| 43 | + if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr { | ||
| 44 | + err := errors.New("UpdateTableByMap: tableStruct must ptr") | ||
| 45 | + log.Error(err) | ||
| 46 | + return err | ||
| 47 | + } | ||
| 48 | + if len(changeMap) < 1 { | ||
| 49 | + log.Info("changeMap is nil") | ||
| 50 | + return nil | ||
| 51 | + } | ||
| 52 | + changeColumn := make([]string, 0, len(changeMap)) | ||
| 53 | + for i, v := range changeMap { | ||
| 54 | + changeColumn = append(changeColumn, i) | ||
| 55 | + if err := SetStructValueByType(tabeleStruct, i, v); err != nil { | ||
| 56 | + log.Error(err, i, v) | ||
| 57 | + return err | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + num, err := o.Update(tabeleStruct, changeColumn...) | ||
| 61 | + if err != nil { | ||
| 62 | + log.Error(err) | ||
| 63 | + return err | ||
| 64 | + } | ||
| 65 | + log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v", GetTableName(tabeleStruct), num, changeColumn)) | ||
| 66 | + return nil | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +// 通过反射调用结构对应的TableName函数,达到返回表名的目的 | ||
| 70 | +func GetTableName(tableStruct interface{}) string { | ||
| 71 | + m := reflect.ValueOf(tableStruct).MethodByName("TableName") | ||
| 72 | + if m.IsValid() && m.Kind() == reflect.Func { | ||
| 73 | + re := m.Call(nil) | ||
| 74 | + for _, v := range re { | ||
| 75 | + if v.IsValid() { | ||
| 76 | + return v.String() | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + return "unknown" | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +// 通用事物提交sql结构体 | ||
| 84 | +type SqlData struct { | ||
| 85 | + Sql string | ||
| 86 | + Param []interface{} | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +func ExecuteSqlByRoll(isCheck bool, sqlSlice ...*SqlData) bool { | ||
| 90 | + o := orm.NewOrm() | ||
| 91 | + var someError bool = false | ||
| 92 | + o.Begin() | ||
| 93 | + for i := range sqlSlice { | ||
| 94 | + if sqlSlice[i].Sql == "" { | ||
| 95 | + continue | ||
| 96 | + } | ||
| 97 | + log.Info("execute sql:", sqlSlice[i]) | ||
| 98 | + if ret, err := o.Raw(sqlSlice[i].Sql, sqlSlice[i].Param...).Exec(); err != nil { | ||
| 99 | + log.Error(err) | ||
| 100 | + someError = true | ||
| 101 | + } else { | ||
| 102 | + num, _ := ret.RowsAffected() | ||
| 103 | + log.Debug("num:", num) | ||
| 104 | + if isCheck && num < 1 { | ||
| 105 | + someError = true | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + if someError { | ||
| 110 | + log.Error("出错,回滚事物") | ||
| 111 | + o.Rollback() | ||
| 112 | + return false | ||
| 113 | + } else { | ||
| 114 | + log.Info("成功,提交事物") | ||
| 115 | + o.Commit() | ||
| 116 | + return true | ||
| 117 | + } | ||
| 118 | + return true | ||
| 119 | +} | ||
| 120 | + | ||
| 121 | +//PrintLogSql 打印sql语句 | ||
| 122 | +func PrintLogSql(sql string, param ...interface{}) { | ||
| 123 | + format := `SQL EXCEUTE:[%s]-%s` | ||
| 124 | + parmformat := `[%v]` | ||
| 125 | + var p strings.Builder | ||
| 126 | + for i := range param { | ||
| 127 | + p.WriteString(fmt.Sprintf(parmformat, param[i])) | ||
| 128 | + } | ||
| 129 | + log.Debug(fmt.Sprintf(format, sql, p.String())) | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +//ExecuteQueryOne 执行原生sql查询单条记录;结果用结构体接收 | ||
| 133 | +func ExecuteQueryOne(result interface{}, sqlstr string, param ...interface{}) error { | ||
| 134 | + var err error | ||
| 135 | + o := orm.NewOrm() | ||
| 136 | + err = ExecuteQueryOneWithOrmer(o, result, sqlstr, param) | ||
| 137 | + return err | ||
| 138 | +} | ||
| 139 | + | ||
| 140 | +//ExecuteQueryOneWithOrmer 执行原生sql查询单条 | ||
| 141 | +func ExecuteQueryOneWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error { | ||
| 142 | + PrintLogSql(sqlstr, param...) | ||
| 143 | + var err error | ||
| 144 | + err = o.Raw(sqlstr, param).QueryRow(result) | ||
| 145 | + if err != nil { | ||
| 146 | + return err | ||
| 147 | + } | ||
| 148 | + return nil | ||
| 149 | +} | ||
| 150 | + | ||
| 151 | +//ExecuteQuerySql 执行原生sql查询多条记录 | ||
| 152 | +func ExecuteQueryAll(result interface{}, sqlstr string, param ...interface{}) error { | ||
| 153 | + //PrintLogSql(sqlstr, param...) | ||
| 154 | + var err error | ||
| 155 | + o := orm.NewOrm() | ||
| 156 | + err = ExecuteQueryAllWithOrmer(o, result, sqlstr, param) | ||
| 157 | + return err | ||
| 158 | +} | ||
| 159 | + | ||
| 160 | +//ExecuteQueryOneWithOrmer 执行原生sql查询多条记录 | ||
| 161 | +func ExecuteQueryAllWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error { | ||
| 162 | + PrintLogSql(sqlstr, param...) | ||
| 163 | + var ( | ||
| 164 | + err error | ||
| 165 | + ) | ||
| 166 | + _, err = o.Raw(sqlstr, param).QueryRows(result) | ||
| 167 | + if err != nil { | ||
| 168 | + return err | ||
| 169 | + } | ||
| 170 | + return nil | ||
| 171 | +} | ||
| 172 | + | ||
| 173 | +func ExecuteSQLWithOrmer(o orm.Ormer, sqlstr string, param ...interface{}) error { | ||
| 174 | + PrintLogSql(sqlstr, param...) | ||
| 175 | + var ( | ||
| 176 | + err error | ||
| 177 | + ) | ||
| 178 | + r, err := o.Raw(sqlstr, param...).Exec() | ||
| 179 | + if err != nil { | ||
| 180 | + return err | ||
| 181 | + } | ||
| 182 | + num, _ := r.RowsAffected() | ||
| 183 | + log.Debug(fmt.Sprintf("RowsAffected:%d", num)) | ||
| 184 | + return nil | ||
| 185 | +} | ||
| 186 | + | ||
| 187 | +type SqlExcutor struct { | ||
| 188 | + table string | ||
| 189 | + wherestr []string | ||
| 190 | + orderstr []string | ||
| 191 | + islimit bool | ||
| 192 | + offset int | ||
| 193 | + pagenum int | ||
| 194 | +} | ||
| 195 | + | ||
| 196 | +func NewSqlExutor() *SqlExcutor { | ||
| 197 | + return &SqlExcutor{} | ||
| 198 | +} | ||
| 199 | + | ||
| 200 | +func (s *SqlExcutor) Table(str string) *SqlExcutor { | ||
| 201 | + s.table = str | ||
| 202 | + return s | ||
| 203 | +} | ||
| 204 | + | ||
| 205 | +func (s *SqlExcutor) Where(condition ...string) *SqlExcutor { | ||
| 206 | + if len(condition) <= 0 { | ||
| 207 | + return s | ||
| 208 | + } | ||
| 209 | + s.wherestr = append(s.wherestr, condition...) | ||
| 210 | + return s | ||
| 211 | +} | ||
| 212 | + | ||
| 213 | +func (s *SqlExcutor) Order(condition ...string) *SqlExcutor { | ||
| 214 | + if len(condition) <= 0 { | ||
| 215 | + return s | ||
| 216 | + } | ||
| 217 | + s.orderstr = append(s.orderstr, condition...) | ||
| 218 | + return s | ||
| 219 | +} | ||
| 220 | + | ||
| 221 | +func (s *SqlExcutor) Limit(page, pagenum int) *SqlExcutor { | ||
| 222 | + offset := 0 | ||
| 223 | + if page > 0 { | ||
| 224 | + offset = (page - 1) * pagenum | ||
| 225 | + } | ||
| 226 | + s.islimit = true | ||
| 227 | + s.offset = offset | ||
| 228 | + s.pagenum = pagenum | ||
| 229 | + return s | ||
| 230 | +} | ||
| 231 | + | ||
| 232 | +func (s *SqlExcutor) WhereString() string { | ||
| 233 | + sql := bytes.NewBufferString("") | ||
| 234 | + if len(s.wherestr) > 0 { | ||
| 235 | + sql.WriteString(" where ") | ||
| 236 | + for i := range s.wherestr { | ||
| 237 | + if i != 0 { | ||
| 238 | + sql.WriteString(" AND ") | ||
| 239 | + } | ||
| 240 | + sql.WriteString(s.wherestr[i]) | ||
| 241 | + } | ||
| 242 | + } | ||
| 243 | + return sql.String() | ||
| 244 | +} |
internal/utils/utils.go
0 → 100644
| 1 | +package utils | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "bytes" | ||
| 5 | + "encoding/gob" | ||
| 6 | + "encoding/json" | ||
| 7 | + "errors" | ||
| 8 | + "fmt" | ||
| 9 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 10 | + "reflect" | ||
| 11 | + "strconv" | ||
| 12 | + "strings" | ||
| 13 | + "time" | ||
| 14 | +) | ||
| 15 | + | ||
| 16 | +// 此函数将指定的结构体成员值更新到结构体中 | ||
| 17 | +func SetStructValueByType(s interface{}, columnType string, columnValue interface{}) error { | ||
| 18 | + columnValueV := reflect.ValueOf(columnValue) | ||
| 19 | + var setValue reflect.Value | ||
| 20 | + var flag = false | ||
| 21 | + v := reflect.ValueOf(s) | ||
| 22 | + for i, n := 0, v.Elem().NumField(); i < n; i++ { | ||
| 23 | + if v.Elem().Type().Field(i).Name == columnType { | ||
| 24 | + setValue = v.Elem().Field(i) | ||
| 25 | + flag = true | ||
| 26 | + break | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + if !flag { | ||
| 30 | + return errors.New("struct is not type:") | ||
| 31 | + } else if !setValue.CanSet() { | ||
| 32 | + return errors.New("setValue.CanSet is false") | ||
| 33 | + } else if setValue.Kind() != columnValueV.Kind() { | ||
| 34 | + return errors.New("struct field and value of type is error") | ||
| 35 | + } | ||
| 36 | + switch columnValueV.Kind() { | ||
| 37 | + case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64: | ||
| 38 | + setValue.SetInt(int64(columnValueV.Int())) | ||
| 39 | + case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
| 40 | + setValue.SetUint(uint64(columnValueV.Uint())) | ||
| 41 | + case reflect.Float32, reflect.Float64: | ||
| 42 | + setValue.SetFloat(float64(columnValueV.Float())) | ||
| 43 | + case reflect.String: | ||
| 44 | + setValue.SetString(columnValueV.String()) | ||
| 45 | + case reflect.Struct: | ||
| 46 | + setValue.Set(columnValueV) | ||
| 47 | + default: | ||
| 48 | + return errors.New("columnValue err for:" + columnType) | ||
| 49 | + } | ||
| 50 | + return nil | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +func JoinInts(ids []int, spilt string) string { | ||
| 54 | + var idStrings []string = make([]string, len(ids)) | ||
| 55 | + for i := 0; i < len(ids); i++ { | ||
| 56 | + idStrings[i] = fmt.Sprintf("%v", ids[i]) | ||
| 57 | + } | ||
| 58 | + return strings.Join(idStrings, spilt) | ||
| 59 | +} | ||
| 60 | +func JoinInt8s(ids []int8, spilt string) string { | ||
| 61 | + var idStrings []string = make([]string, len(ids)) | ||
| 62 | + for i := 0; i < len(ids); i++ { | ||
| 63 | + idStrings[i] = fmt.Sprintf("%v", ids[i]) | ||
| 64 | + } | ||
| 65 | + return strings.Join(idStrings, spilt) | ||
| 66 | +} | ||
| 67 | +func JoinInt64s(ids []int64, spilt string) string { | ||
| 68 | + var idStrings []string = make([]string, len(ids)) | ||
| 69 | + for i := 0; i < len(ids); i++ { | ||
| 70 | + idStrings[i] = fmt.Sprintf("%v", ids[i]) | ||
| 71 | + } | ||
| 72 | + return strings.Join(idStrings, spilt) | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +//判断是否为空 | ||
| 76 | +func IsNil(i interface{}) bool { | ||
| 77 | + vi := reflect.ValueOf(i) | ||
| 78 | + if vi.Kind() == reflect.Ptr { | ||
| 79 | + return vi.IsNil() | ||
| 80 | + } | ||
| 81 | + return false | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +func JsonUnmarshal(jsonData string, v interface{}) { | ||
| 85 | + if len(jsonData) == 0 { | ||
| 86 | + return | ||
| 87 | + } | ||
| 88 | + if e := json.Unmarshal([]byte(jsonData), v); e != nil { | ||
| 89 | + log.Error("json.unmarshal error data:", jsonData, e) | ||
| 90 | + } | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +//深度拷贝 | ||
| 94 | +func DeepCopy(dst, src interface{}) error { | ||
| 95 | + var buf bytes.Buffer | ||
| 96 | + if err := gob.NewEncoder(&buf).Encode(src); err != nil { | ||
| 97 | + return err | ||
| 98 | + } | ||
| 99 | + return gob.NewDecoder(&buf).Decode(dst) | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +//深度拷贝 | ||
| 103 | +func JsonDeepCopy(dst, src interface{}) error { | ||
| 104 | + var buf bytes.Buffer | ||
| 105 | + if err := json.NewEncoder(&buf).Encode(src); err != nil { | ||
| 106 | + return err | ||
| 107 | + } | ||
| 108 | + return json.NewDecoder(&buf).Decode(dst) | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +//检查版本信息 | ||
| 112 | +func ValidVersion(current, compare string) bool { | ||
| 113 | + curVersions := strings.Split(current, ".") | ||
| 114 | + comVersions := strings.Split(compare, ".") | ||
| 115 | + for i := range curVersions { | ||
| 116 | + //v1,v2:=strings.TrimSpace(curVersions[i]),"" | ||
| 117 | + v1, _ := strconv.ParseInt(strings.TrimSpace(curVersions[i]), 10, 64) | ||
| 118 | + var v2 int64 | ||
| 119 | + if i < len(comVersions) { | ||
| 120 | + v2, _ = strconv.ParseInt(strings.TrimSpace(comVersions[i]), 10, 64) | ||
| 121 | + } | ||
| 122 | + if v1 == 0 && v2 == 0 { | ||
| 123 | + continue | ||
| 124 | + } | ||
| 125 | + if v1 >= v2 { | ||
| 126 | + return true | ||
| 127 | + } | ||
| 128 | + if v1 < v2 { | ||
| 129 | + return false | ||
| 130 | + } | ||
| 131 | + } | ||
| 132 | + return false | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +// 统计某函数执行时间 | ||
| 136 | +// 使用方式 | ||
| 137 | +// defer utils.Profiling("test")() | ||
| 138 | +func Profiling(msg string) func() { | ||
| 139 | + start := time.Now() | ||
| 140 | + return func() { | ||
| 141 | + log.Info(fmt.Sprintf("%s[%s]:%s", msg, "use", time.Since(start))) | ||
| 142 | + } | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +//合并字典 | ||
| 146 | +func MergeMap(to map[string]interface{}, from map[string]interface{}) { | ||
| 147 | + for k, v := range from { | ||
| 148 | + to[k] = v | ||
| 149 | + } | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +func GetPageInfo(pageIndex, pageSize int) (offset, size int) { | ||
| 153 | + if pageSize == 0 { | ||
| 154 | + pageSize = 20 | ||
| 155 | + } | ||
| 156 | + if pageIndex == 0 { | ||
| 157 | + pageIndex = 1 | ||
| 158 | + } | ||
| 159 | + offset = (pageIndex - 1) * pageSize | ||
| 160 | + size = pageSize | ||
| 161 | + return | ||
| 162 | +} |
internal/utils/utils_test.go
0 → 100644
| 1 | +package utils | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "encoding/json" | ||
| 5 | + "fmt" | ||
| 6 | + "testing" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +func Test_DeepCopy(t *testing.T) { | ||
| 10 | + type User1 struct { | ||
| 11 | + Name string | ||
| 12 | + Age int | ||
| 13 | + Address string | ||
| 14 | + } | ||
| 15 | + type User2 struct { | ||
| 16 | + Name string | ||
| 17 | + Age int | ||
| 18 | + Job string | ||
| 19 | + } | ||
| 20 | + var src = User1{Name: "foo", Age: 10, Address: "bar"} | ||
| 21 | + var dst *User2 | ||
| 22 | + if err := DeepCopy(&dst, src); err != nil { | ||
| 23 | + t.Fatal(err) | ||
| 24 | + } | ||
| 25 | + if src.Name != dst.Name { | ||
| 26 | + t.Fatal("deep copy fail.") | ||
| 27 | + } | ||
| 28 | + //t.Log(src,"\n",dst) | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +func TestValidVersion(t *testing.T) { | ||
| 32 | + inputs := []struct { | ||
| 33 | + In string | ||
| 34 | + Compare string | ||
| 35 | + Out bool | ||
| 36 | + }{ | ||
| 37 | + {In: "0.9.0", Compare: "0.8.0", Out: true}, | ||
| 38 | + {In: "0.8.11", Compare: "0.8.0", Out: true}, | ||
| 39 | + {In: "0.7.0", Compare: "0.8.0", Out: false}, | ||
| 40 | + {In: "0.8.0", Compare: "0.8.0", Out: true}, | ||
| 41 | + {In: "0.9", Compare: "0.8.0", Out: true}, | ||
| 42 | + {In: "0.10", Compare: "0.8.0", Out: true}, | ||
| 43 | + {In: "1.8.0", Compare: "0.8.0", Out: true}, | ||
| 44 | + {In: "0.99.0", Compare: "0.8.0", Out: true}, | ||
| 45 | + {In: "01.0.0", Compare: "0.8.0", Out: true}, | ||
| 46 | + } | ||
| 47 | + for i := range inputs { | ||
| 48 | + input := inputs[i] | ||
| 49 | + if ValidVersion(input.In, input.Compare) != input.Out { | ||
| 50 | + t.Fatal(fmt.Sprintf("valid version fail. input :%v compare:%v want:%v", input.In, input.Compare, input.Out)) | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +func TestMergeMap(t *testing.T) { | ||
| 56 | + to := map[string]interface{}{"1": 1, "2": 2} | ||
| 57 | + from := map[string]interface{}{"3": 3, "4": 4} | ||
| 58 | + t.Log("merge 前:", to) | ||
| 59 | + MergeMap(to, from) | ||
| 60 | + t.Log("merge 后:", to) | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +func TestIntUnmarsh(t *testing.T) { | ||
| 64 | + jsonString := `{"id":8242051651122944}` | ||
| 65 | + type param struct { | ||
| 66 | + Id int `json:"id"` | ||
| 67 | + } | ||
| 68 | + var p param | ||
| 69 | + json.Unmarshal([]byte(jsonString), &p) | ||
| 70 | + t.Log(p) | ||
| 71 | + jsonByte, _ := json.Marshal(p) | ||
| 72 | + t.Log(string(jsonByte)) | ||
| 73 | +} |
main.go
0 → 100644
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/astaxie/beego" | ||
| 5 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 6 | + _ "openapi/routers" | ||
| 7 | + "time" | ||
| 8 | +) | ||
| 9 | + | ||
| 10 | +func init() { | ||
| 11 | + time.Local = time.FixedZone("CST", 3600*8) | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func main() { | ||
| 15 | + defer func() { | ||
| 16 | + log.Info("app on stop!") | ||
| 17 | + }() | ||
| 18 | + log.Info("app on start!") | ||
| 19 | + log.Info("Beego Run Mode:", beego.BConfig.RunMode) | ||
| 20 | + | ||
| 21 | + beego.Run() | ||
| 22 | +} |
protocol/const.go
0 → 100644
protocol/error.go
0 → 100644
| 1 | +package protocol | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "encoding/json" | ||
| 5 | +) | ||
| 6 | + | ||
| 7 | +//CustomErrParse 解析自定义错误结构体 | ||
| 8 | +type CustomErrParse interface { | ||
| 9 | + ParseToMessage() *ResponseMessage | ||
| 10 | +} | ||
| 11 | + | ||
| 12 | +//ErrorMap 统一消息错误编码 | ||
| 13 | +type ErrorMap map[int]string | ||
| 14 | + | ||
| 15 | +//Search 搜索错误描述 | ||
| 16 | +func (m ErrorMap) Search(code int) ErrorCode { | ||
| 17 | + if v, ok := m[code]; ok { | ||
| 18 | + return ErrorCode{ | ||
| 19 | + Errno: code, | ||
| 20 | + Errmsg: v, | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + return ErrorCode{Errno: code, Errmsg: "错误码未定义"} | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +//ErrorCode 统一错误结构 | ||
| 27 | +type ErrorCode struct { | ||
| 28 | + Errno int `json:"code"` | ||
| 29 | + Errmsg string `json:"msg"` | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +//ResponseMessage 统一返回消息结构体 | ||
| 33 | +type ResponseMessage struct { | ||
| 34 | + ErrorCode | ||
| 35 | + Data interface{} `json:"data"` | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +func NewMesage(code int) *ResponseMessage { | ||
| 39 | + return &ResponseMessage{ | ||
| 40 | + ErrorCode: SearchErr(code), | ||
| 41 | + Data: nil, | ||
| 42 | + } | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +//ErrWithMessage 自定义错误结构 | ||
| 46 | +type ErrWithMessage struct { | ||
| 47 | + Err error `json:"-"` | ||
| 48 | + ErrorCode | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +var ( | ||
| 52 | + _ CustomErrParse = new(ErrWithMessage) | ||
| 53 | + _ error = new(ErrWithMessage) | ||
| 54 | +) | ||
| 55 | + | ||
| 56 | +//NewErrWithMessage 构建错误返回 | ||
| 57 | +//code:用于匹配统一消息错误编码 eRR:填充嵌套错误 | ||
| 58 | +func NewErrWithMessage(code int, eRR ...error) *ErrWithMessage { | ||
| 59 | + r := &ErrWithMessage{ | ||
| 60 | + ErrorCode: SearchErr(code), | ||
| 61 | + } | ||
| 62 | + if len(eRR) > 0 { | ||
| 63 | + r.Err = eRR[0] | ||
| 64 | + } | ||
| 65 | + return r | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +//Error 实现接口error 中的方法 | ||
| 69 | +//将ErrorCode转为json数据,建议用于日志记录 | ||
| 70 | +func (e ErrWithMessage) Error() string { | ||
| 71 | + bt, _ := json.Marshal(e.ErrorCode) | ||
| 72 | + return string(bt) | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +//Unwrap 接口实现 | ||
| 76 | +func (e ErrWithMessage) Unwrap() error { | ||
| 77 | + return e.Err | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +//ParseToMessage 实现CustomErrParse的接口 | ||
| 81 | +func (e ErrWithMessage) ParseToMessage() *ResponseMessage { | ||
| 82 | + return &ResponseMessage{ | ||
| 83 | + ErrorCode: e.ErrorCode, | ||
| 84 | + Data: nil, | ||
| 85 | + } | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +func SearchErr(code int) ErrorCode { | ||
| 89 | + return errmessge.Search(code) | ||
| 90 | +} | ||
| 91 | +func NewReturnResponse(data interface{}, eRR error) *ResponseMessage { | ||
| 92 | + var msg *ResponseMessage | ||
| 93 | + if eRR == nil { | ||
| 94 | + msg = NewMesage(0) | ||
| 95 | + msg.Data = data | ||
| 96 | + return msg | ||
| 97 | + } | ||
| 98 | + // fmt.Println("日志:" + eRR.Error()) | ||
| 99 | + if x, ok := eRR.(CustomErrParse); ok { | ||
| 100 | + msg = x.ParseToMessage() | ||
| 101 | + msg.Data = data | ||
| 102 | + return msg | ||
| 103 | + } | ||
| 104 | + return NewMesage(1) | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +func BadRequestParam(code int) *ResponseMessage { | ||
| 108 | + return NewMesage(code) | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +func NewSuccessWithMessage(msg string) *ErrWithMessage { | ||
| 112 | + return &ErrWithMessage{ | ||
| 113 | + Err: nil, | ||
| 114 | + ErrorCode: ErrorCode{0, msg}, | ||
| 115 | + } | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +func NewCustomMessage(code int, msg string) *ErrWithMessage { | ||
| 119 | + return &ErrWithMessage{ | ||
| 120 | + Err: nil, | ||
| 121 | + ErrorCode: ErrorCode{code, msg}, | ||
| 122 | + } | ||
| 123 | +} |
protocol/global.go
0 → 100644
| 1 | +package protocol | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "sync/atomic" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +type RequestHeader struct { | ||
| 9 | + TimeStamp string | ||
| 10 | + Version string | ||
| 11 | + Uuid string | ||
| 12 | + Sign string | ||
| 13 | + DeviceType int | ||
| 14 | + AppProject string | ||
| 15 | + AccessToken string | ||
| 16 | + Uid int64 //用户基本信息Id | ||
| 17 | + CompanyId int64 | ||
| 18 | + UserId int64 //UserId 唯一标识,唯一关联所有用户信息(=user_company.id) | ||
| 19 | + | ||
| 20 | + requestId string //请求编号 md5 | ||
| 21 | + reqIndex int64 //请求链序号 | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +func (reqHead *RequestHeader) SetRequestId(addString ...string) { | ||
| 25 | + if len(addString) == 0 { | ||
| 26 | + return | ||
| 27 | + } | ||
| 28 | + reqHead.requestId = addString[0] | ||
| 29 | +} | ||
| 30 | +func (reqHead *RequestHeader) GetRequestId() string { | ||
| 31 | + atomic.AddInt64(&reqHead.reqIndex, 1) | ||
| 32 | + return fmt.Sprintf("%s.%d", reqHead.requestId, reqHead.reqIndex) | ||
| 33 | +} |
protocol/message.go
0 → 100644
protocol/upload.go
0 → 100644
| 1 | +package protocol | ||
| 2 | + | ||
| 3 | +import "mime/multipart" | ||
| 4 | + | ||
| 5 | +const ( | ||
| 6 | + FileImage = "image" | ||
| 7 | + FileVoice = "voice" | ||
| 8 | + FileVideo = "video" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +/*Image */ | ||
| 12 | +type FileRequest struct { | ||
| 13 | + //Xxx string`json:"xxx" valid:"Required"` | ||
| 14 | + Files []*multipart.FileHeader | ||
| 15 | + FileType string | ||
| 16 | +} | ||
| 17 | +type FileResponse struct { | ||
| 18 | + Paths []string `json:"paths"` | ||
| 19 | +} |
routers/router.go
0 → 100644
| 1 | +package routers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/astaxie/beego" | ||
| 5 | + "openapi/controllers" | ||
| 6 | + "openapi/controllers/v1" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +var nsV1 *beego.Namespace | ||
| 10 | + | ||
| 11 | +func init() { | ||
| 12 | + nsV1 := beego.NewNamespace("v1", | ||
| 13 | + beego.NSNamespace("vod", beego.NSBefore(controllers.FilterComm), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.VodController{})), | ||
| 14 | + ) | ||
| 15 | + beego.AddNamespace(nsV1) | ||
| 16 | +} |
services/vod/upload.go
0 → 100644
| 1 | +package vod | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 5 | + "openapi/internal/aliyun" | ||
| 6 | + "openapi/protocol" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +//创建视频上传凭证 | ||
| 10 | +func CreateUploadVideo(header *protocol.RequestHeader, request *aliyun.CreateUploadVideoRequest) (rsp *aliyun.CreateUploadVideoResponse, err error) { | ||
| 11 | + var () | ||
| 12 | + client, e := aliyun.DefaultVodClient() | ||
| 13 | + if e != nil { | ||
| 14 | + log.Error(e) | ||
| 15 | + err = e | ||
| 16 | + return | ||
| 17 | + } | ||
| 18 | + rsp, err = aliyun.CreateUploadVideo(client, request) | ||
| 19 | + if err != nil { | ||
| 20 | + log.Error(err) | ||
| 21 | + } | ||
| 22 | + return | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +//创建视频上传凭证 | ||
| 26 | +func RefreshUploadVideo(header *protocol.RequestHeader, request *aliyun.RefreshUploadVideoRequest) (rsp *aliyun.RefreshUploadVideoResponse, err error) { | ||
| 27 | + var () | ||
| 28 | + client, e := aliyun.DefaultVodClient() | ||
| 29 | + if e != nil { | ||
| 30 | + log.Error(e) | ||
| 31 | + err = e | ||
| 32 | + return | ||
| 33 | + } | ||
| 34 | + rsp, err = aliyun.RefreshUploadVideo(client, request) | ||
| 35 | + if err != nil { | ||
| 36 | + log.Error(err) | ||
| 37 | + } | ||
| 38 | + return | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +//创建图片上传凭证 | ||
| 42 | +func CreateUploadImage(header *protocol.RequestHeader, request *aliyun.CreateUploadImageRequest) (rsp *aliyun.CreateUploadImageResponse, err error) { | ||
| 43 | + var () | ||
| 44 | + client, e := aliyun.DefaultVodClient() | ||
| 45 | + if e != nil { | ||
| 46 | + log.Error(e) | ||
| 47 | + err = e | ||
| 48 | + return | ||
| 49 | + } | ||
| 50 | + if len(request.FileName) == 0 { | ||
| 51 | + request.FileName = aliyun.DefaultImageFileName | ||
| 52 | + } | ||
| 53 | + rsp, err = aliyun.CreateUploadImage(client, request) | ||
| 54 | + if err != nil { | ||
| 55 | + log.Error(err) | ||
| 56 | + } | ||
| 57 | + return | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +//获取视频播放地址 | ||
| 61 | +func GetPlayInfo(header *protocol.RequestHeader, request *aliyun.GetPlayInfoRequest) (rsp interface{}, err error) { | ||
| 62 | + var () | ||
| 63 | + client, e := aliyun.DefaultVodClient() | ||
| 64 | + if e != nil { | ||
| 65 | + log.Error(e) | ||
| 66 | + err = e | ||
| 67 | + return | ||
| 68 | + } | ||
| 69 | + rsp, err = aliyun.GetPlayInfo(client, request) | ||
| 70 | + if err != nil { | ||
| 71 | + log.Error(err) | ||
| 72 | + } | ||
| 73 | + return | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +//获取视频播放凭证 | ||
| 77 | +func GetVideoPlayAuth(header *protocol.RequestHeader, request *aliyun.GetVideoPlayAuthRequest) (rsp interface{}, err error) { | ||
| 78 | + var () | ||
| 79 | + client, e := aliyun.DefaultVodClient() | ||
| 80 | + if e != nil { | ||
| 81 | + log.Error(e) | ||
| 82 | + err = e | ||
| 83 | + return | ||
| 84 | + } | ||
| 85 | + rsp, err = aliyun.GetGetVideoPlayAuth(client, request) | ||
| 86 | + if err != nil { | ||
| 87 | + log.Error(err) | ||
| 88 | + } | ||
| 89 | + return | ||
| 90 | +} |
-
请 注册 或 登录 后发表评论