init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
go_build_video
|
||||||
|
dyls
|
||||||
8
.idea/.gitignore
generated
vendored
Executable file
8
.idea/.gitignore
generated
vendored
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
10
.idea/dictionaries/project.xml
generated
Executable file
10
.idea/dictionaries/project.xml
generated
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
<component name="ProjectDictionaryState">
|
||||||
|
<dictionary name="project">
|
||||||
|
<words>
|
||||||
|
<w>goja</w>
|
||||||
|
<w>neec</w>
|
||||||
|
<w>pbmc</w>
|
||||||
|
<w>pemc</w>
|
||||||
|
</words>
|
||||||
|
</dictionary>
|
||||||
|
</component>
|
||||||
8
.idea/modules.xml
generated
Executable file
8
.idea/modules.xml
generated
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/video.iml" filepath="$PROJECT_DIR$/.idea/video.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
9
.idea/video.iml
generated
Executable file
9
.idea/video.iml
generated
Executable file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
21
README.md
Executable file
21
README.md
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
### 获取请求
|
||||||
|
|
||||||
|
```shell
|
||||||
|
curl --request POST \
|
||||||
|
--url http://127.0.0.1:10001/api/v1/base/CallUserNp \
|
||||||
|
--header 'content-type: application/json' \
|
||||||
|
--data '{
|
||||||
|
"EnCodeStr": "https://mtovnd7gjf.gcweqo.cn/api/v1/app/config"
|
||||||
|
}
|
||||||
|
'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 解密响应
|
||||||
|
|
||||||
|
```shell
|
||||||
|
curl --request POST \
|
||||||
|
--url http://127.0.0.1:10001/api/v1/base/CallUserPb \
|
||||||
|
--header 'content-type: application/json' \
|
||||||
|
--data '{"DeCodeStr":"aZmR9OYLTBJiHscJmoiPSeMwPOMZ2S7w7CPL9I15W6W1BA3U0uHXfewX6B8Kh4nK4KIY6pD5iqfvVwMb1teFK74pGh6NGmE6NiZNQSzoWbQ"}'
|
||||||
|
|
||||||
|
```
|
||||||
BIN
api/.DS_Store
vendored
Normal file
BIN
api/.DS_Store
vendored
Normal file
Binary file not shown.
165
api/v1/base.go
Executable file
165
api/v1/base.go
Executable file
@@ -0,0 +1,165 @@
|
|||||||
|
package v1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/dop251/goja"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"video/response"
|
||||||
|
"video/utils/code"
|
||||||
|
"video/utils/exception"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Base struct{}
|
||||||
|
|
||||||
|
type CallUserNpBody struct {
|
||||||
|
EnCodeStr string `json:"EnCodeStr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Base) CallUserNp(c *gin.Context) {
|
||||||
|
var body CallUserNpBody
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, exception.E{Code: code.SERVER_ERROR, Msg: "接收加密参数失败", Err: err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := runJSFunction("script/sign.js", "userNp", body.EnCodeStr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
c.JSON(http.StatusForbidden, exception.E{Code: code.SERVER_ERROR, Msg: "处理失败", Err: err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.SuccessData(result, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CallUserPbBody struct {
|
||||||
|
DeCodeStr string `json:"DeCodeStr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Base) CallUserPb(c *gin.Context) {
|
||||||
|
var body CallUserPbBody
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, exception.E{Code: code.SERVER_ERROR, Msg: "接收解密参数失败", Err: err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := runJSFunction("script/sign.js", "userPB", body.DeCodeStr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
c.JSON(http.StatusForbidden, exception.E{Code: code.SERVER_ERROR, Msg: "处理失败", Err: err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 将结果转为 JSON 字符串
|
||||||
|
jsonBytes, err := json.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, exception.E{Code: code.SERVER_ERROR, Msg: "结果转JSON失败", Err: err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 再转为 Base64 字符串
|
||||||
|
encoded := base64.StdEncoding.EncodeToString(jsonBytes)
|
||||||
|
response.SuccessData(encoded, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runJSFunction(scriptPath string, funcName string, args ...interface{}) (interface{}, error) {
|
||||||
|
script, err := os.ReadFile(scriptPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("读取脚本失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
vm := goja.New()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 注入 URLSearchParams Polyfill
|
||||||
|
polyfill := `
|
||||||
|
function URLSearchParams(query) {
|
||||||
|
this.params = {};
|
||||||
|
if (typeof query === 'string') {
|
||||||
|
query.replace(/^\?/, '').split('&').forEach(function(pair) {
|
||||||
|
var parts = pair.split('=');
|
||||||
|
var k = parts[0];
|
||||||
|
var v = parts[1];
|
||||||
|
if (k) this.params[decodeURIComponent(k)] = decodeURIComponent(v || '');
|
||||||
|
}, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
URLSearchParams.prototype.get = function(key) {
|
||||||
|
return this.params[key] || null;
|
||||||
|
};
|
||||||
|
|
||||||
|
URLSearchParams.prototype.set = function(key, value) {
|
||||||
|
this.params[key] = value;
|
||||||
|
};
|
||||||
|
|
||||||
|
URLSearchParams.prototype.toString = function() {
|
||||||
|
var entries = [];
|
||||||
|
for (var key in this.params) {
|
||||||
|
if (this.params.hasOwnProperty(key)) {
|
||||||
|
entries.push([key, this.params[key]]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var queryString = '';
|
||||||
|
for (var i = 0; i < entries.length; i++) {
|
||||||
|
var pair = entries[i];
|
||||||
|
queryString += encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1]);
|
||||||
|
if (i < entries.length - 1) {
|
||||||
|
queryString += '&';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return queryString;
|
||||||
|
};
|
||||||
|
|
||||||
|
globalThis.URLSearchParams = URLSearchParams;
|
||||||
|
`
|
||||||
|
|
||||||
|
// 先注入 polyfill
|
||||||
|
_, err = vm.RunString(polyfill)
|
||||||
|
if err != nil {
|
||||||
|
//log.Fatal("polyfill error:", err)
|
||||||
|
return nil, fmt.Errorf("注入 URLSearchParams 失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
err = vm.Set("console", map[string]func(...interface{}){
|
||||||
|
"log": func(args ...interface{}) {
|
||||||
|
fmt.Println(args...)
|
||||||
|
},
|
||||||
|
"error":func(args ...interface{}) {
|
||||||
|
fmt.Println(args...)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("注入 console.log 失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := vm.RunString(string(script)); err != nil {
|
||||||
|
return nil, fmt.Errorf("执行 JS 脚本失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn, ok := goja.AssertFunction(vm.Get(funcName))
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("找不到函数: %s", funcName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换参数为 goja.Value
|
||||||
|
var gojaArgs []goja.Value
|
||||||
|
for _, arg := range args {
|
||||||
|
gojaArgs = append(gojaArgs, vm.ToValue(arg))
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := fn(goja.Undefined(), gojaArgs...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("调用函数失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.Export(), nil
|
||||||
|
}
|
||||||
40
go.mod
Executable file
40
go.mod
Executable file
@@ -0,0 +1,40 @@
|
|||||||
|
module video
|
||||||
|
|
||||||
|
go 1.24
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/dop251/goja v0.0.0-20250309171923-bcd7cc6bf64c
|
||||||
|
github.com/gin-gonic/gin v1.10.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
|
github.com/dlclark/regexp2 v1.11.4 // indirect
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||||
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||||
|
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
|
||||||
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
|
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
|
golang.org/x/crypto v0.23.0 // indirect
|
||||||
|
golang.org/x/net v0.25.0 // indirect
|
||||||
|
golang.org/x/sys v0.20.0 // indirect
|
||||||
|
golang.org/x/text v0.15.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.34.1 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
101
go.sum
Executable file
101
go.sum
Executable file
@@ -0,0 +1,101 @@
|
|||||||
|
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
|
||||||
|
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||||
|
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||||
|
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
|
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||||
|
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||||
|
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||||
|
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||||
|
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
|
||||||
|
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||||
|
github.com/dop251/goja v0.0.0-20250309171923-bcd7cc6bf64c h1:mxWGS0YyquJ/ikZOjSrRjjFIbUqIP9ojyYQ+QZTU3Rg=
|
||||||
|
github.com/dop251/goja v0.0.0-20250309171923-bcd7cc6bf64c/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
|
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||||
|
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||||
|
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||||
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||||
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||||
|
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||||
|
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
|
||||||
|
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
|
||||||
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
|
||||||
|
github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||||
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
|
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
|
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||||
|
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||||
|
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||||
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
|
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||||
|
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
|
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||||
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
|
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||||
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||||
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||||
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||||
|
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||||
55
main.go
Executable file
55
main.go
Executable file
@@ -0,0 +1,55 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
"video/router"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := router.InitRouter()
|
||||||
|
host := "0.0.0.0"
|
||||||
|
port := "10001"
|
||||||
|
srv := initServer(host, port, r)
|
||||||
|
|
||||||
|
// 启动服务
|
||||||
|
go startServer(srv, host, port)
|
||||||
|
|
||||||
|
// 优雅关闭
|
||||||
|
gracefulShutdown(srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initServer(host, port string, handler http.Handler) *http.Server {
|
||||||
|
return &http.Server{
|
||||||
|
Addr: fmt.Sprintf("%s:%s", host, port),
|
||||||
|
Handler: handler,
|
||||||
|
ReadTimeout: 60 * time.Second,
|
||||||
|
WriteTimeout: 60 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startServer(srv *http.Server, host, port string) {
|
||||||
|
fmt.Println("Listening and serving HTTP on", host, ":", port)
|
||||||
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
fmt.Printf("listen: %s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func gracefulShutdown(srv *http.Server) {
|
||||||
|
quit := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(quit, os.Interrupt)
|
||||||
|
<-quit
|
||||||
|
fmt.Println("Shutdown Server ...")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := srv.Shutdown(ctx); err != nil {
|
||||||
|
fmt.Println("Server Shutdown:", err)
|
||||||
|
}
|
||||||
|
fmt.Println("Server exiting")
|
||||||
|
}
|
||||||
30
response/response.go
Executable file
30
response/response.go
Executable file
@@ -0,0 +1,30 @@
|
|||||||
|
package response
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
"video/utils/code"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result ... 正常业务获得结果
|
||||||
|
func Result(code int, data interface{}, msg string, c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, Response{code, data, msg})
|
||||||
|
}
|
||||||
|
|
||||||
|
func Success(c *gin.Context) {
|
||||||
|
SuccessMsg("操作成功", c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SuccessMsg(message string, c *gin.Context) {
|
||||||
|
Result(code.SUCCESS, map[string]interface{}{}, message, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SuccessData(data interface{}, c *gin.Context) {
|
||||||
|
Result(code.SUCCESS, data, "操作成功", c)
|
||||||
|
}
|
||||||
31
router/router.go
Executable file
31
router/router.go
Executable file
@@ -0,0 +1,31 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
v1 "video/api/v1"
|
||||||
|
"video/utils/code"
|
||||||
|
"video/utils/exception"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitRouter() *gin.Engine {
|
||||||
|
gin.SetMode("release")
|
||||||
|
r := gin.Default()
|
||||||
|
r.NoRoute(func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusForbidden, exception.E{Code: code.NOT_FOUND_ROUTE, Msg: "Not Found Route", Err: nil})
|
||||||
|
})
|
||||||
|
r.NoMethod(func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusForbidden, exception.E{Code: code.NOT_FOUND_METH, Msg: "Not Found Method"})
|
||||||
|
})
|
||||||
|
|
||||||
|
api := r.Group("api")
|
||||||
|
{
|
||||||
|
baseV1 := new(v1.Base)
|
||||||
|
apiV1 := api.Group("v1")
|
||||||
|
{
|
||||||
|
apiV1.POST("base/CallUserNp", baseV1.CallUserNp)
|
||||||
|
apiV1.POST("base/CallUserPb", baseV1.CallUserPb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
6
script/1.js
Executable file
6
script/1.js
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
function decode(val){
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
function encode(val){
|
||||||
|
return val
|
||||||
|
}
|
||||||
4603
script/sign.js
Executable file
4603
script/sign.js
Executable file
File diff suppressed because one or more lines are too long
25
utils/code/code.go
Executable file
25
utils/code/code.go
Executable file
@@ -0,0 +1,25 @@
|
|||||||
|
package code
|
||||||
|
|
||||||
|
const (
|
||||||
|
SUCCESS = 1 // 操作成功
|
||||||
|
ERROR = 10000 // 默认错误返回
|
||||||
|
|
||||||
|
SERVER_ERROR = 10001 // 服务器错误
|
||||||
|
UNKNOW_ERROR = 10002 // 未知错误
|
||||||
|
REMOTE_ERROR = 10003 // 远程服务错误
|
||||||
|
IP_LINMIT = 10004 // 地址限制
|
||||||
|
IP_LINMIT_CODE = 10014 // 地址限制需要验证码
|
||||||
|
NO_PERMISSION = 10005 // 未拥有授权
|
||||||
|
PARAMETER_ERROR = 10008 // 参数错误
|
||||||
|
RPC_ERROR = 10013 // RPC通讯错误
|
||||||
|
NOT_FOUND_ROUTE = 10020 // 未查询到路由
|
||||||
|
NOT_FOUND_METH = 10021 // 未查询到方式
|
||||||
|
|
||||||
|
NOT_FOUND = 10022 // 未查询到
|
||||||
|
AUTH_ERROR = 10023 // 认证错误
|
||||||
|
NO_VALID_TOKEN = 10024 // token无效
|
||||||
|
REPEAD = 10025 // 重复数据或操作
|
||||||
|
OUT_SLINCE = 10026 // 超出限制
|
||||||
|
DB_ERRROR = 10027 // 数据库错误
|
||||||
|
SENSITIVE = 10028 // 敏感词语
|
||||||
|
)
|
||||||
154
utils/exception/exception.go
Executable file
154
utils/exception/exception.go
Executable file
@@ -0,0 +1,154 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
"video/utils/code"
|
||||||
|
)
|
||||||
|
|
||||||
|
type E struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg any `json:"msg"`
|
||||||
|
Err error `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r E) Error() string {
|
||||||
|
if r.Err != nil {
|
||||||
|
return fmt.Sprintf(`{\"code\":%d;\"msg\":\"%v\",\"err\":\"%s\"}`, r.Code, r.Msg, r.Err.Error())
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf(`{\"code\":%d;\"msg\":\"%v\"}`, r.Code, r.Msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r E) Return(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NE(msg any) error {
|
||||||
|
return NEC(msg, code.ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NEC(msg any, code int) error {
|
||||||
|
return &E{
|
||||||
|
Msg: msg,
|
||||||
|
Code: code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NEE(err error, msg string) error {
|
||||||
|
return NEEC(err, msg, code.ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NEEC(err error, msg string, code int) error {
|
||||||
|
if err != nil {
|
||||||
|
return &E{
|
||||||
|
Msg: msg,
|
||||||
|
Code: code,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func PM(msg any) {
|
||||||
|
panic(&E{code.ERROR, msg, nil})
|
||||||
|
}
|
||||||
|
func PMC(msg any, code int) {
|
||||||
|
panic(&E{code, msg, nil})
|
||||||
|
}
|
||||||
|
|
||||||
|
func PBM(flag bool, msg any) {
|
||||||
|
if flag {
|
||||||
|
panic(&E{code.ERROR, msg, nil})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func PBMC(flag bool, msg any, code int) {
|
||||||
|
if flag {
|
||||||
|
panic(&E{code, msg, nil})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PEM(err error, msg any) {
|
||||||
|
if err != nil {
|
||||||
|
panic(&E{code.ERROR, msg, err})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func PEMC(err error, msg any, code int) {
|
||||||
|
if err != nil {
|
||||||
|
panic(&E{code, msg, err})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//type Exception struct {
|
||||||
|
// HttpCode int `json:"-"`
|
||||||
|
// Code int `json:"code"`
|
||||||
|
// Msg interface{} `json:"msg"`
|
||||||
|
// Err error `json:"err"`
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//type ExceptionResponse struct {
|
||||||
|
// Code int `json:"code"`
|
||||||
|
// Msg interface{} `json:"msg"`
|
||||||
|
// Path string `json:"path"`
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func (r *Exception) Error() interface{} {
|
||||||
|
// return r.Msg
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func NotFoundR(c *gin.Context) {
|
||||||
|
// c.JSON(http.StatusForbidden, ExceptionResponse{NOT_FOUND_ROUTE, "Not Found Route", utils.GetReqPath(c)})
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func NotFoundM(c *gin.Context) {
|
||||||
|
// c.JSON(http.StatusForbidden, ExceptionResponse{NOT_FOUND_METH, "Not Found Method", utils.GetReqPath(c)})
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func Panic(c *gin.Context, e *Exception) {
|
||||||
|
// c.JSON(e.HttpCode, ExceptionResponse{e.Code, e.Msg, utils.GetReqPath(c)})
|
||||||
|
//}
|
||||||
|
//func Unknow(c *gin.Context) {
|
||||||
|
// c.JSON(http.StatusForbidden, ExceptionResponse{UNKNOW_ERROR, "未知错误", utils.GetReqPath(c)})
|
||||||
|
//}
|
||||||
|
//func Server(c *gin.Context) {
|
||||||
|
// c.JSON(http.StatusInternalServerError, ExceptionResponse{SERVER_ERROR, "服务器错误", utils.GetReqPath(c)})
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//// FailMsg 主动抛出错误Exception类型
|
||||||
|
//func FailMsg(msg interface{}) *Exception {
|
||||||
|
// return &Exception{http.StatusOK, ERROR, msg, nil}
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func FailCodeMsg(code int, msg string) *Exception {
|
||||||
|
// return &Exception{http.StatusOK, code, msg, nil}
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func PanicMsg(msg interface{}) {
|
||||||
|
// PanicMsgBool(true, msg)
|
||||||
|
//}
|
||||||
|
//func PanicCodeMsg(code int, msg string) {
|
||||||
|
// PanicCodeMsgBool(true, code, msg)
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func PanicMsgBool(flag bool, msg interface{}) {
|
||||||
|
// if flag {
|
||||||
|
// panic(&Exception{http.StatusOK, ERROR, msg, nil})
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//func PanicCodeMsgBool(flag bool, code int, msg string) {
|
||||||
|
// if flag {
|
||||||
|
// panic(&Exception{http.StatusOK, code, msg, nil})
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func PanicMsgErr(err error, msg interface{}) {
|
||||||
|
// if err != nil {
|
||||||
|
// panic(&Exception{http.StatusOK, ERROR, msg, err})
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//func PanicCodeMsgErr(err error, code int, msg string) {
|
||||||
|
// if err != nil {
|
||||||
|
// panic(&Exception{http.StatusOK, code, msg, err})
|
||||||
|
// }
|
||||||
|
//}
|
||||||
Reference in New Issue
Block a user