Skip to content

Commit

Permalink
Merge pull request jxhczhl#24 from jxhczhl/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jxhczhl authored Apr 22, 2024
2 parents 80249a6 + 19d8bfc commit fada16f
Show file tree
Hide file tree
Showing 15 changed files with 615 additions and 391 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
- `/wst` :ws测试使用-发啥回啥 (ws | wss)
- `/go` :获取数据的接口 (get | post)
- `/execjs` :传递jscode给浏览器执行 (get | post)
- `/page/cookie` :直接获取当前页面的cookie (get)
- `/page/html` :获取当前页面的html (get)

说明:接口用?group分组 如 "ws://127.0.0.1:12080/ws?group={}"
以及可选参数 clientId
Expand Down Expand Up @@ -142,7 +144,7 @@ demo.regAction("hello", function (resolve) {
```

访问接口,获得js端的返回值
http://localhost:12080/go?group=zzz&name=hlg&action=hello
http://127.0.0.1:12080/go?group=zzz&action=hello

![image](https://github.com/jxhczhl/JsRpc/assets/41224971/5f0da051-18f3-49ac-98f8-96f408440475)

Expand All @@ -159,6 +161,7 @@ demo.regAction("hello2", function (resolve,param) {
```

访问接口,获得js端的返回值
http://127.0.0.1:12080/go?group=zzz&action=hello2&param=123456

![image](https://github.com/jxhczhl/JsRpc/assets/41224971/91b993ae-7831-4b65-8553-f90e19cc7ebe)

Expand All @@ -181,7 +184,7 @@ demo.regAction("hello3", function (resolve,param) {
访问接口,获得js端的返回值

```python
url = "http://localhost:12080/go"
url = "http://127.0.0.1:12080/go"
data = {
"group": "zzz",
"action": "hello3",
Expand Down
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ HttpsServices:
DefaultTimeOut: 30 # 当执行端没有返回值时,等待%d秒返回超时
CloseLog: false # 关闭一些日志
CloseWebLog: false # 关闭Web服务访问的日志
Mode: release # release:发布版本 debug:调试版 test:测试版本
Cors: false # 是否开启CorsMiddleWare中间件--默认不开启
52 changes: 46 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
package config

import (
"JsRpc/utils"
"errors"
"flag"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"os"
)

func InitConf(path string, conf *ConfStruct) (err error) {
fileContent, err := os.ReadFile(path)
var DefaultTimeout = 30

func ReadConf() ConfStruct {
var ConfigPath string
// 定义命令行参数-c,后面跟着的是默认值以及参数说明
flag.StringVar(&ConfigPath, "c", "config.yaml", "指定配置文件的路径")
// 解析命令行参数
flag.Parse()

conf, err := initConf(ConfigPath)
if err != nil {
return err
log.Warning("读取配置文件错误,将使用默认配置运行。 ", err.Error())
}
return conf
}

func initConf(path string) (ConfStruct, error) {
defaultConf := ConfStruct{
BasicListen: `:12080`,
HttpsServices: HttpsConfig{
IsEnable: false,
HttpsListen: `:12443`,
},
DefaultTimeOut: DefaultTimeout,
}
if !utils.IsExists(path) {
return defaultConf, errors.New("config path not found")
}
if err = yaml.Unmarshal(fileContent, &conf); err != nil {
return err

file, _ := os.Open(path) // 因为上面已经判断了 文件是存在的 所以这里不用捕获错误
defer func(file *os.File) {
err := file.Close()
if err != nil {
}
}(file)
conf := ConfStruct{}
decoder := yaml.NewDecoder(file)
err := decoder.Decode(&conf)
if err != nil {
return defaultConf, err
}
return
DefaultTimeout = conf.DefaultTimeOut
return conf, nil
}

type ConfStruct struct {
Expand All @@ -22,6 +60,8 @@ type ConfStruct struct {
DefaultTimeOut int `yaml:"DefaultTimeOut"`
CloseLog bool `yaml:"CloseLog"`
CloseWebLog bool `yaml:"CloseWebLog"`
Mode string `yaml:"Mode"`
Cors bool `yaml:"Cors"`
}

// HttpsConfig 代表HTTPS相关配置的结构体
Expand Down
Loading

0 comments on commit fada16f

Please sign in to comment.